How can I make a movie of the plotted function due to time (for t=0:0.1:2)?

조회 수: 6 (최근 30일)
OK
OK 2017년 5월 1일
댓글: OK 2017년 5월 1일
Hello! I want to ask you how to make a movie of the plotted function due to time for t=0:0.1:2. In the code I wrote the formula of the differential equation (gif) after i wrote the formula of the coefficient (a). In the formula of gif i gave (x-v*t) instead of (x) in order to make it depending on the time (t). I plotted it of course, but just for a value of time. I'm stuck on how to make the plot for different time values between 0 and 2 with step width=0.1 as a movie. I hope that you can help me about that issue.
syms x;
a=zeros(110,1);
%stage 3
v=1; %m/s
gif=0;
t=0;
for i=1:1:110
a(i)=(2/50)*(int(x*sin((i*pi*x)/50),x,0,1)+int((2-x)*sin((i*pi*x)/50),x,1,2));
gif =gif + a(i)*sin((i*pi*(x-v*t)/50));
end
clear x
figure(1)
x=[-20 20];
plot=fplot(gif,x);
grid on
xlabel('space');
ylabel('phi(x)');

채택된 답변

hmi amid
hmi amid 2017년 5월 1일
Hi,
First of all it's better to remove the int function from the for loop. It will make it really slow. This is what I came up with to simplify your problem. And also use drawnow if you want to plot within a for loop and thus create an animation. Try to avoid as much as possible for loops in matlab.
clear,clc
syms X I
F=int(X*sin((I*pi*X)/50),X,0,1)+int((2-X)*sin((I*pi*X)/50),X,1,2);
a=double((2/50)*subs(F,I,1:110));
%stage 3
v=1; %m/s
gif=0;
x=-20:0.1:20;
for t=0:0.1:5
for i=1:1:110
gif =gif + a(i).*sin((i*pi*(x-v*t)/50));
end
figure(1)
plot(x,gif);
grid on
xlabel('space');
ylabel('phi(x)');
title(t)
drawnow
end
A better version would be to remove the for loop for i like this
clear,clc
syms X I
F=int(X*sin((I*pi*X)/50),X,0,1)+int((2-X)*sin((I*pi*X)/50),X,1,2);
i=1:1:110;
a=double((2/50)*subs(F,I,i));
%stage 3
v=1; %m/s
x=-20:0.1:20;
for t=0:0.1:5
gif =a*sin(i'*pi*(x-v*t)/50);
figure(1)
plot(x,gif);
grid on
xlabel('space');
ylabel('phi(x)');
title(t)
drawnow
end
Making it even faster (using matrix multiplication).
Amid.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by