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.
Related
'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
I am trying trying to graph the polynomial fit of a 2D dataset in Matlab.
This is what I tried:
rawTable = readtable('Test_data.xlsx','Sheet','Sheet1');
x = rawTable.A;
y = rawTable.B;
figure(1)
scatter(x,y)
c = polyfit(x,y,2);
y_fitted = polyval(c,x);
hold on
plot(x,y_fitted,'r','LineWidth',2)
rawTable.A and rawTable.A are randomly generated numbers. (i.e. the x dataset cannot be represented in the following form : x=0:0.1:100)
The result:
second-order polynomial
But the result I expect looks like this (generated in Excel):
enter image description here
How can I graph the second-order polynomial fit in MATLAB?
I sense some confusion regarding what the output of each of those Matlab function mean. So I'll clarify. And I think we need some details as well. So expect some verbosity. A quick answer, however, is available at the end.
c = polyfit(x,y,2) gives the coefficient vectors of the polynomial fit. You can get the fit information such as error estimate following the documentation.
Name this polynomial as P. P in Matlab is actually the function P=#(x)c(1)*x.^2+c(2)*x+c(3).
Suppose you have a single point X, then polyval(c,X) outputs the value of P(X). And if x is a vector, polyval(c,x) is a vector corresponding to [P(x(1)), P(x(2)),...].
Now that does not represent what the fit is. Just as a quick hack to see something visually, you can try plot(sort(x),polyval(c,sort(x)),'r','LineWidth',2), ie. you can first sort your data and try plotting on those x-values.
However, it is only a hack because a) your data set may be so irregularly spaced that the spline doesn't represent function or b) evaluating on the whole of your data set is unnecessary and inefficient.
The robust and 'standard' way to plot a 2D function of known analytical form in Matlab is as follows:
Define some evenly-spaced x-values over the interval you want to plot the function. For example, x=1:0.1:10. For example, x=linspace(0,1,100).
Evaluate the function on these x-values
Put the above two components into plot(). plot() can either plot the function as sampled points, or connect the points with automatic spline, which is the default.
(For step 1, quadrature is ambiguous but specific enough of a term to describe this process if you wish to communicate with a single word.)
So, instead of using the x in your original data set, you should do something like:
t=linspace(min(x),max(x),100);
plot(t,polyval(c,t),'r','LineWidth',2)
I have two pairs of data sets, x1 vs y1 and x2 vs y2. x1, y1, x2, y2 all have uneven distribution of data represented by the following images:
My problem is to determine the intersection of the two pairs of data sets, x1/y1 and x2/y2, shown in following image:
I tried interpolating the data points to have even spacing, but due invalid regions of x1/y1 where there are multiple solutions for the same x value.
Here is a zoom in of the x1/y1 and x2/y2 relationship, showing that there are knots within the data set that cannot be interpolated in any orientation:
It seems that x2/y2 is a smooth curve, so you should be able to interpolate it piecewise with polynomials, and get decent results. Of course you will not want to do this with x1/y1, as your data is crazy. I will refer to the independent variable in the last two images as t. You can use the matlab spline function to do this interpolation from arrays of t and x2/y2 values. Your t value array in this case should be the same size as your set of x2/y2 values. Then you could loop over your x1/y1 points, using the interpolation to estimate x2/y2 at the same value of t. Then you could subtract these values. When the sign of this value changes for two consecutive x1/y1 points, you have a point of intersection between them. Then perform a linear interpolation between those two x1/y1 points and find the intersection of that line with your interpolated x2/y2 function. The code may get a little messy, but it should work. You will want to look at the MATLAB spline documentation.
this is my problem:
I have the next data "A", which looks like:
As you can see, I have drawn with red circles the apparently peaks, the most defined are 2 and 7, I say that they are defined because its standard deviation is low in comparison with the other peaks (especially the second one).
What I need is a way (anyway) to get the values and the standard deviation of n peaks in a numeric array.
I have tried with "clusters", but I got no good results:
First of all, I used "kmeans" MATLAB function, and I realize that this algorithm doesn't group peaks as I need. As you can see in the picture above, in the red circle, that cluster has at less 3 or 4 peaks. And kmeans need that you set the number of clusters, and I need to identify it automatically.
I hope that anyone can give me some ideas, or a way to get better results, thanks.
Pd: I leave the data "A" in the next link.
https://drive.google.com/file/d/0B4WGV21GqSL5a2EyQ2l0SHZURzA/edit?usp=sharing
The problem is that your axes have very different meaning.
K-means optimizes variance. But variance in X is something entirely different than variance in Y, isn't it? Furthermore, each of these methods will split your data in both X and Y, whereas I assume you want the data to be partitioned on the X axis only.
I suggest the following: consider the Y axis to be a weight, and X axis to be a position.
Then perform weighted density estimation, and look for low density to separate your clusters.
I can't help you with MATLAB. I don't use it.
Mathematically, what you want to do is place a Gaussian at each point, with area Y and center X. Then find minima and maxima on the sum of these Gaussians. See Wikipedia, Kernel Density Estimation for details; except that you want to use the Y axis as weights. You could maybe also use 1/Y as standard deviation, if you don't want to use weights.
I have a vector of 358 numbers. I'd like to make a numerical integration of this vector, but I don't know the function of this one.
I found that we can use trapz or quad, but i don't really understand how to integrate without the function.
If you know the horizontal spacing of your vector, you can use trapz in order to integrate it without the function. For example, to integrate y=sin(x) from 0 to pi with 358 sections,
x=0:pi/357:pi;
y=sin(x);
area=trapz(x,y);
If you just use trapz(y), you'll get a much larger number, since the default distance between points is assumed to be 1. This problem can be fixed by multiplying by the distance between x points:
area=pi/357*trapz(y);
You don't need to know the function in order to numerically integrate; that's the point of trapz and quad. Just pass trapz your vector. Here's a link to the documentation.
Think about integration as to find area under the curve, which is formed by your vector. Well it's not actually a curve, but polygonal chain. What TRAPZ function is doing, it finds sum of areas of each trapezoids formed by every two neighbor points in your vector and their projection on X axis. See the function documentation, if you have uneven distance between your points or if distance not equal one.
You can read more about this method, for example, on Wikipedia.