How to plot a function of two variable one defined in Matlab - matlab

How do you plot a user-defined function of two variables in Matlab?

X, Y = meshgrid(xs, ys); % values of x and y at which we want to evaluate
Z = my_func(X,Y);
surf(X,Y,Z);
Alternatively, if your function is not vectorized,
X, Y = meshgrid(xs, ys); % values of x and y at which we want to evaluate
for x = 1:length(xs)
for y = 1:length(ys)
Z(x,y) = my_func(X(x,y), Y(x,y));
end
end
Z = my_func(X,Y);
surf(X,Y,Z);

ezsurf is a simple solution, or ezmesh, or ezcontour, or ezsurfc, or ezmeshc.

It has many types.
You can go plot gallery and select your variable and then types like mesh, 3D, surface, ...

Related

Extracting Z values after 3D plot is generated by 2D curve revolution with repmat

I am stuck with an apparently simple problem. I have to revolve of 360° a 2D curve around an axis, to obtain a 3D plot. Say, I want to do it with this sine function:
z = sin(r);
theta = 0:pi/20:2*pi;
xx = bsxfun(#times,r',cos(theta));
yy = bsxfun(#times,r',sin(theta));
zz = repmat(z',1,length(theta));
surf(xx,yy,zz)
axis equal
I now want to visualize the numerical values of the Z plane, stored in a matrix. I would normally do it this way:
ch=get(gca,'children')
X=get(ch,'Xdata')
Y=get(ch,'Ydata')
Z=get(ch,'Zdata')
If I visualize Z with
imagesc(Z)
I don't obtain the actual values of Z of the plot, but the "un-revolved" projection. I suspect that this is related to the way I generate the curve, and from the fact I don't have a function of the type
zz = f(xx,yy)
Is there any way I can obtain the grid values of xx and yy, as well as the values of zz at each gridpoint?
Thank you for your help.
Instead of bsxfun you can use meshgrid:
% The two parameters needed for the parametric equation
h = linspace(0,2) ;
th = 0:pi/20:2*pi ;
[R,T] = meshgrid(h,th) ;
% The parametric equation
% f(x) Rotation along Z
% ↓ ↓
X = sin(R) .* cos(T) ;
Y = sin(R) .* sin(T) ;
% Z = h
Z = R ;
surf(X,Y,Z,'EdgeColor',"none")
xlabel('X')
ylabel('Y')
zlabel('Z')
Which produce:
And if you want to extract the contour on the X plane (X = 0) you can use contour:
contour(Y,Z,X,[0,0])
Which produce:

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

Matlab plot ksdensity without first storing its arguments

In MATLAB, if I want to plot density of a variable V I have to do
[x, y] = ksdensity(V);
plot (y, x);
If I do plot(ksdensity(V)), it only plots x and not x Vs y.
Is there an easier alternative to give ksdensity() as an argument to plot() and do the same job as plot(y, x)?
You can refactor it into a function that takes in V and plots y vs x:
function h = plot_ksdensity(V, varargin)
[x, y] = ksdensity(V);
h = plot (y, x, varargin{:});
end
using varargin means you will still have access to plot options like colours. hold on will also still work because this just calls the regular plot function.
Unfortunately no. If you don't specify explicitly the outputs, a function will return always the leftmost one from output parameter list. To convince yourself about that, create the function ftest() somewhere in your MATLAB path:
function [x, y] = ftest( )
x = 1;
y = 2;
end
then call it in the Command Window without specifying the outputs
>> ftest()
ans =
1

Matlab : x v/s y plot

I want to plot a function y=1-exp(c) ,where as the range of x is defined. The plot is to be between x and the function y. The plot just shows just 1 point instead of showing a series of points exponentially.I am new in Matlab.Sp,please help me where I am going wrong Here is the code:
for x = -10:0.25:10
if(x>0)
c=-6*x;
m=exp(c);
y = 1-m
end
plot(x,y,'o')
xlabel('x')
ylabel('y')
title('Plot')
end
This should do it:
x = -10:0.25:10; % define the x vector
c= -5*x.*(x>0); % using a logical condition the 'if' is avoided
y = 1-exp(c); % calc y given c
plot(x,y,'o')
xlabel('x')
ylabel('y')
title('Plot')
no 'for' loop or 'if' needed...
Your problem is the for loop. It is resetting the value of y and re-ploting that one point each loop. You don't need that loop at all. This code will do the trick for y = 1-exp(A*x)
Edit (2012-10-30) OP says y is zero for x<=0. #Nate's code in the answer above is probably best, but here I use logical indexing to show a different way to do the same thing.
x = -10:0.25:10; % <vector>
y = zeros(size(x)); % prealocate y with zeros and make it the same size as x
y(x>0) = 1 - exp(-5*x(x>0)); % only calculate y values for x>0
plot(x,y,'o')
xlabel('x')
ylabel('y')
title('Plot')