Graphing a multi variable function in MATLAB - 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.

Related

How to get the integral (function) from a vector?

I have a vector than can be plotted and I would like to compute its integral. I don't mean the total area below, but how it evolves over the domain of integration. Basically, its "indefinite" integral. Is this possible? maybe via interpolation? Since I am working with Chebyshev differentiation matrices, do you know if there is an equivalent for integration?
Thank you
You probably want cumtrapz:
x = linspace(0,2*pi,1000); % example x axis values
y = sin(x); % example function
I = cumtrapz(x, y); % compute its integral
plot(x, y, x, I) % plot them
grid on % grid

How to plot using surf gird in 2D using function value

if the function F is available it is easy to draw surf plot i.e.
x=1:0.1:4;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=sin(X).^2+cos(Y).^2;
surf(X,Y,Z);
view(2) ;
in my case I calculated F function using least square:
for example I have x and y vectors
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2754.5 4043.3 5376.3 5050.4];
the linear function of these two vector is define by
F= [1149.73 , 37.63];
therefore the estimation is equal to
z= [ones(5,1) x']*F';
which is
z = [1149.73 1519.67 4051.96 5589.35 4912.65];
and if it is plotted
plot(x,y,'b.');
hold on;plot(x,y,'b-');
hold on; plot(x,z,'-r');
The linear z ( red line) is showing correctly. Now I want to plot it for all possible combination of x and y using grid and I need to have a mesh for all inputs
[X,Y] = meshgrid(x,y);
but how to make the Z matrix to show the intensity plot of function Z? The Z suppose to have high intensity close to z value and less value far from it. I should suppose to get something like this
Thanks
P.S: the F is calculated using pinv (least square).
You have to interpolate the scattered data to plot it on grid. Here is a simple example for your x, y and z vectors
xi=linspace(min(x),max(x),100)
yi=linspace(min(y),max(y),100)
[XI YI]=meshgrid(xi,yi);
ZI = griddata(x,y,z,XI,YI);
contourf(XI,YI,ZI)

Creating a surface correctly

I want to create and show the surface z=x*exp(-x^2-y^2) in the section x,y~[-10;10]. I am trying to use:
x=-10:10;
y=-10:10;
z=x*exp(-x^2-y^2);
[X,Y,Z]=meshgrid(x,y,z);
surf(X,Y,Z);
and getting:
"Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead."
I understand that x is a vector and so this is not a logical statement. Never the less, I do not have an Idea as to how to create this surface?
You'll want to use meshgrid before computing z so that you compute a value for z for each combination of x and y. Also you'll want to use element-wise operators (.^ and .*) to create z
% Create all permutations of x and y
[x, y] = meshgrid(-10:10,-10:10);
% Compute z for each permutation
z = x .* exp(-x.^2 - y.^2);
% Plot as a surface
surf(x, y, z)

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

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.