Using ODE45 with sine function

조회 수: 6 (최근 30일)
Heather Newell
Heather Newell 2021년 12월 2일
답변: Star Strider 2021년 12월 2일
I have a preliminary simple system with the code as follows. I keep getting an error for 'Not enought input arguments'. I'm not really sure what is going wrong here.
function Fv=funsys(t,Y)
Fv(1,1)=2*Y(1)+Y(2)+5*Y(3)+exp(-2*t);
Fv(2,1)=-3*Y(1)-2*Y(2)-8*Y(3)+2*exp(-2*t)-cos(3*t);
Fv(3,1)=3*Y(1)+3*Y(2)+2*Y(3)+cos(3*t);
[tv,Yv]=ode23(@(t,Y)funsys(t,Y),[0 pi/2],[1;-1;0]);
plot(tv,Yv)
Any help would be appreciated.
  댓글 수: 2
James Tursa
James Tursa 2021년 12월 2일
Is the code you posted all in one file as written? You should not have the ode23 and plot code inside the funsys function.
Heather Newell
Heather Newell 2021년 12월 2일
Yes. I know they work together seperatly, but is there any way to write ode and plot within the same file?

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2021년 12월 2일
... is there any way to write ode and plot within the same file?
See if this produces the desired result —
[tv,Yv]=ode23(@(t,Y)funsys(t,Y),[0 pi/2],[1;-1;0]);
figure
plot(tv,Yv)
grid
legend(compose('Y_{%d}',1:size(Yv,2)), 'Location','best')
function Fv=funsys(t,Y)
Fv(1,1)=2*Y(1)+Y(2)+5*Y(3)+exp(-2*t);
Fv(2,1)=-3*Y(1)-2*Y(2)-8*Y(3)+2*exp(-2*t)-cos(3*t);
Fv(3,1)=3*Y(1)+3*Y(2)+2*Y(3)+cos(3*t);
end
All in one script (.m) file.
.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by