How can I make a movie of the plotted function due to time (for t=0:0.1:2)?
1 次查看(过去 30 天)
显示 更早的评论
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)');
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!