If..else statement in a function - how to obtain the graph
Show older comments
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 Comments
madhan ravi
on 28 Nov 2018
upload your code instead of a picture
Daniella Crispi
on 28 Nov 2018
Star Strider
on 28 Nov 2018
What are the differential equations you are coding?
Please post an image or PDF and a description of the simulation.
Daniella Crispi
on 28 Nov 2018
Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!