Matlab - how to plot the intersection between an implicitly defined surface and a plane - matlab

I'm not very familiar with matlab, but I'm trying to plot the intersection of a plane (x + y + z = 1) with a surface. the surface is defined implicitly (x, y, and z as functions of alpha, beta). this is my code:
alpha = linspace(0,pi);
beta = linspace(0,pi);
[alpha,beta]=meshgrid(alpha,beta);
L= 4*exp(-.6*beta).*sin(alpha);
%converting to x,y,z coordinates:
x = L.*sin(alpha).*cos(beta);
y = L.*cos(alpha);
z= L.*sin(alpha).*sin(beta);
to plot this i generally would use surf(x,y,z). but in this case i want to plot its intersection with a plane, for example the one defined by z2 = 1-x+y. (im not sure whether it would be better to define separate matrices for new x, y values, or whether it is better to use the existing ones.) i hope this question isn't too confusing. if you have any advice, please help.

What if you try the following: (I based my code on this answer to a previous question Plotting Implicit Algebraic equations in MATLAB) and it is not perfect but maybe it can help you:
clear
clc
alpha = linspace(0,pi);
beta = linspace(0,pi);
[alpha,beta]=meshgrid(alpha,beta);
L= 4*exp(-.6*beta).*sin(alpha);
x = L.*sin(alpha).*cos(beta);
y = L.*cos(alpha);
z= L.*sin(alpha).*sin(beta);
% Use an anonymous function to define the plane you want to plot
[X,Y] = meshgrid(min(x(:)):.5:max(x(:)),min(y(:)):0.5:max(y(:))); % x and y limits
f = #(X,Y) -X+Y+1; % sorry for the choice of capital letters; it was the most intuitive I thought.
hold on
surf(x,y,z);
contour3(X,Y,f(X,Y),200); % the 200 is arbitrary; play with it to change the # of lines making up the plane
hold off
rotate3d on
The result looks like this:
As I said I'm not 100% confident this is the most robust way but it looks fine to me :)

Related

Plot a surface in MATLAB

I want to plot a surface in MATLAB using surf. I have this equation: x = y^2 +4z^2.
What I am doing is the following:
[x,y] = meshgrid(-4:.1:4, -4:.1:4);
z = sqrt((x - y.^2)./4); % Basically I'm just clearing for z
surf(x,y,z)
But with this I am getting the error: Error using surf X,Y,Z and C cannot be complex. I know there is a complex number because of the values that x and y have, plus the square root. Is there another way to plot a surface in MATLAB? because I really don't know what to do, and my skills are very basics.
Why do you feel that you need to grid x and y, and not use the form of the original equation itself?
This seems to work perfectly fine
[y,z] = meshgrid(-4:.1:4, -4:.1:4);
x = y.^2 + 4*z.^2;
surf(x,y,z)
to produce

Draw a line with non-Cartesian coordinates in MATLAB

