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:
Related
I made two 3D plots on the same axis. now I desire to give them different colors for easy identification. How do I do this coloring? The MATLAB code is shown below.
tic
Nx = 50;
Ny = 50;
x = linspace(0,1,Nx);
y = linspace(0,0.5,Ny);
[X,Y] = meshgrid(x,y);
[M,N] = size(X);
for m=1:M
for n=1:N
%get x,y coordinate
x_mn = X(m,n);
y_mn = Y(m,n);
%%% X=D2 and Y=D1
%Check if x_mn and y_mn satisfy requirement
if(x_mn >= y_mn)
%evaluate function 1
Z(m,n) = (x_mn^2 - 2*x_mn*y_mn + y_mn^2);
Z_1(m,n) = (x_mn^2);
elseif(x_mn < y_mn)
%evaluate function 2
Z(m,n) = 0;
Z_1(m,n) = (x_mn^2);
%% Z(m,n) = 2*(2*x_mn*y_mn + y_mn - y_mn^2 - 2*x_mn);
else
Z(m,n) = 0;
end
end
end
%Plot the surface
figure
surf(X,Y,Z) %first plot
surfc(X,Y,Z)
hold on
surf(X,Y,Z_1) %second plot
xlabel('Dm');
ylabel('D');
zlabel('pR');
grid on
shading interp
toc
disp('DONE!')
How can I create two differently colored surfaces?
figure
surf(X,Y,Z) %first plot
surfc(X,Y,Z)
hold on
surf(X,Y,Z_1)
Your surfc() call actually overwrites your surf() call, is this intended?
As to your colour: the documentation is a marvellous thing:
surfc(X,Y,Z,C) additionally specifies the surface color.
In other words: just specify the colour as you want it. C needs to be a matrix of size(Z) with the desired colours, i.e. set all of them equal to create an monocoloured surface:
x = 1:100;
y = 1:100;
z = rand(100);
figure;
surfc(x,y,z,ones(size(z)))
hold on
surfc(x,y,z+6,ones(size(z))+4)
Results in (MATLAB R2007b, but the syntax is the same nowadays)
I'm trying to practice curve fitting on a 2D Gaussian, but in order to do that I need to add random noise to my predefined Gaussian. My first instinct was to cycle through two for loops and create two matrices X and Y with random numbers, but when I tried that (I don't have the code anymore) Matlab wouldn't let me plot the Gaussian because I didn't generate my X and Y values using the meshgrid function. Since I seem to need to use meshgrid, can anyone help me figure out how to generate a random meshgrid so I can add some noise to my Gaussian?
amp = 1;
x0 = 0;
y0 = 0;
sigmaX = 1;
sigmaY = 1;
%X = 1:1:100;
%Y = 1:1:100;
[X,Y] = meshgrid(-3:.1:3);
%Z = X .* exp(-X.^2 - Y.^2);
Z = amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)));
surf(X, Y, Z);
%Add noise now
EDIT: So I found out that rand can return a random matrix which will work with the surf function (for some reason it wasn't working for me earlier though). The result looks something like this: noisy 2D gaussian
amp = 1;
x0 = 0;
y0 = 0;
sigmaX = 1;
sigmaY = 1;
[X,Y] = meshgrid(-3:.1:3);
%Z = X .* exp(-X.^2 - Y.^2);
Z = amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)));
surf(X, Y, Z);
%Make some noise
[xRows, xColumns] = size(X);
[yRows, yColumns] = size(Y);
figure(2)
X = -.1 + (.1+.1)*rand(61,61);
Y = -.1 + (.1+.1)*rand(61,61);
Z = amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)));
surf(X, Y, Z)
But I feel like the Gaussian has largely lost it's typical bell shape and looks more like a slope field than anything. I'm going to try and refine it but I would love any input.
That's what i would do.
amp=1;
x0=0;
y0=0;
sigmaX=1;
sigmaY=1;
noiseAmp=.1;
x=[-2:.1:2];
y=[-2:.1:2];
%Create two Noise Vectors
noisez1=noiseAmp.*rand(1,length(x));
noisez2=noiseAmp.*rand(1,length(x));
% Make an meshgrid out of the two Vectors
[noiseZ1,noiseZ2]=meshgrid(noisez1,noisez2);
% Add the Meshgrids togehter
Noise=noiseZ1+noiseZ2;
[X,Y]=meshgrid(x,y);
% Add the Noise to the result of Z
Z=amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)))+Noise;
surf(X,Y,Z);
if you just want a 2D plot you can try this
amp=1;
noiseAmp=0.01;
x0=0;
y0=0;
sigmaX=1;
sigmaY=1;
x=[-5:.01:5];
noiseY=noiseAmp*rand(1,length(x));
y=noiseY+amp*exp(-((x-x0).^2/(2*sigmaX^2)));
plot(x,y);
where noiseAmp is the Amplitude of the noise.
But if you still want to create a 3D plot with the surf() function, you have to add a random meshgrid to the Z result.
I have plotted a surface from some data. In the same plot I want to have a 3D line (I have the [x,y,z] values for the line I want to plot). When I try to do this using plot3(x,y,z) in the same figure, the line is always below the surface.
Is there any way to fix this? I don't know if this problem appears in Matlab as well.
Minimal example:
figure;
hold all;
y = x = 0:35;
z = ones(1,36).*0.5;
plot3(x,y,z);
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
surf(Z);
The result (the blue line is below the surface):
To answer part of your question, you don't get this problem in MATLAB with the following code:
figure;
hold all;
x = 0:35;
y = x;
z = ones(1,36).*0.5;
plot3(x,y,z);
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
surf(Z);
I also had this problem with surf in Octave, so I used the mesh function instead. It is not as pretty, and has different parameters but does allow lines to overlay it:
I created that with the same code as above but replacing surf with:
mesh ((X+8)*2.2, (Y+8)*2.2, Z);
Because mesh needed its parameters to be scaled up. The result is roughly the same.
I want to use Octave to plot fairly simple functions with two variables like: f(x,y) = x^2 + 3y . It is very easy to plot single variable functions, but I am having a lot of trouble finding out how to do two variable functions. Does anyone know the best way of doing this?
Plotting a function of two variables would normally mean a 3-dimensional plot - in MATLAB you would use the function plot3 for that. To plot your function f(x,y) in the interval [-10,10] for both X and Y, you could use the following commands:
x = [-10:.1:10];
y = [-10:.1:10];
plot3(x, y, x.^2 + 3*y)
grid on
In case it may help someone out there... I ran in Octave the code in the accepted answer and I got this plot:
But I really wanted the function for every point in the Cartesian product of x and y, not just along the diagonal, so I used the function mesh to get this 3D plot with the projected contour lines in the x,y plane:
x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;
mesh(x, y, z)
meshc(xx,yy,z)
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
grid on
To get rid of the mesh-wire texture of the plot, the function surf did the trick:
x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;
h = surf(xx,yy,z);
colormap hsv;
set(h,'linestyle','none');
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
Another way to plot is as a heatmap with contour lines:
x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + yy.*3;
contourf(xx,yy,z);
colormap hsv;
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
grid on
And for completeness, the levels can be labeled:
x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;
[C,h] = contour(xx,yy,z);
clabel(C,h)
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
grid on
In addition to the excellent answers from #Toni and #esskov, for future plotters of functions with two variables, the contour and contourf functions are useful for some applications.
MATLAB Code (2018b):
x = [-10:.1:10];
y = [-20:.1:20];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy; % Borrowed 4 lines from #Toni
figure
s(1) = subplot(1,2,1), hold on % Left Plot
[M,c] = contour(xx,yy,z); % Contour Plot
c.ShowText = 'on'; % Label Contours
c.LineWidth = 1.2; % Contour Line Width
xlabel('X')
ylabel('Y')
box on
s(2) = subplot(1,2,2), hold on % Right Plot
[M2,c2] = contourf(xx,yy,z);
colorbar % Add Colorbar
xlabel('X')
ylabel('Y')
box on
title(s(1),'Contour Plot')
title(s(2),'Filled Contour Plot')
Update: Added example of surfc
h = surfc(xx,yy,z)
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)