Hey there! Sign in to join this conversationNew here? Join for free

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

    • Thread Starter
    Offline

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

 
 
 
Poll
Do you agree with the PM's proposal to cut tuition fees for some courses?
Useful resources

Make your revision easier

Maths

Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

Equations

How to use LaTex

Writing equations the easy way

Student revising

Study habits of A* students

Top tips from students who have already aced their exams

Study Planner

Create your own Study Planner

Never miss a deadline again

Polling station sign

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

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.