MATLAB's surf command allows you to pass it optional X and Y data that specify non-cartesian x-y components. (they essentially change the basis vectors). I desire to pass similar arguments to a function that will draw a line.
How do I plot a line using a non-cartesian coordinate system?
My apologies if my terminology is a little off. This still might technically be a cartesian space but it wouldn't be square in the sense that one unit in the x-direction is orthogonal to one unit in the y-direction. If you can correct my terminology, I would really appreciate it!
EDIT:
Below better demonstrates what I mean:
The commands:
datA=1:10;
datB=1:10;
X=cosd(8*datA)'*datB;
Y=datA'*log10(datB*3);
Z=ones(size(datA'))*cosd(datB);
XX=X./(1+Z);
YY=Y./(1+Z);
surf(XX,YY,eye(10)); view([0 0 1])
produces the following graph:
Here, the X and Y dimensions are not orthogonal nor equi-spaced. One unit in x could correspond to 5 cm in the x direction but the next one unit in x could correspond to 2 cm in the x direction + 1 cm in the y direction. I desire to replicate this functionality but drawing a line instead of a surf For instance, I'm looking for a function where:
straightLine=[(1:10)' (1:10)'];
my_line(XX,YY,straightLine(:,1),straightLine(:,2))
would produce a line that traced the red squares on the surf graph.
I'm still not certain of what your input data are about, and what you want to plot. However, from how you want to plot it, I can help.
When you call
surf(XX,YY,eye(10)); view([0 0 1]);
and want to get only the "red parts", i.e. the maxima of the function, you are essentially selecting a subset of the XX, YY matrices using the diagonal matrix as indicator. So you could select those points manually, and use plot to plot them as a line:
Xplot = diag(XX);
Yplot = diag(YY);
plot(Xplot,Yplot,'r.-');
The call to diag(XX) will take the diagonal elements of the matrix XX, which is exactly where you'll get the red patches when you use surf with the z data according to eye().
Result:
Also, if you're just trying to do what your example states, then there's no need to use matrices just to take out the diagonal eventually. Here's the same result, using elementwise operations on your input vectors:
datA = 1:10;
datB = 1:10;
X2 = cosd(8*datA).*datB;
Y2 = datA.*log10(datB*3);
Z2 = cosd(datB);
XX2 = X2./(1+Z2);
YY2 = Y2./(1+Z2);
plot(Xplot,Yplot,'rs-',XX2,YY2,'bo--','linewidth',2,'markersize',10);
legend('original','vector')
Result:
Matlab has many built-in function to assist you.
In 2D the easiest way to do this is polar that allows you to make a graph using theta and rho vectors:
theta = linspace(0,2*pi,100);
r = sin(2*theta);
figure(1)
polar(theta, r), grid on
So, you would get this.
There also is pol2cart function that would convert your data into x and y format:
[x,y] = pol2cart(theta,r);
figure(2)
plot(x, y), grid on
This would look slightly different
Then, if we extend this to 3D, you are only left with plot3. So, If you have data like:
theta = linspace(0,10*pi,500);
r = ones(size(theta));
z = linspace(-10,10,500);
you need to use pol2cart with 3 arguments to produce this:
[x,y,z] = pol2cart(theta,r,z);
figure(3)
plot3(x,y,z),grid on
Finally, if you have spherical data, you have sph2cart:
theta = linspace(0,2*pi,100);
phi = linspace(-pi/2,pi/2,100);
rho = sin(2*theta - phi);
[x,y,z] = sph2cart(theta, phi, rho);
figure(4)
plot3(x,y,z),grid on
view([-150 70])
That would look this way

What interpolation technique does Matlab plot function use to show the data?

It seems to be very basic question, but I wonder when I plot x values against y values, what interpolation technique is used behind the scene to show me the discrete data as continuous? Consider the following example:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
My guess is it is a Lagrangian interpolation?
No, it's just a linear interpolation. Your example uses a quite long dataset, so you can't tell the difference. Try plotting a short dataset and you'll see it.
MATLAB's plot performs simple linear interpolation. For finer resolution you'd have to supply more sample points or interpolate between the given x values.
For example taking the sinus from the answer of FamousBlueRaincoat, one can just create an x vector with more equidistant values. Note, that the linear interpolated values coincide with the original plot lines, as the original does use linear interpolation as well. Note also, that the x_ip vector does not include (all) of the original points. This is why the do not coincide at point (~0.8, ~0.7).
Code
x = 0:pi/4:2*pi;
y = sin(x);
x_ip = linspace(x(1),x(end),5*numel(x));
y_lin = interp1(x,y,x_ip,'linear');
y_pch = interp1(x,y,x_ip,'pchip');
y_v5c = interp1(x,y,x_ip,'v5cubic');
y_spl = interp1(x,y,x_ip,'spline');
plot(x,y,x_ip,y_lin,x_ip,y_pch,x_ip,y_v5c,x_ip,y_spl,'LineWidth',1.2)
set(gca,'xlim',[pi/5 pi/2],'ylim',[0.5 1],'FontSize',16)
hLeg = legend(...
'No Interpolation','Linear Interpolation',...
'PChip Interpolation','v5cubic Interpolation',...
'Spline Interpolation');
set(hLeg,'Location','south','Fontsize',16);
By the way..this does also apply to mesh and others
[X,Y] = meshgrid(-8:2:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure
mesh(Z)
No, Lagrangian interpolation with 200 equally spaced points would be an incredibly bad idea. (See: Runge's phenomenon).
The plot command simply connects the given (x,y) points by straight lines, in the order given. To see this for yourself, use fewer points:
x = 0:pi/4:2*pi;
y = sin(x);
plot(x,y)

What does TriScatteredInterp in Matlab do?

I am trying to understand the TriScatteredInterp in Matlab.
I followed sample program in the help file.
x = rand(100,1)*4-2;
y = rand(100,1)*4-2;
z = x.*exp(-x.^2-y.^2);
Construct the interpolant:
F = TriScatteredInterp(x,y,z);
What I observe is F.X is same as x and y and F.V is same as z;
ti = -2:.25:2;
[qx,qy] = meshgrid(ti,ti);
qz = F(qx,qy);
I consider linear interpolation is done in the qz = F(qx,qy);. How does it do for the linear interpolation?
Thanks
Now I understand how TriScatteredInterp works in Matlab.
We have x,y,z points for N X 3 dimensions.
All these points, we need to implement Delaunay triangles in C++.
That is easy. Then, for all your desired grid points x', y', please search the triangle in which your x',y' is located. Then do Barycentric interpolation in a triangle as shown in the link. You will get z' for these x',y'. That is all what we need to do in C++ for TriScatteredInterp. Good Luck!

Plotting the result of a 2 parameter function in matlab (3D Graph)

Basically, I have a function f(X,Y) that would return one value for each X,Y that I give. Is there any function in matlab where I can pass the function f, the ranges for X,Y so that it plots a 3d graph showing the magnitude of f (along the z axis) for all values within the given range.
ezplot3, does this kind of, but it takes only one parameter 't'. I am very new to matlab and am trying my best to learn it fast, but I couldnt find much regarding this. Any help would be appreciated
Keep in mind, that with matlab, you're never really plotting "functions"; You're plotting arrays/vectors. So instead of trying to plot g = f(X,Y), you'll actually by plotting the vectors X, Y, and g, where X and Y are your original inputs and g is a vector containing your outputs.
I'm having a hard time visualizing what exactly you're trying to plot but basically, you can follow any standard matlab plotting example such as: http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/plotting.html
It does not produce a 3D plot, but I have found the 2D scatter plot useful for this kind of task before:
scatter(x, y, 5, z)
Where z is the value of the function at the point (x, y) will produce something similar to what you want. Its perhaps not quite as pretty as a full 3D plot but can be used to good effect.
See:
http://www.mathworks.com/matlabcentral/fileexchange/35287-matlab-plot-gallery-scatter-plot-2d/content/html/Scatter_Plot_2D.html
Here is some (very ugly) code I put together to demonstrate the difference:
j=1;
y = -100:1:100;
for i = -100:1:100
y = [y -100:1:100];
count = 0;
while count < 202;
x(j) = i;
j = j+1;
count = count + 1;
end
end
z = (abs(x) + abs(y));
figure(1)
scatter(x, y, 10, z)
h=colorbar;
figure(2)
ezsurf('(abs(x) + abs(y))')
Well, this is what I was going for : http://www.mathworks.com/help/matlab/ref/ezsurf.html
if i do this
ezsurf('f(x,y)');
I get the 3d graph I wanted.
Thanks anyways!