MATLAB Answers

## Animation help needed with

Asked by Tom

### Tom (view profile)

on 18 Jan 2012
Latest activity Commented on by David

### David (view profile)

on 25 Oct 2013
Accepted Answer by Chandra Kurniawan

### Chandra Kurniawan (view profile)

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 Comments

Sign in to comment.

## 5 Answers ### Chandra Kurniawan (view profile)

Answer by Chandra Kurniawan

### Chandra Kurniawan (view profile)

on 18 Jan 2012
Accepted Answer

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 Comments

Sign in to comment.

Answer by Tom

### Tom (view profile)

on 18 Jan 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 Comment

Chandra Kurniawan

### Chandra Kurniawan (view profile)

on 18 Jan 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

Sign in to comment.

Answer by Tom

### Tom (view profile)

on 18 Jan 2012

Actually - it did work I just couldn't see it properly. Many thanks again.

#### 0 Comments

Sign in to comment. ### Chandra Kurniawan (view profile)

Answer by Chandra Kurniawan

### Chandra Kurniawan (view profile)

on 18 Jan 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 Comments

Sign in to comment.

Answer by owr

### owr (view profile)

on 18 Jan 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!

David

### David (view profile)

on 25 Oct 2013
Thanks for sharing this!

Sign in to comment.