Second Order, Piecewise ODE help

조회 수: 13 (최근 30일)
Lgreer
Lgreer 2016년 3월 13일
댓글: Lgreer 2016년 3월 16일
I'm very new to MatLab and need to solve a second order ODE that equals a piecewise function. I've looked up how to solve a second order ODE with ode45 and how to solve a piecewise ODE, but I'm not sure how to combine them. Further, the IVP I need to solve does not have a y' value and I do not know how to deal with that.
The IVP is:
y'' + 4y = g(t), y(0)=y'(0)=0
where: g(t) = 0 in 0<=t<5; (t-5)/5 for 5<=t<10; 1 for t>=10
Thanks in advance, Logan

채택된 답변

Torsten
Torsten 2016년 3월 14일
fun=@(t,y)[y(2);-4*y(1)];
y0=[0 0];
tspan=[0 5];
[T1,Y1]=ode45(fun,tspan,y0);
fun=@(t,y)[y(2);-4*y(1)+(t-5)/5;
y0=[Y1(end,1) Y1(end,2)];
tspan=[T1(end) 10];
[T2,Y2]=ode45(fun,tspan,y0);
fun=@(t,y)[y(2);-4*y(1)+1];
y0=[Y2(end,1) Y2(end,2)];
tspan=[T2(end) 20];
[T3,Y3]=ode45(fun,tspan,y0);
Now put T1, T2, T3 and Y1, Y2, Y3 together to get the result of your ODE on [0 20].
Best wishes
Torsten.
  댓글 수: 1
Lgreer
Lgreer 2016년 3월 16일
Thanks for the response, The issue is that the graph comes up with two functions for each piece instead of one. Any ideas on how to combine them?

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

추가 답변 (1개)

Torsten
Torsten 2016년 3월 16일
Does this work ?
T=vertcat(T1(1:end),T2(2:end),T3(2:end));
Y=vertcat(Y1(1:end,1),Y2(2:end,1),Y3(2:end,1));
plot(T,Y:,1))
Best wishes
Torsten.
  댓글 수: 1
Lgreer
Lgreer 2016년 3월 16일
Yep, just made it plot(T,Y) and it worked perfectly :) you're my hero, thanks a lot

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

카테고리

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