The classical fourth order Runge-Kutta method of solving an ODE has the form: y_
ID: 3109705 • Letter: T
Question
The classical fourth order Runge-Kutta method of solving an ODE has the form: y_n + 1 = y_n + h/6 (k_1 + 2k_2 + 2k_3 + k_4), where dy/dx = f(x, y); y_n = y(x_n); k_1 = f(x_n, y_n); k_2 = f(x_n + h/2, y_n + k_1 h/2); k_3 = f(x_n + h/2, y_n + k_2 h/2); k_4 = f(x_n + h, y_n + k_3 h); (a) Write a Matlab function that will return the value y_n + 1 when the ODE f(x, y), x_n, y_n and the step-size h are given as the input. Give your function a reasonable name and properly identify input and output parameters. (b) An ODE (plus the initial value) and its analytical solution are given below. Write a Matlab script m-file that will solve the following ODE in the range 0Explanation / Answer
THE MATLAB CODE:
clc;
clear all;
h=input('Enter step size '); % step size
x = 0:h:3; % Calculates upto y(3)
y = zeros(1,length(x));
y(1) = input('Enter initial condition i.e y(0) value '); % initial condition y(0)
F = @(t,s) 4.*exp(-0.8.*t)-0.5*s; % The desired function. Can be changed according to the requirement
for n=1:(length(x)-1) % loop to calculate
k1 = F(x(n),y(n));
k2 = F(x(n)+0.5*h,y(n)+0.5*h*k1);
k3 = F((x(n)+0.5*h),(y(n)+0.5*h*k2));
k4 = F((x(n)+h),(y(n)+k3*h));
y(n+1) = y(n) + (1/6)*(k1+2*k2+2*k3+k4)*h; % Final equation.
end
for i=1:length(x)
Fa(i)=4/1.3*(exp(0.8*x(i))-exp(-0.5*x(i))+2.*exp(-0.5*x(i))); %Analytical solution values
end
plot(y,x,'r'); %plotting numerical solution vs x. Comes in red color
hold all
plot(Fa,x,'b'); %plotting analytical solution vs x. Comes in blue color
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.