Modify the following function so that it performs N steps of the Adams-Bashforth
ID: 3844764 • Letter: M
Question
Modify the following function so that it performs N steps of the Adams-Bashforth-Moulton combination in PECE mode. function [x, y] = ab4 (x, y, h, N) % The first 3 steps are taken using a four-stage explicit % Runge-Kutta method for i=1: 3 k1 = fn(x, y): k2 = fn(x + 0.5 * h, y+ 0.5 * h * k1): k3 = fn(x + 0.5 * h, y+ 0.5 * h * k2): k4 = fn(x+h, y + h + k3): y = y + h/6 *(k1 + 2 * k2 + 2 * k3 + k4): x = x + h: f(:, i) = k1: end f(:, 4) = fn(x, y): %Now do the rest of the steps using the four-step % Adams Bash forth for i = 4: H y = y + h/24 *(55 * f(:, 4)-59 * f(:, 3) + 37 * f(:, 2) -9 * f(:, 1)): x = x + h: f(:, 1) = f(:, 2): f(:, 2) = f(:, 3): f(:, 3) = f(: .4): f(:, 4) = fn (x, y): end (b) Estimate y(10) for IVP1.Explanation / Answer
clear;
% rhs = input(' Enter the right-hand side of the ODE, f(t,y) => ','s');
% Hardwired for HTML
rhs = 't*t';
f = inline(rhs,'t','y') % Inline function whose input arguments are t & y.
f = vectorize(f)
t0 = 0;
y0 = 1;
T = 1;
h = 0.05;
N = round( (T-t0)/h ) % Compute the number of steps from t0 to T
% ROUND(X) rounds the elements of X to the nearest integers.
tn = linspace (t0, T, N+1) % LINSPACE(t0, T, N+1) generates N+1 points between t0 and T.
yn = zeros(1, N+1); % ZEROS(1, N+1) is an 1-by-(N+1) matrix of zeros.
yn(1, 1) = y0 % Store the initial y0 in yn.
% Initialize t and y with t0 and y0, respectively.
t = t0
y = y0
for i = 1:3
k1 = f(t,y);
k2 = f(t+h/2, y+h/2*k1);
k3 = f(t+h/2, y+h/2*k2);
k4 = f(t+h, y+h*k3);
y = y+h/6*(k1 + 2*k2 + 2*k3 + k4);
t = t + h;
yn(1,i+1) = y;
end
for i = 4:N
y = y + h/24*( 55*f(tn(1,i), yn(1,i)) - 59*f(tn(1,i-1), yn(1,i-1)) + 37*f(tn(1,i-2), yn(1,i-2)) - 9*f(tn(1,i-3), yn(1,i-3)) );
t = t+h;
yn(1,i+1) = y;
end
skip = 0.1/h; % List data with the step size 0.1
fprintf('%6.3f %10.6f ', [tn(1:skip:N+1); yn(1:skip:N+1)])
%change the function by changing the value of rhs.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.