Plotting x,y,z with intervals - matlab

How do I plot xyz In rectangular, polar, and 3-D?
for x = 0 to 35pi:
Y = x*sin(x)
Z = x*cos(x)
Using the the intervals of X which provides very smooth plots . Create three plots including tittle and labels .
This is the input I have put in so far. I'm not sure if this is correct:
x = pi*linspace(0,35);
y = pi*x,sin(pi*x);
z = pi*x,cos(pi*x);
plot3(x,y,z)
title('data analysis')
xlabel('independent x')
ylabel('dependent y')
zlabel('z')

I believe this solves the problem as you describe it:
x = linspace(0, 35*pi, 10000);
y = x .* sin(x);
z = x .* cos(x);
plot3(x, y, z);
title('data analysis');
xlabel('independent x');
ylabel('dependent y');
zlabel('z');

Related

How can I plot a surface generated by a function f(x,y) where the values x and y are subject to constraints?

I'm new to Matlab and I have a function
and
How can I plot:
Define your X and Y in a linear array
X = linspace(-2, 2, 1000);
Y = linspace(-2, 2, 1000);
Mesh them so you have a grid of x and y
[x,y] = meshgrid(X,Y);
Get the value for your function
f = sqrt(x.^2 + y.^2);
Define your domain
D = (1 <= x.^2 + y.^2);
Set everything outside your domain to nan's so it won't plot
f(~D) = nan;
Plot the surface
surf(x,y,f, 'linestyle', 'none')

How to plot spheres on top of scattered stars in MATLAB?

I would like to make a scattered stars and plot multiple spheres on top of it? I tried doing this:
x = rand(100,1); y = rand(100,1); z = rand(100,1);
scatter(x,y,z,'c*')
After this I tried plotting sphere but the sphere pushes the stars to the side. How do I fix this?
vec = [1;1;1]; rads = 1;
[x y z] = sphere;
x = rads*x+vec(1); y = rads*y+vec(2);
z = rads*z+vec(3);
surf(x, y, z, 'Edgecolor', 'none')
colormap colorcube
As you can see it pushed the stars to the side
Thank you.
Its pushing the scatter plot to the side because the range of the 'stars' is [0 1] because the command rand(...) generates values between 0 and 1. On the other hand, the sphere goes from 0 to 2 in all 3 directions.
To fix the problem, you can simply multiply the data used to generate the scatter plot by 2, so they will be in the range [0 2].
Doing so results in the following:
And the code. Note that I used scatter3 instead of scatter.
clear
clc
close all
xs = 2*rand(100,1); ys = 2*rand(100,1); zs = 2*rand(100,1);
hScatter = scatter3(xs,ys,zs,'c*')
hold on
vec = [1;1;1]; rads = 1;
[x y z] = sphere;
x = rads*x+vec(1); y = rads*y+vec(2);
z = rads*z+vec(3);
surf(x, y, z, 'Edgecolor', 'none')
colormap colorcube
rotate3d on

fill function in MATLAB

I'm having issues unterstanding the function fill in MATLAB , I have a PSD of a file the I want to change it background like :
[xPSD,f] = pwelch(x,hanning(4096),2048,4096*2 ,fs);
plot(f,10*log10(xPSD));
x= f(100:150);
y= 10*log10(xPSD(100:150))
fill(x,y,'y')
the result is in the right direction but not what I need :
I would like get the color tell x axis like :
is their a way to do this
A working solution is:
[xPSD,f] = pwelch(x,hanning(4096),2048,4096*2 ,fs);
plot(f,10*log10(xPSD));
hold on
x= f(100:150);
y= 10*log10(xPSD(100:150));
yMax = ylim;
yMax = yMax(2);
x = x'; % Use this line only in the case that the size(x, 1) > 1
X = [x fliplr(x)];
Y = [y' ones(1, length(y)) .* yMax];
fill(X, Y, 'y')
What you were missing is that fill method looks for an area to fill. In the above code the area is defined by pairs of points. That, for the first (i.e. lower part) of the area we have the x vector and the y points. The second area (i.e. the upper part) is defined by the reversed vector x (image your pencil to first starting drawing towards rights for the lower part and then for the upper going left) and the points of the upper limit of your axes.
Edit:
Minimal example with the handel data from MATLAB:
load handel;
x = y; % Just to be consistent with the OP
fs = Fs; % Just to be consistent with the OP
[xPSD,f] = pwelch(x,hanning(4096),2048,4096*2 ,fs);
plot(f,10*log10(xPSD));
hold on
x= f(100:150);
y= 10*log10(xPSD(100:150));
yMax = ylim;
yMax = yMax(2);
x = x'; % Use this line only in the case that the size(x, 1) > 1
X = [x fliplr(x)];
Y = [y' ones(1, length(y)) .* yMax];
fill(X, Y, 'y')
xlim([0 200]) % To focus on the result
The result is:
Yes, there is always a way ;)
In your case, you simply need to add two points in x and y that go to the top boudary of the plot:
x = f(100:150);
y = 10*log10(xPSD(100:150))
% Add two points
Y = ylim;
x = [x(1) ; x(:) ; x(end)];
y = [Y(2) ; y(:) ; Y(2)];
% Display filled area
fill(x,y,'y')
Best,

How can I plot a function with two variables in Octave or Matlab?

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)

Matlab: 2D projection of a 3D function

I have a function f(x,y)= Exp(-x^2-y^-2)(x^2+y^2). I would like to look at the projection of this function onto the x-axis in MATLAB.
Any thoughts on the best way to do this?
something like this:
xs = [];
ys = [];
zs = [];
for x = -10:0.1:10
for y = -10:0.1:10
xs = [xs x];
ys = [ys y];
z = f(x,y);
zs = [zs z];
end
end
figure; plot3(xs,ys,zs); %plots the full function over both dimensions
figure; plot(xs,zs,'rx'); %plots the projection onto the x axis
figure; plot(ys,zs,'rx'); %plots the projection onto the y axis
that does it over the range -10 to 10 along both x and y but you can change that accordingly.
#Amro has a great solution, but you might also take a look at Scott Hirsch's awesome shadowplot from the MATLAB Central File Exchange. Check it out:
>> f = #(x,y) exp(-x.^2 -y.^(-2)).*(x.^2+y.^2);
>> [X,Y] = meshgrid(-10:0.5:10,-10:0.5:10);
>> surf(X,Y,f(X,Y))
>> xlim([-11,11])
>> ylim([-11,11])
>> shadowplot x
>> shadowplot y
You can manipulate the view to see the 2D-projection on the x-axis:
f = #(x,y) exp(-x.^2 -y.^(-2)).*(x.^2+y.^2);
[X,Y] = meshgrid(-10:0.5:10,-10:0.5:10);
surf(X,Y,f(X,Y))
view(90,0), shading interp
xlabel X, ylabel Y, zlabel Z