Transform a solution vector of PDE into a piecewise linear function - MATLAB - matlab

I know that when I use the PDE toolbox in Matlab to solve a PDE the result is a vector, which represents the values of the function in each vertex of the mesh.
Is there a command in the PDE toolbox such that we could transform the vector solution into a piecewise linear function on the domain of definition, so that we could be able to use it like u(x,y) to find directly the approximate value in (x,y)?

I don't know about such function. But your solution is defined on a structured rectangular grid. If you simply need to interpolate data on a 2D rectangular grid, you can use interp2 for that. If your grid is made of triangles, use TriScatteredInterp. If you want to use different interpolation (e.g., FEM), you will have to implement it yourself.

Related

Multidimensional Shape Preserving/Monotone Spline - Matlab

Does anyone know whether matlab has anything similar to PCHIP for multidimensional interpolation? I can use it for 1-dimensional interpolation. For multidimensional, matlab only allows me to use spline. The issue with splines is that they are not shape preserving. Below an example taken from here on why the difference between them is actually very important:

Solving PDE system in Matlab: Derivatives on a mesh

I am using MatLab to solve a system of coupled PDEs, with pdenonlin.
I create a mesh for my geometry (a square box with a circular hole in the middle), and refine it until I :
[p,e,t] = initmesh('DefectGeom2');
[p,e,t] = refinemesh('DefectGeom2',p,e,t);
I solve the system
% SOLUTION:
u = pdenonlin(b_s,p,e,t,c_s,a_s,f_s);
% EXTRACT different functions from the full solutions (systems):
np = size(p,2); % number of node points
uk = reshape(u,np,[]); % each uk column has one component of u
Therefore I now have my uk (in my case 3) solutions.
Now I want to calculate integrals and derivatives of this approximate solutions. I tried by interpolating to a uniform grid using tri2grid:
x=linspace(-1,1,Npts);
y=x;
psi=tri2grid(p,t,uk(:,3),x,y);
theta=tri2grid(p,t,uk(:,1),x,y);
theta_y=derivative(theta,1,2);
psi_x=derivative(psi,1,1);
and calculate:
pressure = trapz(x,psi_x-cos(2*theta).*theta_y+sin(2*theta));
But this gives me a poor approximation, I guess because of the fact that the grid is uniform whereas the mesh is finer around the central circle and coarser elsewhere.
Is there a way I could use MatLab built-in functions to calculate derivatives of the solutions of pdenonlin without brutally interpolate with tri2grid on a uniform grid?
There is a pdeInterpolant class in the pde toolbox which might be useful. Use meshToPet to generate the input arguments from your mesh and then evaluate to query the interpolant.
You can specify a different grid when you query the interpolant so if you use a finer grid for this stage you might get better results without having to alter the original mesh.

Method behinds "interp2" command

I always use interp2 to interpolate my data in MATLAB.
But I want to know what kind of algorithm is really used when running that command, such as Local Weight Regression (LWR) or something.
If you type doc interp2, you'll see that you can specify one of four different methods that interp2 can use: nearest neighbour interpolation, linear interpolation, cubic spline interpolation, or cubic interpolation.
If you don't specify one of those explicitly, it will use the default method, which is linear interpolation.

How to plot a 3D figure in matlab based on a function like f(x,y,z)=0?

How to plot a 3D figure in MATLAB based on a function like f(x,y,z)=0?
And this complicated function can not be written as z = f(x,y).
f(x,y,z)=sum(a.*exp(sv(:,1)-x).^2+sv(:,2)-y).^2+sv(:,3)-z).^2)-b=0
where a is a known vector, sv is a known matrix, b is a known value. x,y,z are three variables. How to draw this surface in 3D way in matlab?
I just solve this question by this tool from the Matlab File Exchange:
Ezimplot3: implicit 3D functions plotter
your function only contains 1D vectors( I am assuming they are of equal lengths), if summed it will give you a constant; therefore, there is really nothing to plot.

Plotting quaternion in Matlab using "engine.h" from c++

I have an algorithm in C++ that uses Kalman Filter. Somewhere in the code a predict a Quaternion q' and then I update the Quaternion with Kalman Filter q.
I want to plot two graphics in Matlab with the evolution of the predicted quaternion and the corrected(updated) quaternion so I am using "engine.h" library to send quaternion info to Matlab during processing (actually what I send is a 4x1 matrix).
So my question is: What is the best way to plot a quaternion in Matlab so I can visually extract information? Is it maybe better to plot the angles separately?
I think a good option is sending the quaternion as a vector to MATLAB, using C++ MATLAB engine
[qx qy qz qw]
Then, in MATLAB environment you can use a toolbox for translating to Euler Angles, which is a common visual option.
For adding a path of a toolbox in matlab engine:
addpath(genpath('C:\Program Files (x86)\MATLAB\R2010a\toolbox\SpinCalc'));
With spincalc toolbox, converting would be something like this:
Angles=SpinCalc('QtoEA321',Quaternion,0,0);
Well, assuming that the question is "How to visualize in a nice way a 4D space",
I can think of a few options:
Show multiple slices of the space, that is for (x,y,z,t) -> (x,y), (y,z),etc..
Show (x,y) as scatter plot and encode the information of z in color, t in size of dot. For that you can use the scatter command :
SCATTER(X,Y,S,C) displays colored circles at the locations specified
by the vectors X and Y (which must be the same size).
If your question was "How to visualize in a nice way quarternions,
check this out