If..else statement in a function - how to obtain the graph
古いコメントを表示
I'm using ode45 to solve a pair of ODEs which include an element with an 'if...then' statement. When I try to solve, MATLAB doesn't seem to take account of this command.
This is my function:
function dxdt = newode1(t,x)
%x(1)->Xo
%x(2)->Xi
a = 3600;
mu = 3600;
x(1)=1-x(2);
if (0 <= t) && (t <= 0.3)
Dt1 = 1;
else
Dt1 = 0;
end
Uh = 10*max(x(1)-x(2),0)*Dt1;
dxdt=zeros(2,1);
dxdt(1)=(a*x(1).^2)./(x(1).^2+x(2).^2)+Uh-mu.*x(1);
dxdt(2)=(a*x(2).^2)./(x(1).^2+x(2).^2)-Uh-mu.*x(2);
end
and what I put in the command line:
tspan = [0 1];
x0 = [0 1];
[t,x] = ode45(@(t,x) newode1(t,x), tspan, x0);
plot(t,x(:,1),'-o',t,x(:,2),'-.')
This is the graph I get

And this is what I should get

Note that I have denoted X_0(t) as x(1) and X_1(t) as x(2). What am I doing wrong?
4 件のコメント
madhan ravi
2018 年 11 月 28 日
upload your code instead of a picture
Daniella Crispi
2018 年 11 月 28 日
Star Strider
2018 年 11 月 28 日
What are the differential equations you are coding?
Please post an image or PDF and a description of the simulation.
Daniella Crispi
2018 年 11 月 28 日
回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
