Extracting data values from MATLAB meshgrid output - matlab

I need to extract data values for a particular coordinate from a meshgrid in MATLAB, my code is the following:
PaarX=Paar(:,1);
PaarX1=PaarX(1:20:length(PaarX));
PaarY=Paar(:,2);
PaarY1=PaarY(1:20:length(PaarY));
x=PaarX;
y=PaarY;
v=Paar(:,3);
[xi, yi]=meshgrid(PaarX1, PaarY1);
vq=griddata(x, y, v, xi, yi, 'cubic');
The PaarX, PaarY and v are the X, Y and Z values of the surface, with the Z values the values to be interpolated. PaarX1 and PaarY1 are the values used in the meshgrid with every 20th value taken (the array was too large before this). I need to extract interpolated Z values in vq from particular X and Y coordinates.

As I understood your question, you need this:
nx = 3; % <= length(PaarX1)
ny = 4; % <= length(PaarY1)
fprintf('the interpolated value at x=%g and y=%g is %g',PaarX1(nx),PaarY1(ny),vq(ny,nx))
Or you can transpose the matrix vq
vq = vq.';
fprintf('the interpolated value at x=%g and y=%g is %g',PaarX1(nx),PaarY1(ny),vq(nx,ny))
vq(ny,nx) (y is first) is because you use the meshgrid function. You can use access to a matrix element in the form vq(nx,ny) (x is first) for the ndgrid function (but I'm not sure that it works with griddata).

Related

Extract a value from a vector field by inputting x and y coordinates [duplicate]

How can I make a function of 2 variables and given a 2D array, it would return an interpolated value?
I have N x M array A. I need to interpolate it and somehow obtain the function of that surface so I could pick values on not-integer arguments.
(I need to use that interpolation as a function of 2 variables)
For example:
A[N,M] //my array
// here is the method I'm looking for. Returns function interpolatedA
interpolatedA(3.14,344.1) //That function returns interpolated value
Here is an example using scatteredInterpolant:
%# get some 2D matrix, and plot as surface
A = peaks(15);
subplot(121), surf(A)
%# create interpolant
[X,Y] = meshgrid(1:size(A,2), 1:size(A,1));
F = scatteredInterpolant(X(:), Y(:), A(:), 'linear');
%# interpolate over a finer grid
[U,V] = meshgrid(linspace(1,size(A,2),50), linspace(1,size(A,1),50));
subplot(122), surf(U,V, F(U,V))
Note that you can evaluate the interpolant object at any point:
>> F(3.14,3.41)
ans =
0.036288
the above example uses a vectorized call to interpolate at all points of the grid
For data on a regular grid, use interp2. If your data is scattered, use griddata. You can create an anonymous function as a simplified wrapper around those calls.
M = 10;
N = 5;
A = rand(M,N);
interpolatedA = #(y,x) interp2(1:N,1:M,A,x,y);
%interpolatedA = #(y,x) griddata(1:N,1:M,A,x,y); % alternative
interpolatedA(3.3,8.2)
ans =
0.53955
Have you seen the interp2 function?
From MatLab documentation:
ZI = interp2(X,Y,Z,XI,YI) returns matrix ZI containing elements corresponding to the elements of XI and YI and determined by interpolation within the two-dimensional function specified by matrices X, Y, and Z. X and Y must be monotonic, and have the same format ("plaid") as if they were produced by meshgrid. Matrices X and Y specify the points at which the data Z is given. Out of range values are returned as NaNs.
Use the spline() command like this:
% A contains N rows and 2 columns
pp = spline(A(:,1), A(:,2));
ppval(pp,3.44)
ans =
0.4454

Graphing a multi variable function in MATLAB

I have never used MATLAB before, so I am very lost. For my calculus class, we were tasked with finding a certain function and then using MATLAB to graph it. Finding the function was no problem. However, trying to graph it has me pulling my hair out. The function is z(x,y)= xy(x+y)(2x+y)(3x+y)(x-2y)(x-3y)(x-4y). Any help or advice is GREATLY appreciated.
You can define a anonymous function handle.
% define function
% .* denotes element wise multiplication
f = #(x,y) x.*y.*(x+y).*(2*x+y).*(3*x+y).*(x-2*y).*(x-3*y).*(x-4*y);
% define range and resolution for x and y
x = -20:0.5:20;
y = -20:0.5:20;
% create meshgrid for 3d plotting
[X, Y] = meshgrid(x,y);
% calculate z values (for meshgrid)
z = f(X, Y);
% plot the function
figure()
surf(x,y,z)
To explain further, since you want to calculate the z value for x and y pairs, you should use a element wise multiplication .*.
Then you have to create a meshgrid for the x and y values, to have all the possible x and y pairs in the two new matrices X and Y. Providing these to your function will calculate the corresponding z value for all these pairs. You can use these for plotting, e.g. surf.

how to interpolate a fluctuated vector in matlab?

I have two arrays
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2.7545 4.0433 5.3763 5.0504];
figure; plot(x, y)
I want to make more samples of x and y then I interpolated both arrays. I tried this code
xi =min(x):.1:max(x);
yi = interp1(x,y,xi);
figure; plot(xi, yi)
but the trajectory is not same as previous plot. Its because the xi is not fluctuating same as x. How should I interpolate both arrays with same trajectory as original one?
This is an issue because when interpolating, MATLAB is going to ignore the order that you feed in the points and instead just sort them based upon their x location.
Rather than interpolating in x/y coordinates, you can instead use a parameter which represents the cumulative arc length of the line segments and use that to interpolate both the x and y coordinates. This will provide you with an interpolant that respects the order and guarantees monotonicity even for multiple values at the same x coordinate.
% Compute the distance between all points.
distances = sqrt(diff(x).^2 + diff(y).^2);
% Compute the cumulative arclength
t = cumsum([0 distances]);
% Determine the arclengths to interpolate at
tt = linspace(t(1), t(end), 1000);
% Now interpolate x and y at these locations
xi = interp1(t, x, tt);
yi = interp1(t, y, tt);

