Plotting a 3D Function - matlab

Im trying to plot the following function:
F(x,y)=Cos(x)*Sin(y)
With the following features: Labeled all three axes, the x and y axes range from 0 to 2pi with 0.1 spacing, and the viewing angles are orthogonal to any x/y/z=0 plane.
I have the following code
t= 0:0.1:2*pi;
A=cos(t);
B=sin(t);
for i=1:numel(t)
C(:,i)=A(i).*B(:);
end
surf(C);
xlabel('x axis','fontsize',12)
ylabel('y axis','fontsize',12)
zlabel('z axis','fontsize',12)
title ('3D Plot')
That is almost what I need, for some reason the chart is plotting x and y from 0 to 80, and Im not sure why. I there another way I can try plotting this so that I can use different variables for the cosine and sine inputs?:

I think you wanna do
surf(t,t,C)
instead of only surf(C). Now it will range from 0 to 2pi. You need to define the x- and y-values if you want them to have specific values.
You might want to use this as well, to remove the unnecessary parts of the axises.
axis tight

Related

MATLAB - Plotting a smooth volume from 3D scatter plot data

So I have data in the form [x y z intensity] that I plot on a scatter3 figure with xyz axes. The colour of the data is used to dictate the intensity value. Problem is, using a scatter plot means the data points show up as discrete points. What I need, is a smooth shape - so I guess I need some kind of interpolation between the points?
I've tried using trisurf, but the problem with this one is that it interpolates between points that it shouldn't. So where I should have 'gaps' in my surface, it joins up the edges instead so it fills in the gaps. See the attached pics for clarification.
Does anyone have any suggestions?
The code I use is as below (the commented out scatter3 is what does the scatter plot, the rest does the trisurf):
% Read in data
dataM = csvread('3dDispersion.csv');
% scatter3(dataM(:,1), dataM(:,2), dataM(:,3), 5, dataM(:,4),'filled');
% Plot
hold on;
x = dataM(:,1);
y = dataM(:,2);
freq = dataM(:,3);
tri = delaunay(x,y);
h = trisurf(tri, x, y, freq);
% Make it pretty
% view(-45,30);
view(3);
axis vis3d;
lighting phong;
shading interp;
Use the boundary function in Matlab. This will apply a mesh similar to shrinkwrap over your points. In order to reduce the "gap closers", you will want to increase the "shrink factor".
Try K = boundary(X,Y,Z,0.9)
Where X, Y & Z are the vectors of your data points
https://www.mathworks.com/help/matlab/ref/boundary.html
You can then use trimesh or related surface plotting functions depending on how you want to display it.

How to Adjust y axis plot range in Matlab?

I need to plot the following functions in matlab
y1=sign(x)
y2=tanh(x)
y3=(x)/(x+1)
The x-range is -5,5 with 0.1 spacing
The y-plot range should be between -1.5 to 1.5.
Each plot should have a labeled x and y axis and a legend in the lower right corner.
The only things I cant figure out is how to adjust the y plot range. Ive tried editing the actual figure but all that seems to do is distort the graph.
Is there a command within matlab that will let me adjust the y axis plot range?
The other thing I havent figured out yet is adding a legend, I can do it after the figure is created but I guess it needs to be done by matlab command.
Yes, use axis after the plot command:
axis([-5 5 -1.5 1.5])
If you only want to set the y-range without setting the x-range you can use
ylim([-1.5 1.5])
or alternatively axis([-inf inf -1.5 1.5]). I found this from the original MATLAB-source: https://de.mathworks.com/help/matlab/ref/ylim.html
PS: For trigonometric functions I would recommend to use axis equal to have equally spaced x and y-axis (see MATLAB)

Plotting points while plotting vectors : Matlab

