I am trying to show the above using matlab, ive been working on this code for a week and am new to matlab. im struggling to complete this can someone please help me alter my code to allow the loglog plot to be outputted.
The code i have so far is:
% To solve y' = -3y+6t+5 s.t. y(-1) = 2exp(3)-1 for -1 <= t <= 2 using
% 3-step Adams-Bashforth method.
% Dom = Time Domain i.e. 2-(-1)=3
% f = RHS Function in @(t,y) format.
% F = Initial Solution.
% F2 = 2nd Step found using RK2.
% F3 = 3rd Step found using RK2.
% h = step size.
% G = Exact Solution in @(t) format.
function [y, t, h] = AB32(dom, f, F, F2, F3, h, G)
T = diff(dom); % This is length of the time interval for which you're solving for, i.e. 2-0 = 2.
N = ceil(T/h); % total number of times steps
h = T/N;
t = zeros(N+1, 1);
y = zeros(N+1, 1);
y(1) = F; % Initial value of the ODE IVP
% Compute the solution at t = h by using the true solution:
t(2) = -1+h; %To allow the process to start at t =-1
y(2) = F2;
t(3) = -1+(2*h); %To allow the process to start at t =-1
y(3) = F3;
% Main loop for marching N steps:
for i = 3:N
t(i+3) = -1+(i-1)*h; % time points
y(i+3) = y(i+2) + (h/12)*(23*f(t(i+2), y(i+2)) - (16*f(t(i+1), y(i+1))) + 5*f(t(i), y(i))); % 3-step Adams-Bashforth method!!!
%Find Error in Estimation
errAB3 = (abs(y(i+2)-G(i+2)));
%Plotting of Log-Log Plot
loglog(h, errAB3, '.-r', 'MarkerSize', 15)
legend('Error in Adams-Bashforth 3 Step Method')
xlabel('Step Size h')
ylabel('error at point x')
title('Error in Adams-Bashforth 3 Step Method')
The actual question i am answering is:
Create a Matlab function which implements 3-step Adams-Bashforth method with the time domain, the right-hand side function, the initial conditions, and the step size as inputs (arguments) and the computed solutions, the time points, and the tweaked step size as outputs.
Calculate the unknown initial conditions using RK2 method and then solve (1) using the function you just created with various step size h (e.g. ranging from 10−4 to 10−1) to verify using a log-log plot that your conclusion in (b) is correct. Remember to explain what you observe in the log-log plot. The error can be measured using the exact solution y(t) = 2e^(−3t) + 2t + 1.
Turn on thread page Beta
Trying to plot the loglog plot for the error of the AB3 method for h = 10^-1 to 10^-4 watch
- Thread Starter
- 30-10-2017 14:38