interpolate velocity for a curved mesh - matlab

'x','y' and 'u' are 2D matrices for the x-coordinate, y-coordinate, and velocity of each node of the below mesh (figure). and I want to interpolate for (x_q,y_q) to find u_q for an arbitrary point(q).
My mesh is not rectangular and "interp2" error is: "Input grid is not a valid MESHGRID".
Any Ideal what can I do?
Thanks.
minimal reproducible example:
x=[0.0482114583977891,0.0482201588072998,0.0482288592168105;0.0513027685854806,0.0512573490659834,0.0512119412024664;0.0550957532853860,0.0550195888619688,0.0549437786953572;0.0589706773586289,0.0589051888951395,0.0588397004316502];
y=[-0.0481475644832381,-0.0450026617243515,-0.0418577589654650;-0.0454644879533678,-0.0426002662557475,-0.0397360636257961;-0.0434988768053532,-0.0408400599208391,-0.0381819554721181;-0.0414343539900984,-0.0389341351227583,-0.0364339162554183];
u=[1.52583130467469,14.3816671073665,58.5433654462735;108.677373003789,124.842139940676,145.468567077514;110.206733380171,111.157308056414,111.709609403516;135.414711548714,138.843419308648,147.988201447309];
xq=0.065;
yq=0.035;
uq = interp2(x,y,u,xq,yq,'cubic',0);

The interp2 function needs a regular grid (for instance something that was createed by meshgrid). In your case you have scattered data, in which case it you'd have to use griddata for interpolation:
uq = griddata(x,y,u,xq,yq, 'nearest');
(Note that in your MCVE your query point is way outside of the defined input points, so only 'nearest' will really work as a method.)

Per Matlab documentation for the interp2 function, the requirements for cubic spline interpolation are:
Grid must have uniform spacing in each dimension, but the spacing does not have to be the same for all dimensions
Requires at least four points in each dimension
In the case of your code, neither of these conditions is met.
You would need to make sure that the x and y points are evenly spaced, ideally using meshgrid to generate the points and you would need to make the specified points, which are currently 3x4, at least 4x4.
Documentation for the function is found here: https://www.mathworks.com/help/matlab/ref/interp2.html
There is a response to a similar question here: https://www.mathworks.com/matlabcentral/answers/866695-input-grid-is-not-a-valid-meshgrid

Related

Interpolating 3D points from input points corresponding to a closed surface

I have a list of scattered 3D points similar to the one below:
Using MATLAB, I want to interpolate further points from the surface that those original points correspond to, in order to obtain a more complete scatter. Note that there are no particular slices defined on this scattered data. That is, the z values of the point cloud are not discrete, so it's not possible to interpolate slice by slice.
I think that the ideal way to achieve this would be to somehow obtain the smooth closed surface which best matches the scattered data, and then sample it. But I have found no straightforward way to achieve this.
The scatterinterpolant class could be a simple option.
Use scatteredInterpolant to perform interpolation on a 2-D or 3-D
Scattered Data set. For example, you can pass a set of (x,y) points
and values, v, to scatteredInterpolant, and it returns a surface of
the form v = F(x, y). This surface always passes through the sample
values at the point locations. You can evaluate this surface at any
query point, (xq,yq), to produce an interpolated value, vq.
http://au.mathworks.com/help/matlab/math/interpolating-scattered-data.html
Scattered data consists of a set of points X and corresponding values
V, where the points have no structure or order between their relative
locations. There are various approaches to interpolating scattered
data. One widely used approach uses a Delaunay triangulation of the
points.

i have 100*100 matrix, how can i make plot3 graph?