I need to make a plot with only points and tried something like
plot(x,y)
where x and y are vectors: collection of points.
I do not want matlab to connect these points itself. I want to plot as if plotted with
for loop
plot;hold on;
end
I tried
plot(x,y,'.');
But this gave me too thick points.
I do not want to use forloop because it is time expensive. It takes a lot of time.
You're almost there, just change the MarkerSize property:
plot(x,y,'.','MarkerSize',1)
Try:
plot(x,y,'*');
or
plot(x,y,'+');
You can take a look to the documentation: http://www.mathworks.nl/help/matlab/creating_plots/using-high-level-plotting-functions.html
help scatter
IIRC: where S is the size of the scatter points:
scatter(x,y,S)
You may try this piece of code that avoid using loops. The plot created does not have lines but markers of different colors corresponding to each column of matrices x and y.
%some data (matrix)
x = repmat((2:10)',1,6);
y = bsxfun(#times, x, 1:6);
set(0,'DefaultAxesColorOrder', jet(6)); %set the default matlab color
figure('Color','w');
plot(x,y,'p'); %single call to plot
axis([1 11 0 70]);
box off;
legend(('a':'f')');
This gives

Matlab - Continuous Plot and Semilogx on the same figure

I am trying to plot on the same figure the evolution of a function f, with argument x in ]0,1]. I would like to see both the evolution of f far away from 0 and close to 0, on the same figure, with one x axis.
For now I only have two different figures, one using plot with x=[0.1 ... 1], and one using semilogx with x=[1e-9 1e-7 1e-5... 0.1]. I would like to have both graphs on the same figure, with the x axis being logarithmic at the beginning, then linear after a certain x0 (let's say x0=0.1).
I do not want something using plotxx since I want only one x axis.
Do you know if this is possible?
Thank you for your time and help.
Just plot your y vector without specifying the x vector, this will get you a uniformly spaced plot, then use XTick and XTickLabel to make it work. For example,
x1=logspace(-10,-1,10);
x2=linspace(1,10,10);
y1=x1.^0.25;
y2=1./x2;
plot([y1 y2],'-x')
xlabels=num2cell([x1 x2]);
set(gca,'XTick',1:numel(x1)+numel(x2),'XTickLabel',xlabels)
If you want to use Latex to format tick labels, you'll need to download a function from the Matlab File Exchange.

Fixing the Radial Axis on MATLAB Polar Plots

I'm using polar plots (POLAR(THETA,RHO)) in MATLAB.
Is there an easy way to fix the range for the radial axis to say, 1.5?
I'm looking for something analogous to the xlim, ylim commands for cartesian axes. Haven't found anything in the docs yet.
this worked for me... i wanted the radius range to go to 30, so i first plotted this
polar(0,30,'-k')
hold on
and then plotted what i was actually interested in. this first plotted point is hidden behind the grid marks. just make sure to include
hold off
after your final plotting command.
Here's how I was able to do it.
The MATLAB polar plot (if you look at the Handle Graphics options available) does not have anything like xlim or ylim. However, I realized that the first thing plotted sets the range, so I was able to plot a function with radius range [-.5 .5] on a [-1 1] plot as follows:
theta = linspace(0,2*pi,100);
r = sin(2*theta) .* cos(2*theta);
r_max = 1;
h_fake = polar(theta,r_max*ones(size(theta)));
hold on;
h = polar(theta, r);
set(h_fake, 'Visible', 'Off');
That doesn't look very good and hopefully there's a better way to do it, but it works.
Simple solution is to make a fake graph and set its color to white.
fake=100;
polar(0,fake,'w');
hold on;
real=10;
polar(0,real,'w');
In case anyone else comes across this, here's the solution:
As Scottie T and gnovice pointed out, Matlab basically uses the polar function as an interface for standard plots, but with alot of formatting behind the scenes to make it look polar. Look at the values of the XLim and YLim properties of a polar plot and you'll notice that they are literally the x and y limits of your plot in Cartesian coordinates. So, to set a radius limit, use xlim and ylim, or axis, and be smart about the values you set:
rlim = 10;
axis([-1 1 -1 1]*rlim);
...that's all there is to it. Happy Matlabbing :)