# Animation help needed with

6 visualizzazioni (ultimi 30 giorni)
Tom il 18 Gen 2012
Commentato: David il 25 Ott 2013
Animation help needed
I'm trying to display 10 graphs in a sequence as an animation, running from t=1 to t=10. Here's my code,
close all;
clear all;
l=0.33;R=100;T=68;m=0.000125;w0=0.1;t=0;mu=m/l;c=sqrt(T/mu);r=1/2;x0=l*r;
for t = 1:10
for x=1:34;
for n=1:5000;
wxt=((4*w0)/pi)*exp(-R*(t*0.001))*((l/(pi*n^2*x0))...
*sin((n*pi*x0)/l))*sin((n*pi*((x-1)*0.01))/l)*cos((sqrt((c*n*pi/l)^2-R^2)*(t*0.001)));
wxtrec(n)=wxt;
end
w=sum(wxtrec);
wrec(x)=w;
end
end
plot(1:34,wrec(t+10))
axis equal
M(t) = getframe
%plot(1:34,wrec)
The bottom plot works by its self, displaying the graph of t=10, but I can't seem to make the animation work.
Any help would be really appreciated.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Chandra Kurniawan il 18 Gen 2012
l =0.33;R=100;T=68;m=0.000125;w0=0.1;t=0;mu=m/l;c=sqrt(T/mu);r=1/2;x0=l*r;
for t = 1:10
for x=1:34;
for n=1:5000;
wxt=((4*w0)/pi)*exp(-R*(t*0.001))*((l/(pi*n^2*x0))...
*sin((n*pi*x0)/l))*sin((n*pi*((x-1)*0.01))/l)*cos((sqrt((c*n*pi/l)^2-R^2)*(t*0.001)));
wxtrec(n)=wxt;
end
w=sum(wxtrec);
wrec(x)=w;
end
plot(1:34,wrec)
M(t) = getframe
end
%plot(1:34,wrec(t+10))
%axis equal
%M(t) = getframe
%plot(1:34,wrec)
movie(M)
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (4)

Tom il 18 Gen 2012
Brilliant! That's really good thank you loads. One last thing - is there a way to stop the vertical axis from automatically changing its scale for each graph? I just tried and all I did was just stop the animation from working.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Chandra Kurniawan il 18 Gen 2012
Hi,
You can write :
axis ([0 35 -0.05 0.05]);
before line
plot(1:34,wrec)
inside the loop.
Or you can also set the axis unvisible inside the loop by
axis off

Accedi per commentare.

Tom il 18 Gen 2012
Actually - it did work I just couldn't see it properly. Many thanks again.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Chandra Kurniawan il 18 Gen 2012
l=0.33;R=100;T=68;m=0.000125;w0=0.1;t=0;mu=m/l;c=sqrt(T/mu);r=1/2;x0=l*r;
axis equal
for t = 1:10
for x=1:34;
for n=1:5000;
wxt=((4*w0)/pi)*exp(-R*(t*0.001))*((l/(pi*n^2*x0))...
*sin((n*pi*x0)/l))*sin((n*pi*((x-1)*0.01))/l)*cos((sqrt((c*n*pi/l)^2-R^2)*(t*0.001)));
wxtrec(n)=wxt;
end
w=sum(wxtrec);
wrec(x)=w;
end
plot(1:34,wrec(t+10));
F(t) = getframe;
end
movie(F)
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

owr il 18 Gen 2012
If you're not actually trying to make a movie, just visualize the animation within MATLAB, I'd highly recommend calling "plot" just once and capturing a handle to the plot object. You can then update this handle's "ydata" property within your loop without repeatedly calling plot. The result is that the animation will be much smoother and you'll be able to zoom, pan, rotate interactively during the animation without everything reseting. Here is a simple example - try rotating, etc. mid animation to see what Im talking about:
x = -1:0.01:1;
y = x.^2;
h = plot(x,y);
axis([-1,1,-1,1]);
for t = 1:500
set(h,'ydata',cos(t/10)*y);
pause(0.1);
end
Good luck!
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
David il 25 Ott 2013
Thanks for sharing this!

Accedi per commentare.

### Categorie

Scopri di più su Animation in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by