Plotting a 3D Function in Matlab: Visualizing the Evolution of a System

  • Context: MATLAB 
  • Thread starter Thread starter qspeechc
  • Start date Start date
  • Tags Tags
    3d Matlab Plot
Click For Summary

Discussion Overview

The discussion revolves around plotting a 3D function in MATLAB to visualize the evolution of a system represented by a quantity w, which is a function of time t and space x. Participants explore various methods for creating 3D plots, including surface plots, arrow representations, and animations, while addressing challenges in visualizing wave interactions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach to plotting w as a 3D plot using the plot3 function but encounters issues, prompting a request for guidance.
  • Another participant suggests using surf() or contour() for representing z(x, y) instead of plot3, which is meant for discrete points.
  • A different viewpoint proposes using 3D arrows to represent the magnitude and direction of w, indicating a method for visualizing fluid flow.
  • One participant mentions using mesh plots to show wave interactions but notes limitations in visualizing wave shapes effectively.
  • A suggestion is made for animating the motion using a 3D array and surf, detailing a script that updates the plot over time.
  • Another participant reiterates the arrow representation idea and mentions the quiver plot command as a potential solution, although they express uncertainty about its effectiveness.
  • A new participant seeks assistance in creating a scatter plot with connected lines to represent z values, indicating a different approach to 3D visualization.

Areas of Agreement / Disagreement

Participants express multiple competing views on the best methods for visualizing the data in 3D. There is no consensus on a single approach, as various techniques are proposed and discussed.

Contextual Notes

Some participants mention specific limitations in their methods, such as the need for suitable scaling in arrow representations and the challenges of visualizing wave shapes with mesh plots. Additionally, there are unresolved questions about the effectiveness of certain MATLAB commands like quiver.

Who May Find This Useful

This discussion may be useful for MATLAB users interested in 3D plotting techniques, particularly those working with time-dependent functions or wave interactions in physics and engineering contexts.

qspeechc
Messages
839
Reaction score
15
Hi everyone.

So, I have a quantity I am computing, say w, a vector. Now w is a function of time t and space x. I have the vectors x and t. At each time step in a loop, I calculate w for that time step. How do I plot w as a 3D plot, as a function of time t and space x, so I can see the evolution of the system?

Here's what I tried. I created a matrix A of dimension NxM, where N is the length of the vector x, and M the length of the vector t. At the mth time step I placed the computed value of w in the mth column of A. Then I tried to plot it like this:

>> plot3(t,x,A)

but that doesn't work, instead I tried t.*x, x.*t in all combinations, but nothing worked. What is the correct way to do it?
Thanks.
 
Physics news on Phys.org
I think you want surf() or contour()... plot3 is for plotting (x, y, z) points in 3-space, not for showing z(x, y).

-Kerry
 
I think it would help to picture how you'd like to represent this information first, and then try to figure out how to do it in Matlab.

One possibility would be to plot 3-D arrows, one at each (t;x,y,z), such that the length of the arrow was proportional to the magnitude of w (you'll have to choose a scale that looks good), and the direction is given by the direction of w. You could do this by adding the vector w, multiplied by a suitable scaling factor, to each (x,y,z) vector, to get the location of the "head" of the arrow. If you then plot a line from (x,y,z) to this new point, that will represent the vector w. I think you can even put an arrowhead on it, but I don't recall how to do that from the command line or a script.

This would give you something like those plots that show fluid flow by displaying an array of arrows that indicate the velocity of the fluid at each point on a grid.
 
I am actually supposed to be showing the interaction of a bunch of waves. I tried mesh and it gives an ok result if I pick a good viewing angle, I can see the direction of the waves, but not really their shapes. I guess it'll do. Thanks.
 
If you want to do an animation of your motion, here's a suggestion. In the following script I am plotting from a 3-D array 'U'. The third dimension corresponds to time. Surf plots a surface. Xi and Yi are meshes produced using meshgrid(x,y). I fix my axes and loop over time.

h1=surf(Xi,Yi,U(:,:,1),'erasemode','normal');

shading interp;
%colormap(gray);
colorbar;
axis([0 3 0 3 -1 1]);
caxis([-.25 .25]);

for ii = 2:M
set(h1,'zdata',U(:,:,ii));
refreshdata;
drawnow;
% pause(0.01);
% pause(0.3);
end
 
belliott4488 said:
I think it would help to picture how you'd like to represent this information first, and then try to figure out how to do it in Matlab.

One possibility would be to plot 3-D arrows, one at each (t;x,y,z), such that the length of the arrow was proportional to the magnitude of w (you'll have to choose a scale that looks good), and the direction is given by the direction of w. You could do this by adding the vector w, multiplied by a suitable scaling factor, to each (x,y,z) vector, to get the location of the "head" of the arrow. If you then plot a line from (x,y,z) to this new point, that will represent the vector w. I think you can even put an arrowhead on it, but I don't recall how to do that from the command line or a script.

This would give you something like those plots that show fluid flow by displaying an array of arrows that indicate the velocity of the fluid at each point on a grid.

You can also do this with the 'quiver' plot command. I have never have success using it, but I have seen other people have success so I can only assume it's possible. Hit up the ol' help quiver and see what you can come up with.
 
Hello:

I would like to generate a similar 3D graph in MATLAB. The points are (x,y,z). I would like to have a SCATTER plot on the (x,y) axes. But then each one of these (x,y) points are LINE connected to their respective z point. If anyone has code for this, or know how to do this in SAS, it would be so helpful. Thx!
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K