I have a 100 x 100 matrix and i have to use plot3 in MATLAB environment to graph this data. I tried plot3(matrix name) but I faced this error "not enough input arguments". I think plot3 needs 3 input arguments, but I only have this matrix of data. could anyone help me to solve this problem? Is there any alternative for plot3 when we don't have enough arguments?
I need a graph like this:
I think you want to plot the values in a figure as a sort of surface element. What you can do then is:
[X,Y] = size(matrix);
figure;
surface(1:X,1:Y,matrix);
What this does is that it creates a vector for both X and Y indices, as possible in surface. The X and Y indices are obtained by setting them as integers from 1:size, so basically you assign the location of each matrix element to an index.
Note that you can strictly speaking use surface(matrix) as well, but the former approach allows you to use custom indexing, as long as the lengths of the vectors X and Y are the same as the size of your matrix.
For the waterfall use:
figure;
waterfall(matrix);
Sample code:
A=rand(100);
figure;
waterfall(1:100,1:100,A);
Gives:
where you can play around with the name-value pairs, see the documentation on that.
I think what you need is mesh or surf instead of plot3.
plot3 draws a line in 3d-space, so it will need three vectors of the same length (one for each dimension).
When you have a matrix, one reasonable way of displaying it is as a surface in 3d space, which is done by the functions mesh and surf.
Try it out! I hope i helps!

How to use matlab contourf to draw two-dimensional decision boundary

I finished an SVM training and got data like X, Y. X is the feature matrix only with 2 dimensions, and Y is the classification labels. Because the data is only in two dimensions, so I would like to draw a decision boundary to show the surface of support vectors.
I use contouf in Matlab to do the trick, but really find it hard to understand how to use the function.
I wrote like:
#1 try:
contourf(X);
#2 try:
contourf([X(:,1) X(:,2) Y]);
#3 try:
Z(:,:,1)=X(Y==1,:);
Z(:,:,2)=X(Y==2,:);
contourf(Z);
all these things do not correctly. And I checked the Matlab help files, most of them make Z as a function, so I really do not know how to form the correct Z matrix.
If you're using the svmtrain and svmclassify commands from Bioinformatics Toolbox, you can just use the additional input argument (...'showplot', true), and it will display a scatter plot with a decision boundary and the support vectors highlighted.
If you're using your own SVM, or a third-party tool such as libSVM, what you probably need to do is to:
Create a grid of points in your 2D input feature space using the meshgrid command
Classify those points using your trained SVM
Plot the grid of points and the classifications using contourf.
For example, in kind-of-MATLAB-but-pseudocode, assuming your input features are called X1 and X2:
numPtsInGrid = 100;
x1Range = linspace(x1lower, x1upper, numPtsInGrid);
x2Range = linspace(x2lower, x2upper, numPtsInGrid);
[X1, X2] = meshgrid(x1Range, x2Range);
Z = classifyWithMySVMSomehow([X1(:), X2(:)]);
contourf(X1(:), X2(:), Z(:))
Hope that helps.
I know it's been a while but I will give it a try in case someone else will come up with that issue.
Assume we have a 2D training set so as to train an SVM model, in other words the feature space is a 2D space. We know that a kernel SVM model leads to a score (or decision) function of the form:
f(x) = sumi=1 to N(aiyik(x,xi)) + b
Where N is the number of support vectors, xi is the i -th support vector, ai is the estimated Lagrange multiplier and yi the associated class label. Values(scores) of decision function in way depict the distance of the observation x frοm the decision boundary.
Now assume that for every point (X,Y) in the 2D feature space we can find the corresponding score of the decision function. We can plot the results in the 3D euclidean space, where X corresponds to values of first feature vector f1, Y to values of second feature f2, and Z to the the return of decision function for every point (X,Y). The intersection of this 3D figure with the Z=0 plane gives us the decision boundary into the two-dimensional feature space. In other words, imagine that the decision boundary is formed by the (X,Y) points that have scores equal to 0. Seems logical right?
Now in MATLAB you can easily do that, by first creating a grid in X,Y space:
d = 0.02;
[x1Grid,x2Grid] = meshgrid(minimum_X:d:maximum_X,minimum_Y:d:maximum_Y);
d is selected according to the desired resolution of the grid.
Then for a trained model SVMModel find the scores of every grid's point:
xGrid = [x1Grid(:),x2Grid(:)];
[~,scores] = predict(SVMModel,xGrid);
Finally plot the decision boundary
figure;
contour(x1Grid,x2Grid,reshape(scores(:,2),size(x1Grid)),[0 0],'k');
Contour gives us a 2D graph where information about the 3rd dimension is depicted as solid lines in the 2D plane. These lines implie iso-response values, in other words (X,Y) points with same Z value. In our occasion contour gives us the decision boundary.
Hope I helped to make all that more clear. You can find very useful information and examples in the following links:
MATLAB's example
Representation of decision function in 3D space

