Plot corresponding lines on the surface - matlab

[X Y]=ndgrid(-1:0.1:1,-1:0.1:1);
Z = sin(X)+sin(Y);
surf(X,Y,Z)
How to plot corresponding lines (taken from grid rows or columns) in Matlab?

You can use the following code
[X Y]=ndgrid(-1:0.1:1,-1:0.1:1);
Z = sin(X)+sin(Y);
surf(X,Y,Z)
hold on
for d = [3,5,7,10,15]
plot3(X(d,:),Y(d,:),Z(d,:),'LineWidth',2,'Color','r')
end

Related

How to create multiple plots on the same graph in matlab

How I can plot the following graph in Matlab, and how to name av_d, av_c, ...
Thank you so much!
To get you started:
% Example data
x = 0:0.01:2*pi;
z = sin(x);
y = ones(size(x));
% Three identical lines plotted in x-z for different y
line([x;x;x;x]',[0*y;y;2*y;3*y]',[z;z;z;z]')
% Rename the yticks
set(gca,'YTickLabel',{'L1','L2','L3','L4'})
grid on
% Change the viewing angle
view(45,45)
Output:

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 overplot comet3 on a surf graph

I would like to overplot a comet3 plot over a surf plot, is this possible? hold on did not work.
It should work with the appropriate x,y and z values for the surface you are trying to display.
Simple example:
clc
clear
t = -2*pi:pi/100:2*pi;
x = (cos(2*t).^2).*sin(t);
y = (sin(2*t).^2).*cos(t);
%// Create x and y data for the surface plot
[xx,yy] = meshgrid(x,y);
zz = xx.^2 + yy.^2 - 7;
surf(xx,yy,zz)
hold on
comet3(x,y,t);
output:

Plotting elliptical paraboloid in matlab

How can I plot an elliptical paraboloid in MATLAB with surf() function, using parametric equations with 2 variables u and v? The equation looks like
r = {ucos{v}, u^2,5usin{v}}
I understand that I need to make a meshgrid from u and v, but what to do next?
You can do it the following way:
%// Create three function handles with the components of you function
fx = #(u,v) u.* cos(v); %// Notice that we use .*
fy = #(u,v) u.^2; %// and .^ because we want to apply
fz = #(u,v) 5.*u.*sin(v);%// multiplication and power component-wise.
%// Create vectors u and v within some range with 100 points each
u = linspace(-10,10, 100);
v = linspace(-pi,pi, 100);
%// Create a meshgrid from these ranges
[uu,vv] = meshgrid(u, v);
%// Create the surface plot using surf
surf(fx(uu,vv), fy(uu,vv), fz(uu,vv));
%// Optional: Interpolate the color and do not show the grid lines
shading interp;
%// Optional: Set the aspect ratio of the axes to 1:1:1 so proportions
%// are displayed correctly.
axis equal;
I added some annotation, because you seem to be new to Matlab.

Drawing a surface with x,y,z points

I have a list of x, y, z in this form:
-0.2894 1.2835 0.5405
-0.8171 -0.3034 0.1824
2.7864 0.5506 0.0037
I could plot using plot3(x,y,z, '*') and it works fine.
Now I would like to draw a surface, but when I do:
>> surf(x,y,z)
??? Error using ==> surf at 78
Z must be a matrix, not a scalar or vector.
How should I draw it?
I suggest you do a Delaunay triangulation of the x,y values, and then use z as height for a surface plot:
x = randn(100,1);
y = randn(100,1);
z = (exp(-x.^2-y.^2));
tri = delaunay(x,y);
trisurf(tri,x,y,z)
EDIT
Since you seem to have problems with your Matlab version, here's an alternative: use griddata to interpolate your data onto a regular grid, so that you can use surf for plotting.
x = randn(100,1);
y = randn(100,1);
z = (exp(-x.^2-y.^2));
[xx,yy]=meshgrid(-2:0.1:2,-2:0.1:2);
zz = griddata(x,y,z,xx,yy);
dfig,surf(xx,yy,zz)