1
$\begingroup$

$$ dx/dt= 1-y \\ dy/dt= x^2-y^2 $$ $t=[0,5]$, $h=0.1$, $x(0)= -1.2$, $y(0)= 0$,

Am I right about writing code ? Can you check it out ?

h=0.1;                                             
t = 0:h:5;                                         
x = zeros(1,length(t)); 
y = zeros(1,length(t)); 

x(1) = -1.2;                                          
y(1) = 0; 

F_txy = @(t,x,y) 1-y;                                   
G_txy = @(t,x,y) x^2-y^2;

for i=1:(length(t)-1)   

    k_1 = F_txy(t(i),x(i),y(i));
    L_1 = G_txy(t(i),x(i),y(i));

    k_2 = F_txy(t(i)+0.5*h,x(i)+0.5*h*k_1,y(i)+0.5*h*L_1);
    L_2 = G_txy(t(i)+0.5*h,x(i)+0.5*h*k_1,y(i)+0.5*h*L_1);

    k_3 = F_txy((t(i)+0.5*h),(x(i)+0.5*h*k_2),(y(i)+0.5*h*L_2));
    L_3 = G_txy((t(i)+0.5*h),(x(i)+0.5*h*k_2),(y(i)+0.5*h*L_2));

    k_4 = F_txy((t(i)+h),(x(i)+k_3*h),(y(i)+L_3*h));        
    L_4 = G_txy((t(i)+h),(x(i)+k_3*h),(y(i)+L_3*h));

    x(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h;  % main equation
    y(i+1) = y(i) + (1/6)*(L_1+2*L_2+2*L_3+L_4)*h;  % main equation

end

plot(x,y)

ylabel('y'); xlabel('x'); legend('numerical');

hold on
$\endgroup$

closed as too broad by Rahul, Aditya Hase, José Carlos Santos, Parcly Taxel, max_zorn Oct 28 '18 at 0:36

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ What exactly is your problem? Does the code run? Does it work on a test example with known exact solution? Does it produce for your ODE compatible results for half and double step length? Do the error estimates there support the order 4 of the method? $\endgroup$ – LutzL Oct 27 '18 at 9:03