Filter noisy points during interpolation

I am using matlab's interpolation feature to interpolate the values of some points inside the convex hull formed. However, some of the points inside the convex hull have noisy z value. My input points are of two dimension x & y with z giving the value at the location(x,y). In some cases the z value is particular noise value. So, how can I make sure that it doesn't affect the interpolation, I mean I don't want this value to be considered. Suggestions?
Define your criteria for the point to be 'too noisy'.
Too many standard deviations from the mean? (Calculate mean and standard deviation, then threshold at n standard deviations?)
Too different from the surrounding values? (Use a smoothing/lowpass filter.)
Some background on what this data represents, and some of its characteristics, would help here.

Griddata with 'cubic' Interpolation Method Returns NaN

I found that if I use griddata Method with Cubic Interpolation method, for certain values of x, y, it will return NaN. One post says that this is because the x and y data are very near to convex hull.
Any idea how to fix this?
Edit: Note that I can't make sure that my inputs are monotonously increasing ( thus, gridfit doesn't work). The reason is because I would have to mesh my area ( which could be an irregular polygon in 2D), get all the points before generating the coressponding Z values for each points. My code is as follows:
function ZI=Interpolate3D(scatteredData, boundary)
%scatteredData is the scattered points, boundary is the area that I want to generate 3D surface.
% Given the boundaries, generate mesh first
[element,points]= GenMesh(boundary);
ZI = griddata(scatteredData(:,1),scatteredData(:,2),scatteredData(:,3),points(:,1),points(:,2), 'cubic',{'QJ'});
If your points are outside of the convex hull, you CANNOT get a result other than NaN from griddata using the cubic option. If the point is right on the line, then a NaN may result, depending upon what happens in the least significant bits of the computation.
The issue is that the cubic method uses a triangulation. If your point is outside of the convex hull, then the triangulation fails on that point.
Of course, you CAN use the -v4 method, but there are tremendously good reasons why it has largely been superceded. It uses a distance based interpolation method, where for n data points, a full nxn matrix must be generated. Then a system of equations is solved using that matrix. This will be quite slow for even moderately large problems.
The virtue of the -v4 method is it will extrapolate smoothly without producing nans. This is why it was left in there.
For larger problems where you need a smooth result, and you still wish to extrapolate outside of the convex hull, you can use my gridfit tool. It does do smoothing though, not pure interpolation.
All such methods have tradeoffs that you must resolve for your particular problem.
Since the release of Matlab R2013a you can use scatteredInterpolant instead of griddata.
This has several advantages:
Matlab can perform interpolation as well as extrapolation on a scatteredInterpolant object. You can specify a point outside the convex hull of your scattered data and will still not get a NaN.
Once created, the scatteredInterpolant object can be evaluated multiple times, thus saving computational time compared to calling griddata several times.
On the down side: While you can specify the interpolation and extrapolation methods, cubic is not available but only linear, nearest and natural.
Using scatteredInterpolant your code could look like
F = scatteredInterpolant(scatteredData(:,1),scatteredData(:,2),scatteredData(:,3));
ZI=F(points(:,1),points(:,2));
Are you sure you want cubic interpolation? For some input data the calculated z-nodes could have extreme large values!
I always use -v4 option like the post in your link mentions. You can also play with options used in Qhull via delaunayn, some (but not all) are {'Qt','Qbb','Qc'} http://www.qhull.org/html/qhull.htm