How does MATLAB's normpdf function work? - matlab

When trying to plot a normal PDF with mean=0 and standard deviation=20 using the MATLAB command normpdf() I get weird results, see picture.
The code used to plot the figure is as follows:
plot(normpdf((-100:0.1:100),0,20))
What is the correct way of using this function?

When you call plot with ONE argument, it plots those numbers on the y axis, using the index numbers of those values for the x axis. If you wanted the x axis scaled properly, you had to provide them in the first place. Thus...
x = -100:0.1:100;
plot(x,normpdf(x,0,20),'-')

I assume you expected the x-axis to be centered at 0? You need to specify an x-vector for plot. Try plot([-100:0.1:100], normpdf((-100:0.1:100),0,20));.

Related

Matlab date help on x-axis

I have a porkchop plot that looks similar to this:
The contour function used to make it has input arguments for the x and y positions that are serial dates (as that seemed to be required by MATLAB). Then I used the following command to get the format I want:
datetick('x', 2); datetick('y', 2);
The problem I am having is that when I zoom in on the plot the tick labels to not autogenerate and I can be left with no ticks on the x or y axis if I zoom in to use a weeks' date range for example.
I tried enabling 'auto' for XtickMode and YtickMode but when I zoom in or pan after using those commands the relationship between the independent and dependent variables are lost for some reason (aka the dates don't stay with data like they do when you just have numbers on the x axis and zoom in).
Any ideas on how to solve this issue to get the functionality I'm looking for?
I have also tried the command xtickformat('dd-MMM-yy') but I get an error "Invalid numeric tick label format." when I use it with the contour plot.
As far as I know there is no builtin method in MATLAB to do this. I use the datetickzoom function from the MATLAB FileExchange. If you replace all instances of datetick with datetickzoom, it will automatically update the relevant axis labels when you zoom in.

Matlab - Plot difference between two graphs in specific points

I have two graphs, one is the exact graph of a solution, the other is a numerical approach. I have 4 specific points in my figure (t=0.25,0.5,0.75,1), where I want to illustrate the difference between the two graphs with a straight line. I found the errorbars function but i don't see any use there. Hope you can help me!
Edit:
this is the example figure:
t = [0:0.25:1];
y = t.*4;
x = t.^2+3;
plot(t,y,t,x)
I have 4 points now, t=0.25; t=0.5; t=0.75; t=1; At this points, I just want a vertical line between the two plots. I already have tried this: plot([t(1),y(1)],[t(1),x(1)])
but it just creates a line over the whole figure.
✶ It seems that you're not using hold on before using plot command the second time because otherwise you'd have got the desired result (which is actually not a correct way of plotting a vertical line).
✶ You're mixing up the values of x and y for plot(x,y). To plot a vertical line, it should be used like this: plot([x,x], [y1,y2])
For your case, you may not notice the difference between plot([t(1),y(1)],[t(1),x(1)]) (which is incorrect) and plot([t(1),t(1)],[x(1),y(1)]) (which is correct) because it is by chance that the values are same. Plot it for some other points and you'll realize the difference.
Fixed Code:
t = [0:0.25:1];
y = t.*4;
x = t.^2+3;
plot(t,y,t,x)
hold on
plot([t(1) t(1)],[x(1) y(1)])
% You have 't' on your horizontal axis and 'x'and 'y' values on the vertical axis
axis equal % just for better visualization
Output:

new to matlab, trying to plot a vector field using quiver

I'm trying to plot a simple vector field but I'm unsure of the syntax for quiver because it doesn't seem to be working. It's just displaying an empty plot. I think it has something to do with the ./ but it won't even run when I remove the period. Thanks
v = -5:0.1:5;
[x,y] = meshgrid(v);
u1=(2./x);
u2=(2.*y./(x.^2));
quiver(x,y,u1,u2);
The problem is that your vector field is infinitely large at x=0, which messes with the auto-scaling of arrows. Try avoiding the coordinate axes, for instance by using v=linspace(-5,5,10);. By using an ever number of points on a symmetric domain, you ensure that x=0 and y=0 are never used.
Result using this v:

MATLAB: Plotting Contours at Specfic Points (x,y)

I'm currently producing a contour plot using
contour(x,y,z)
However, I would like to specify some additional contour lines to the ones provided.
I understand that I can use contour(x,y,z,v) where v is some vector containing values of the contour levels I would like but I don't really want to use this since I don't know exactly the levels.
Instead is it possible to plot the contour that goes through a specific point (x,y)?
Thanks.
You can overplot a second contour with a single, specific value for the contour, optionally specifying parameters like line width to make it obvious:
contour(x,y,z)
hold on
lev = z(n,m); % find the value you want in z
contour(x,y,z,lev,'Linewidth',2);

How to make a log plot in matlab

Is it possible to make a plot in matlab that does not actually take the logs of the values? I'm plotting wide ranges of values and when I try to make a log plot of them, those below 1 become negative. I would just like it to plot the values on a log scale without taking their logs.
Alternatively, set(gca,'XScale','log') if you have your plot already.
Yes, it is possible. Use the loglog command.
The example from the Mathworks website:
x = logspace(-1,2); % generate a sequence of points equally spaced logarithmically
loglog(x,exp(x),'-s')
grid on
If you do not want both axes to be log scale, use semilogx or semilogy.
So, you want to plot liner data on logarithmic axes? You can exponentiate you values before using the log plot. This way the point p=(10,3) will plot at the x=10 position.