필터 지우기
필터 지우기

Solving a system of ODEs with conditionally defined coefficient

조회 수: 1 (최근 30일)
Cris19
Cris19 2019년 12월 15일
댓글: Cris19 2019년 12월 15일
Hi, I want to plot on the interval the solution of the following system of ODEs
with the initial conditions
,
where
.
I wrote the function, by defining the coefficient f, using "piecewise" sintax:
function dzdt=odefunw1(t,z)
syms t
f=piecewise(0<=t<1, -2*t^2+3*t, t>=1, 1/t);
dzdt=zeros(2,1);
dzdt(1)=z(2);
dzdt(2)=z(1)-f*z(2);
end
In the Command Window, I wrote
tspan = [0 1000];
z0 = [0.1 0.1];
[t,z] = ode45(@(t,z) odefunw1(t,z), tspan, z0);
plot(t,z(:,1),'g',t,z(:,2),'b')
But several errors occured. Where do I wrong and how could I fix it? Thanks in advance.

채택된 답변

darova
darova 2019년 12월 15일
replace
f=piecewise(0<=t<1, -2*t^2+3*t, t>=1, 1/t);
with
if t >= 1
f = 1/t;
else
f = -2*t^2+3*t;
end
  댓글 수: 3
darova
darova 2019년 12월 15일
You are overwriting t variable. Remove this line:
syms t
Cris19
Cris19 2019년 12월 15일
It worked! Thank you very much!

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

추가 답변 (0개)

카테고리

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