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

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

Related

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.

Extracting data values from MATLAB meshgrid output

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).

Matlab interpolation of a 2D ARRAY [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

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 generate a function of two variables without using any loop?

Suppose I have a function y(t,x) = exp(-t)*sin(x)
In Matlab, I define
t = [0: 0.5: 5];
x = [0: 0.1: 10*2*pi];
y = zeros(length(t), length(x)); % empty matrix init
Now, how do I define matrix y without using any loop, such that each element y(i,j) contains the value of desired function y at (t(i), x(j))? Below is how I did it using a for loop.
for i = 1:length(t)
y(i,:) = exp(-t(i)) .* sin(x);
end
Your input vectors x is 1xN and t is 1xM, output matrix y is MxN. To vectorize the code both x and t must have the same dimension as y.
[x_,t_] = meshgrid(x,t);
y_ = exp(-t_) .* sin(x_);
Your example is a simple 2D case. Function meshgrid() works also 3D. Sometimes you can not avoid the loop, in such cases, when your loop can go either 1:N or 1:M, choose the shortest one. Another function I use to prepare vector for vectorized equation (vector x matrix multiplication) is diag().
there is no need for meshgrid; simply use:
y = exp(-t(:)) * sin(x(:)'); %multiplies a column vector times a row vector.
Those might be helpful:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/meshgrid.html
http://www.mathworks.com/company/newsletters/digest/sept00/meshgrid.html
Good Luck.