Turn on thread page Beta
 You are Here: Home >< Maths

Trying to plot the loglog plot for the error of the AB3 method for h = 10^-1 to 10^-4 watch

1. 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.

%Inputs Below:
% 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;

% Preallocations:
t = zeros(N+1, 1);
y = zeros(N+1, 1);

% Initializations:
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)));

end

%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
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: October 30, 2017
Today on TSR

Cambridge interviews

Find out which colleges are sending invitations

University open days

• University of East Anglia
UEA Mini Open Day Undergraduate
Fri, 23 Nov '18
• Norwich University of the Arts
Fri, 23 Nov '18
• Edge Hill University
Sat, 24 Nov '18
Poll
Useful resources

Make your revision easier

Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

How to use LaTex

Writing equations the easy way

Study habits of A* students

Top tips from students who have already aced their exams

Create your own Study Planner

Never miss a deadline again

Thinking about a maths degree?

Chat with other maths applicants

Can you help? Study help unanswered threads

Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE