[問題] 兩種解ode的方法結果不同已回收

看板MATLAB作者 (~黑妞~)時間16年前 (2010/02/18 12:15), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
我使用了matlab內建的ode45解題器 時間是t=t0:ts:tf t0=0 ts=0.01 tf=10 A法:一種是直接讓解題器從0~10秒解完 B法:一種是解了0.01秒之後所得到的解,將此解當作是下一次的初始值 這兩解方法的結果應該要一樣,解出的結果卻不同~ A法程式: ========================================================================= clc clear close all global u; u=0; t0=0;ts=0.01;tf=10; t=t0:ts:tf; x0=[0.3;0;-0.2;0]; [T,x]=ode45('plant',t,x0); hold on plot(T,x(:,1)) plot(T,x(:,2),'r') plot(T,x(:,3),'k') plot(T,x(:,4),'m') hold off xlabel('time(sec)') ylabel('state-value') legend('x1','x2','x3','x4') title('state') ========================================================================= B法程式: ========================================================================= clc clear close all global u; u=0; t0=0;ts=0.01;tf=10; x0=[0.3;0;-0.2;0]; for k=1:tf/ts [T,x]=ode45('plant',ts,x0); x0=x(end,:); x1(k,1)=x(end,1); x2(k,1)=x(end,2); x3(k,1)=x(end,3); x4(k,1)=x(end,4); time(k,1)=k*T(end); end hold on plot(time,x1) plot(time,x2,'r') plot(time,x3,'k') plot(time,x4,'m') hold off xlabel('time(sec)') ylabel('state-value') legend('x1','x2','x3','x4') title('state') ========================================================================= 函式plant: ========================================================================= function dx=plant(t,x) global u; disturbace=(x(1)+x(2))*cos(0.5*t)+2*sin(t); dx=[x(2) x(3) x(4) x(1)-x(2)-2*x(3)+x(4)+u+disturbace]; ========================================================================= 請板上的各位幫我解惑~ 困擾很久了~~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 124.8.138.176 fishfree:宅神原po真是帥!!! 09/16 06:23 推 lovetone:樓上真是專業!!! 09/16 12:19 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.121.207.199 ※ 編輯: lovetone 來自: 122.121.207.199 (02/18 12:27) ※ 編輯: lovetone 來自: 122.121.207.199 (02/18 12:30)

02/18 23:47, , 1F
B法每次解完時間沒有更新,所以每次他都是解0~0.01秒
02/18 23:47, 1F
文章代碼(AID): #1BVBxlHK (MATLAB)
文章代碼(AID): #1BVBxlHK (MATLAB)