Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(16 marks) Consider the initial value problem dy (a) Without using pre-built com

ID: 3907322 • Letter: #

Question

(16 marks) Consider the initial value problem dy (a) Without using pre-built commands write an m-file function that uses the fourth-order Runge-Kutta method to estimate the value of y(n) for a given value n and a given step size h (b) Use the m-file function built in part (a) to compute an estimate of y(2) using step size ate the step size needed to h 0.5 and h 0.25. From these two estimates, approxim estimate y(2) correct to 4 decimal places, explaining your answer. Compute y(2) using the approximate step size and then with half the approximate step size. Comment on your results. (30 marks)

Explanation / Answer

Find the required program implementing rk 4th order. As the value of alpha is not given, it asks the same as a user input. Moreover the requirement of step size calculation is also dependent on alpha value. Though, starting with a step size of the order 10-5, can get near the requriement of getting correct estimate upto 4 decimal palces.

%=================== Main program ====================

h = [0.5 0.25 0.00001]; % Step size variants
x = 0; % Initial value of x to start from

% Asking alpha value from user
alpha = input('please provide the value for alpha: ');
w=sqrt(alpha);

for j=h
x=0;
for i=1:(2/j) % Dividing iterations based on the required step size to move from 0 to 2
k1 = j*rk(x,w);
k2 = j*rk(x+j/2, w+k1/2);
k3 = j*rk(x+j/2, w+k2/2);
k4 = j*rk(x+j, w+k3);
w = w + (k1+2*k2+2*k3+k4)/6;
x = x + j;
fprintf('@Step %d: x = %6.4f, w = %18.15f, h = %f ', i, x, w, j);
end
fprintf(' ');
end

%============================================================

%================== RK function =================

function [ dy ] = rk(x,y)
dy = (x*x) - sqrt(x*y);
end

%==========================================

Hope this helps!

NOTE: As a reminder, the matlab function requires its filename to be the same as the function itself.