Exporting smoothened data [duplicate]

How can I make a function of 2 variables and given a 2D array, it would return an interpolated value?
I have N x M array A. I need to interpolate it and somehow obtain the function of that surface so I could pick values on not-integer arguments.
(I need to use that interpolation as a function of 2 variables)
For example:
A[N,M] //my array
// here is the method I'm looking for. Returns function interpolatedA
interpolatedA(3.14,344.1) //That function returns interpolated value
Here is an example using scatteredInterpolant:
%# get some 2D matrix, and plot as surface
A = peaks(15);
subplot(121), surf(A)
%# create interpolant
[X,Y] = meshgrid(1:size(A,2), 1:size(A,1));
F = scatteredInterpolant(X(:), Y(:), A(:), 'linear');
%# interpolate over a finer grid
[U,V] = meshgrid(linspace(1,size(A,2),50), linspace(1,size(A,1),50));
subplot(122), surf(U,V, F(U,V))
Note that you can evaluate the interpolant object at any point:
>> F(3.14,3.41)
ans =
0.036288
the above example uses a vectorized call to interpolate at all points of the grid
For data on a regular grid, use interp2. If your data is scattered, use griddata. You can create an anonymous function as a simplified wrapper around those calls.
M = 10;
N = 5;
A = rand(M,N);
interpolatedA = #(y,x) interp2(1:N,1:M,A,x,y);
%interpolatedA = #(y,x) griddata(1:N,1:M,A,x,y); % alternative
interpolatedA(3.3,8.2)
ans =
0.53955
Have you seen the interp2 function?
From MatLab documentation:
ZI = interp2(X,Y,Z,XI,YI) returns matrix ZI containing elements corresponding to the elements of XI and YI and determined by interpolation within the two-dimensional function specified by matrices X, Y, and Z. X and Y must be monotonic, and have the same format ("plaid") as if they were produced by meshgrid. Matrices X and Y specify the points at which the data Z is given. Out of range values are returned as NaNs.
Use the spline() command like this:
% A contains N rows and 2 columns
pp = spline(A(:,1), A(:,2));
ppval(pp,3.44)
ans =
0.4454

How to make contour plots in matlab for a self-defined function f(x,y) in which x and y cannot take vector values?

We know that the usual way to make a contour plot in Matlab for a function Z(x,y) is
[X,Y] = meshgrid(-2:.2:2,-2:.2:3);
Z = X.*exp(-X.^2-Y.^2); (for example)
contour(X,Y,Z);
However, this way does not work for the following function f(x,y):
Suppose h_{ij}(x,y) is a large (e.g., 100x100) matrix, in which each component is a (self-defined) function of x and y. We define another function
f(x,y)=det(h_{ij}(x,y))
and want to make a contour plot of the function f(x,y).
The determinant in f=det(h) requires each component of the matrix h be a number. So f(x,y) can be calculated by Matlab only if x and y are numbers, not vectors. If we use [X,Y]=meshgrid(...), it means that each component of the matrix h is a vector, and f(X,Y) cannot be calculated.
Is there a way to make a contour plot for the above function f(x,y), in which x and y cannot take vector values?
Assuming that h is pre-defined to be a matrix of functions each of which takes two scalar arguments and outputs a matrix (or any valid input to the det function), and the subscripts i and j refer to the indices in X and Y of the arguments to that function, something like the following code should work (X and Y should be the same size as h):
applyh = #(fn, x, y) fn(x, y);
[I, J] = meshgrid(1:m, 1:n);
Z = arrayfun(#(i, j) det(applyh(h(i, j), X(i), Y(j))), I, J);
I think you're misunderstanding what meshgrid does - the output of meshgrid can be easily fed to a function as above. They are not vectors in each element (just a 2-D matrix). You can then plot Z as usual.