Problems animating a surf plot.

2 views (last 30 days)
Argento
Argento on 12 Mar 2016
Commented: Argento on 12 Mar 2016
Hello all, I am employing MATLAB to solve some basic PDEs (transport equation) using various numerical schemes. I have no problem creating a 2-d movie of the solution over time. However, when I try the same trick with surf, I either just get the plot of the final frame, or if I try to index it, I get the error:
Error using surf (line 69) Z must be a matrix, not a scalar or vector.
How can I fix this? Below I've attached my code, I appreciate all the help.
W.U.
  1 Comment
Image Analyst
Image Analyst on 12 Mar 2016
Edited: Image Analyst on 12 Mar 2016
Note: REQUIRES SYMBOLIC TOOLBOX (so I can't run it). I've added that to the product list.

Sign in to comment.

Accepted Answer

Ced
Ced on 12 Mar 2016
Edited: Ced on 12 Mar 2016
The code runs without error. I was not quite sure what you were trying to plot in 3D. I mean, you have one nx x nt matrix. If you index it, you have a vector... how should surf plot that?
My best guess is that you want to do something like this:
U_movie = zeros(size(U_t_x));
U_movie(:,1) = U_t_x(:,1);
h = surf(U_movie);
xlim([0 100])
ylim([0 201 ])
view(74, 34)
title(mytitle)
grid on
M(1) = getframe;
for i=2:Tsteps
U_movie(:,i) = U_t_x(:,i);
set(h,'ZData',U_movie);
M(i) = getframe;
pause(0.1)
end
There might be a more elegant way of doing this, but it works :).
I don't think the angle is so good, I would also only plot a subset of the points to make the edges less prominent, but I've kept these settings as you had them for now.
Note the usage of the "set" function. It's usually a good practice when updating the data in an otherwise constant plot (instead of recreating the figure over and over), and will speed up the plotting process significantly.
Hope this helps.
  1 Comment
Argento
Argento on 12 Mar 2016
Thanks Ced, that does it. I was trying to do exactly what you did but could not figure out who to get surf to plot at every time step. Thanks a lot for the help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!