Plotting a multivariable function on a 2-D graph
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I'm trying to plot a multivariable function on a 2D grid, using loops.
This is what I have so far, but I cannot get the plot to work correctly.
tau=4.05;
t=0:tau/20:tau;
z=0:1/20:6;
[zz , tt]=meshgrid(z,t);
T=exp(-zz).*cos((2*10^-6.*tt)-zz);
for i=1:121;
plot(T(:,i),z(:,1))
hold on
end
The axis should be z versus T, so the loop is to plot z against T with all the versions of t.
It should look like this (I attached the png):
2 commentaires
Réponses (1)
Cris LaPierre
le 2 Fév 2019
Modifié(e) : Cris LaPierre
le 2 Fév 2019
You are close, but there are some errors in your code logic. After giving myself a crash course in Geodynamics, I understand how to modify your code to create the plot. Let me explain.
The plot command
It is plotting something. Add a symbol and you'll see it: plot(T(:,i),z(:,1),'-d'). However, it's still not correct, so what is going on?
- z(:,1) = 0. Every x value is plotted with a y-value of 0. Perhaps you meant to use zz(:,i)?
- Even with that, you still need to include symbols to see the plot. Now what? Your code produces 21 series (lines) - each of which has 121 data points - and places them in rows. But you are plotting columns - all 121 of them, which means there will be 121 horizontal lines in your plot, one at each point in z (you have to zoom way in to see them). Plot the 21 series instead: plot(T(i,:),zz(i,:)) (don't forget to adjust the loop counter, too).
- Maybe now is a good time to point out your y-axis is the reverse of what you want. Below I show you how to reverse it.
Recommendations:
- MATLAB will automatically treat columns as series, so best to arrange your data that way. It will mean you do not have to use a for loop to plot.
[tt,zz]=meshgrid(t,z);
T=exp(-zz).*cos((2*10^-6.*tt)-zz);
plot(T,zz)
ax = gca;
ax.YDir = 'reverse';
Getting lines to spread out
With those corrections, you'll see a line, but just one. Zoom in and you'll see there are 21. They are just all practially the same. The reference plot has them distributed. How? This is the part that required the slides.
- The cos term in your equation is what determines the cycle to display. It will return values from -1 to 1. In order to get lines evenly spaced over the entire cycle, you need cos to return values evenly spaced between -1 and 1. Note that the x-axis on your reference plot goes from -1 to 1. Convenient.
- The value you are using for τ is way too small to achieve this. You use ω=2E-6 so your τ needs to be 2*pi/w; in order to get one complete cycle, or 3.1416e+06.
- Incidentally, if you are going for an annual cylce (common for soil temps at least), it seems ω should be 2e-7.
w = 2e-7;
tau = 2*pi/w;
t=0:tau/20:tau;
Put it all together
Putting it all together, I can recreate the plot with the following code
w = 1.99E-7; % Frequency - Annual
tau = 2*pi/w;
t=0:tau/20:tau;
z=0:1/10:6; % Use a different step so I can tell which dimension is what
[tt,zz]=meshgrid(t,z);
T=exp(-zz).*cos(w*tt-zz);
plot(T,zz,'k')
ax = gca;
ax.YDir = 'reverse';

Voir également
Catégories
En savoir plus sur Graphics Performance dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!