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
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.