Parametric curves on surfaces in MATLAB - matlab

Suppose we have
x=linspace(-1,1,25);
y=linspace(-1,1,25);
[X,Y]=meshgrid(x,y);
Z = X.^2 - Y.^2;
surf(Z)
How does MATLAB calculate the parametric curves (the black lines in the above figure) for obtaining the surface? Are there any explicit formulas to do that? If the parametrization is (u,v) then how to get MATLAB to spit out the
u=f(x,y,z)
v=g(x,y,z)
functions?

The black line on the surface is described by three vectors corresponding to x,y and z coordinates. For example, if you wish to extract the line corresponding to x=x(5)=-0.6667 you need to extract three vectors that are already contained in the meshgrid and Z-array - X(5,:),Y(5,:),Z(5,:):
x=linspace(-1,1,25);
y=linspace(-1,1,25);
[X,Y]=meshgrid(x,y);
Z = X.^2 - Y.^2;
hold off
surf(X,Y,Z)
hold on;
plot3(X(5,:),Y(5,:),Z(5,:),'r','LineWidth',5)
axis square
Similarly, if you want to extract the line at y=y(5)=-0.6667, then you need: X(:,5),Y(:,5),Z(:,5):
x=linspace(-1,1,25);
y=linspace(-1,1,25);
[X,Y]=meshgrid(x,y);
Z = X.^2 - Y.^2;
hold off
surf(X,Y,Z)
hold on;
plot3(X(:,5),Y(:,5),Z(:,5),'r','LineWidth',5)
axis square
Hope that helps

Related

MATLAB: Symbolic Toolbox Plotting versus Discreet Plotting

I'm wondering if anyone has any insight into why these two plot commands produce domains that are orders of magnitude different?
syms t
x1Axis = 0:.01:10;
fun1(t) = sin(t)
plot(sin(x1Axis))
hold on
y = sin(x1Axis)
plot(x1Axis, y)
fun1(t) is plotted "symbolically" while y is evaluated and plotted "discreetly". Should I be using a different plot method in the case of the first function?
No, you are not plotting the symbolic function correctly.
In your code, the instruction plot(sin(x1Axis)) is not a symbolic plot, but a numeric plot of the sine versus the index of each value.
From the plot documentation page:
plot(Y) creates a 2-D line plot of the data in Y versus the index of
each value.
If Y is a vector, then the x-axis scale ranges from 1 to length(Y).
To plot the symbolic function use fplot.
The following example will allow you to see that both the symbolic and numeric plots are the same:
xmin = 0;
xmax = 10;
% Symbolic Plot.
syms t
fun1(t) = sin(t);
fplot(fun1, [xmin xmax], '-r');
hold on;
% Numeric Plot.
x = xmin:.01:xmax;
y = sin(x);
plot(x, y, '--g');
% Add legend.
legend('Symbolic Plot', 'Numeric Plot', 'Location', 'north');
This is the result:

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)

Graphing a hyperbola in Matlab

I'm trying to graph a solution obtained through the quadratic formula in Matlab. Since it's obtained by the quadratic formula, there are two parts: plus and minus. The graph should be a hyperbola. How can I place the upper part and the bottom part on the same graph?
There are different ways. Let's say you want to plot the solution of y^2 = x, that is y = ±sqrt(x):
You can plot the two parts with the same color using a plot once…
x = 0:0.1:10;
plot(x, sqrt(x), 'k', x, -sqrt(x), 'k')
…or twice:
x = 0:0.1:10;
plot(x, sqrt(x), 'k')
hold on
plot(x, -sqrt(x), 'k')
hold off
Or you can plot everything in one go like you might draw it with a pen:
x = [10:-0.1:0 0.1:0.1:10];
y = [-sqrt(10:-0.1:0) sqrt(0.1:0.1:10)];
plot(x, y)

Cross section 2D data of symmetric len into Z matrix MATLAB - 3D plot from 2D cross section

I have a problem and maybe you will be able to help me. Like in the title i have cross section data of a symmetric lens - coordinates s=-100:1:100 and height y - and I would like to create 3D plot the whole lens (x,y,z). Is there any build in function that helps with that? Thanks for help in advance!
If I'm understanding correctly, you have a 1-D array that you'd effectively like to 'sweep' around a circle to produce a 3-D plot. Here is an example of how to do that
% Some dummy data
Npts = 100;
z = sin(linspace(0, pi, Npts));
Nreps = 100; % How many times to repeat around circle
% Create polar meshgrid and convert to Cartesian
[r, theta] = meshgrid( ...
linspace(-length(z)/2, length(z)/2, Npts), ...
linspace(0, pi, Nreps));
[X, Y] = pol2cart(theta, r);
% Copy data Nreps times
Z = repmat(z, Nreps, 1);
% Plot!
surf(X, Y, Z)
Without more specs (such as if your y is a 2D matrix or a 1D array), it's not possible to give you the exact answer. However here is how you draw a surface in Matlab:
% create a meshgrid used as the independent variables of your surface
[sx, sy] = meshgrid(-100:100);
% if you have your own 2D matrix, ignore this line.
% if you have a formula, replace this line with your own formula
y = cos(sqrt(((sx/100).^2+(sy/100).^2)/2)*(pi/2));
% draw the surface
surf(sx, sy, y);
To have the opposite side as well, draw another surf on the same figure:
hold on;
surf(sx, sy, -y);

How to let the axes in matlab have different lengths without changing axes limits

I'm trying to plot a 3D surface in Matlab and I want to "compress" the plot a little bit in the z dimension. Now the lengths of x, y and z axes are the same, and the plot looks like a cube. I'd like it to look flatter in the z dimension, without altering the axes limits.
Is there any easy way to achieve this?
Try fiddling with the DataAspectRatio and the PlotBoxAspectRatio properties of the axes, which may also be controlled by the pbaspect and daspect commands, correspondingly.
Example
%// Plot surface
[X, Y] = meshgrid(-10:.1:10, -10:.1:10);
Z = 100 - X .^ 2 - Y .^ 2;
surf(X, Y, Z, 'EdgeColor', 'None')
%// Flatten the z-axis a bit
pbaspect([1 1 .2])
daspect([1 1 50])
Original plot:
Flattened plot: