1

I'm trying to solve a system of ode's using Runge-kutta, i made a function for RK2(f,h,x0,y0,xfinal) and tried to solve the system shown below with specified IC's. Could someone help fix the code as I get errors and code doesn't work. ode set

beta = 1/3;
gamma = 1/7;
syms R S I % Symbolic Math Toolbox
N = S+I+R;
ode1 = -(beta*I*S)/N;
ode2 = -(beta*I*S)/N-gamma*I;
ode3 = gamma*I;
odes = [ode1,ode2,ode3];
for j = odes
    RK2(j,0.2,0,8e6,7);
end


function [xs,ys] = RK2(f,h,x0,y0,xfinal)
ffnc = matlabFunction(f);
fprintf('\n x        y ');
o = 1;
while x0 <= xfinal
    fprintf('\n%4.3f  %4.3f ',x0,y0); %values of x and y
    xs(o) = x0;
    ys(o) = y0;
    k1 = h*ffnc (x0,y0); 
    x1 = x0+h; 
    k2 = h*ffnc (x1,y0+k1);
    y1 = y0+(k1+k2)/2;           
    x0 = x1;
    y0 = y1;  
    o = o+1;
end
end
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
Mv2156
  • 27
  • 5
  • 1
    Why do you use the *Symbolic Math Toolbox* at all? You could define the MATLAB function right away `ode1 = @(R,S,I)-(beta*I*S)/(S+I+R)`... BTW, you never hand over the input `S` when calling the function-handle in `RK2` – max Apr 14 '20 at 05:50
  • Are you sure this is RK2? Usually that series RK2, RK3, RK4 has midpoint evaluations. You use the second order Heun method. (If `f` were properly defined.) Note that in the computation of the `S` data series you are trying to use values of the `I` series that are not yet computed. Your computer would need a time tunneling device and Matlab the time-travel library to make it work this way. – Lutz Lehmann Apr 14 '20 at 06:33

0 Answers0