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

Write MATLAB code to solve the second-order Adams-Bashforth method. Please help!

ID: 3889991 • Letter: W

Question

Write MATLAB code to solve the second-order Adams-Bashforth method. Please help! I have my current code as this:

n = 100;
t_i=0;
t_f=10;
f = @(x) -x;
% Analytical solution
T = @(t) exp(-t);
T_end = T(10);
Err = zeros(1,length(n));
for j=1:length(n)
h = (t_f - t_i)/n(j);
t = t_i:h:t_f;
x = zeros(1,n(j)+1);
for i=1
x(1) = x(i) + h*f(x(i));
for i=2:n(j)
x(i+1) = x(i) + h*(3*f(x(i)-f(x(i-1)))/2);
end
end
end

4. Write Matlab code to solve the following multi-step IVP iteration, which we recall is the second-order Adams-Bashforth method (or AB2) that we discussed in class Here, use Euler's method to start the multi-step method, ie. let y1 y0 + hf(to, yo). Use this code to solve the IVP above using the same h and time span that you used for your Euler method approximation. Is this a good approximation of the analytical solution that you computed? You should see that for the IVP that we defined above, our second order AB2 method managed to produce a good approximation of our true solution where the midpoint method did not. However, this does not mean that the AB2 method doesn't suffer from the same issue.

Explanation / Answer

please use the below method by passing the values of the following so as to get the desired result

function [ x, y ] = ab2 ( f, xRangeV, yIni, nSteps )

x=zeros(nSteps+1,1);

x(1) = xRangeV(1);
h = ( xRangeV(2) - xRangeV(1) ) / nSteps;
y(1,:) = transpose(yIni);
yprime(1,:) = transpose(feval ( f, x(1), y(1,:) ));
%for one step
k = 1;
yhalf = y(k,:) + 0.5 * h * yprime(k,:);
xhalf = x(k) + 0.5 * h;
yprime1 = transpose(feval ( f, xhalf, yhalf ));
y(k+1,:) = y(k,:) + h * yprime1;
x(k+1) = x(k) + h;
yprime(k+1,:) = transpose(feval ( f, x(k+1), y(k+1,:) ));
%for 2 to n steps
for k = 2 : nSteps
y(k+1,:) = y(k,:) + ...
x(k+1) = x(k) + h;
h * ( 3.0 * yprime(k,:) - yprime(k-1,:) ) / 2.0;
if k<nSteps
yprime(k+1,:) = transpose(feval ( f, x(k+1), y(k+1,:) ));
end
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote