i want to solve that system using 4 order kutta method

1 view (last 30 days)
this is my code
function dxdt = myderiv(t,x)
x1 = x(1);
x2 = x(2);
dx1dt = (-x1 + (0.097).*(1-x1)*exp(x2/(x2/20)+1));
dx2dt = (-x2 + 8.*(0.097).*(1-x1)*exp(x2/(x2/20)+1) - (0.3).*x2);
dxdt = [dx1dt;dx2dt];
end
and
f = @(t,x) myderiv(t,x)
h = 0.00001;
t = (0:h:1)';
t(1)=0;
x(:,1) = [0 ; 0] ;
for i=1:100000
K1 = f( t(i) , x(:,i) );
K2 = f( t(i) + h/2, x(:,i) + K1*h/2 );
K3 = f( t(i) + h/2, x(:,i) + K2*h/2 );
K4 = f( t(i) + h , x(:,i) + K3*h );
x(:,i+1) = x(:,i) + (h/6)*( K1 + 2*K2 + 2*K3 + K4 );
end
plot(t,x,'.b')
I need to see a graph result of system.

Accepted Answer

Jan
Jan on 18 Nov 2021
Edited: Jan on 18 Nov 2021
The output is a simple point at [0, 0].
You start at 0. Then the expression x2/(x2/20) + 1 is NaN, because you divide by 0:
x2/(x2/20) + 1 =
20 * x2 / x2 + 1
All following points are NaN in consequence, so the trajectory is you initial value only.
Replace:
exp(x2 / (x2 / 20) + 1)
% by
exp(x2 / (x2 / 20 + 1))
Avoid using too many parentheses. Including scalar contants in parentheses is clutter. Spaces around operators are useful:
dx1dt = -x1 + 0.097 * (1 - x1) * exp(x2 / (x2 / 20 + 1));
dx2dt = -x2 + 8 * 0.097 * (1 - x1) * exp(x2 / (x2 / 20 + 1)) - 0.3 .* x2;

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!