Write a function RungeKutta2 then write a code rungekutta2ex

Write a function Runge_Kutta2, then write a code runge_kutta2_exp.m to solve Example 2.25. You may need to look at the function Euler2. m and Example 2.29. Use the MATLAB function ode4 5 to solve Example 2.25; Use the Euler\'s, the improved Euler\'s and fourth-order Runge-Kutta methods above to compute numeric solutions to the following systems of ODEs in the range of [0, 0.5] with a step size of 0.1. dx/dt = x - 4y dy/dt = -x + y with the initial conditions x(0) = x_0 = 1 y(0) = y_0 = 0. Let us use the Euler method first. For this problem, we have f(t, x, y) = dx/dt = x - 4y g(t, x, y) = dy/dt = -x + y Then form the Euler formula (2.78) we obtain x_i + 1 = x_i + hf (t_i, x_i, y_i) = x_i + 0.1(x_i - 4y_i) y_i + 1 = y_i + hg(t_i, x_i, y_i) = y_i + 0.1(-x_i + y_i) Therefore x_1 = x_0 + 0.1(x_0 - 4y_0) = 1 + 0.1(1 - 4 times 0) = 1.1, y_1 = y_0 + 0.1(-x_0 + y_0) = 0 + 0.1(-1 + 0) = -0.1. At the next step, x_2 = x_1 + 0.1(x_1 - 4y_1) = 1.1 + 0.1(1.1 - 4(-0.1)) = 1.25, y_2 = y_1 + 0.1(-x_1 + y_1) = -0.1 + 0.1(-1.1 - 0.1) = -0.22. In this way, other values can be computed step by step. The results are compiled in the table below. The improve Euler method algorithm can be obtained using Eq. (2.79). kx_1 =f(t_i, x_i, y_i) = x_i - 4y_i k_y1 = g(t_i, x_i, y_i) = -x_i + y_i k_x2 = f(t_i + h, x_i + hk_x1, y_i + hk_y1) = (x_i + 0.1 k_x1) - 4(y_i + 0.1k_y1) k_y2 = g(t_i + h, x_i + hk_x1, y_i + hk_y1) = -(x_i + 0.1 k_x1) + (y_i + 0.1k_y1)

Solution

MATLAB CODE:

t=0;

x(1)=1;

y(1)=0;

h=0.1;

N=5;

fprintf(\'\\t\\t\\t Initial Values: X(0)=1 and y(0)=1\ \ \');

for N=1:5

k_x1=x(N)-4*y(N);

k_y1=-x(N)+y(N);

  

k_x2=(x(N)+0.05*k_x1)-4*(y(N)+0.05*k_y1);

k_y2=-(x(N)+0.05*k_x1)+(y(N)+0.05*k_y1);

  

k_x3=(x(N)+0.05*k_x2)-4*(y(N)+0.05*k_y2);

k_y3=-(x(N)+0.05*k_x2)+(y(N)+0.05*k_y2);

  

k_x4=(x(N)+0.05*k_x3)-4*(y(N)+0.05*k_y3);

k_y4=-(x(N)+0.05*k_x3)+(y(N)+0.05*k_y3);

  

x(N+1)=x(N)+(0.1/6)*(k_x1+2*k_x2+2*k_x3+k_x4);

y(N+1)=y(N)+(0.1/6)*(k_y1+2*k_y2+2*k_y3+k_y4);

fprintf(\'\\t Iteration %d : x(%d)= %.4f \\t\\t y(%d)=%0.4f \ \', N, N, x(N+1),N,y(N+1));

end

OUTPUT:

Initial Values: X(0)=1 and y(0)=1

   Iteration 1 : x(1)= 1.1225        y(1)=-0.1093
   Iteration 2 : x(2)= 1.3078        y(2)=-0.2453
   Iteration 3 : x(3)= 1.5753        y(3)=-0.4182
   Iteration 4 : x(4)= 1.9511        y(4)=-0.6416
   Iteration 5 : x(5)= 2.4706        y(5)=-0.9333

 Write a function Runge_Kutta2, then write a code runge_kutta2_exp.m to solve Example 2.25. You may need to look at the function Euler2. m and Example 2.29. Use
 Write a function Runge_Kutta2, then write a code runge_kutta2_exp.m to solve Example 2.25. You may need to look at the function Euler2. m and Example 2.29. Use

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site