How to run this differential MATLAB program

2 次查看(过去 30 天)
My lecturer give me MATLAB code for diff eq. question and it can show animation of pendulum and graph. but i dont know how to use his code, I know how to run it but dont know where can I input my diff eq. the code i copy has its own diff eq. but i dont know which one and also it doesnt have any comment so i would know how to use it. This is the code, maybe someone can teach me how to use it or maybe make the code more easy to read. Please kindly help :)
Edit : i found that dt can change the run time of the code it makes the graph run faster. but i cant solve my problem yet :(
clc
close all
clear all
a=1;b=-2;c=2;b=b/a;c=c/a;
dt=.001;tap=1;btsa=tap;btsb=-tap;
y0=0;y1=1;
y1=y0-y1*dt;
n=40;
for k=1:n;
px(k)=(-1)^k*.1;
end
px=[0 0 px 0 0];
g0=0;
g=0;
for k=1:4000;
t=k*dt;
g=exp(-t);
g=g/a;
y=((2+b*dt-c*dt^2)*y0+g*dt^2-y1)/(1+b*dt);
by=y+.3;
py=linspace(by,tap-.3,n+1)-(tap-.3-by)/(2*n);
py(1)=py(1)+(tap-.3-by)/(2*n);
py=[y py tap-.3 tap];
subplot(2,2,1)
hold off
plot([-2 2],[tap tap],'Color','k','LineWidth',4)
hold on
plot([0 4],[y y],'Color',[.8 .8 .8]);
plot(px,py,'-r')
plot(0,y,'--o','MarkerFaceColor',[1 0 0],'MarkerEdgeColor',[0 0 0],'MarkerSize',20)
axis([-3 3 -tap+.1 tap+.1])
subplot(2,2,2)
plot([0 k*dt+10],[0 0],'-k')
hold on
gam=plot([0 (k+1)*dt],[y y],'Color',[.8 .8 .8]);
plot([k*dt (k+1)*dt],[y0 y],'-b')
btsa=max(max(btsa,g),y);btsb=min(min(btsb,g),y);
axis([0 max(10,k*dt)+.5 btsb-.1 btsa+.1])
y1=y0;y0=y;
subplot(2,2,4)
plot([0 k*dt+10],[0 0],'-k')
hold on
plot([k*dt (k+1)*dt],[g0 g],'-r')
axis([0 max(10,k*dt)+.5 btsb-.1 btsa+.1])
g0=g;
pause(.1)
delete(gam)
end

回答(1 个)

Yongjian Feng
Yongjian Feng 2021-11-2
This is not the whole script, right? The first line is
end
The second line is px=[0 0 px 0 0 ];
Then what is px?
  2 个评论
Aji Bowo
Aji Bowo 2021-11-2
编辑:Aji Bowo 2021-11-2
yes it is not the whole and i think the px is for plot it shown below. but let me add the first line
clc
close all
clear all
a=1;b=-2;c=2;b=b/a;c=c/a;
dt=.001;tap=1;btsa=tap;btsb=-tap;
y0=0;y1=1;
y1=y0-y1*dt;
n=40;
for k=1:n;
px(k)=(-1)^k*.1;
end
Yongjian Feng
Yongjian Feng 2021-11-2
No idea how it works. In my opinion, it is reasonable at this point to ask your lecturer for help.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by