Turn on thread page Beta

log-log plot in MATLAB comparing Adams-Bashforth watch

    • Thread Starter
    Offline

    0
    ReputationRep:
    I've been trying to compare 3 step Adams-Bashforth to the actual solution of y'=t^-2 (sin(2t) - 2ty) using a log-log plot in MATLAB, however I'm fairly new to this program and am unsure of how to progress further than this point, could anyone give me any advice on how to do this?



    H = 10.^-(1:0.5:4);
    H(i)=h;
    f = @(t, y) (t^-2)*(sin(2*t) - 2*t*y);
    F = @(t) (cos(2)-cos(2*t)+4)/(2*t.^2); % true solution

    % Initializations:
    t(1) = 1; % Initial time t_0 = 1 + 0*h.
    yAB(1) = 2; % Initial value of the ODE IVP

    % Solution at t = 1+h using the true solution:
    t(2) = 1 + h;
    yAB(2) = F(t(2));
    t(3) = 1 + 2*h;
    yAB(3) = F(t(3));

    % Main loop for marching 1/h steps:
    for i = 3:1/h

    t(i+1) = 1 + i*h; % time points

    % Adams-Bashforth:
    yAB(i+1) = yAB(i) + (h/12)*(23*f(t(i), yAB(i)) - 16*f(t(i-1), y(i-1)) + 5*f(t(i-2), y(i-2)));
    x=2
    err(i)=abs(yAB(1/h)-y(x));
    end

    loglog(H.', err)
    xlabel('step size h')
    ylabel('error at point t')
    Offline

    14
    ReputationRep:
    Fixed. Read the code to understand what I did.

    Code:
    h = 0.001; %step size
    t = 1 : h : 2; %initialise time vector. Better than dynamically growing 
    
    f = @(t, y) (t^-2)*(sin(2*t) - 2*t*y); %ODE
     F = @(t) (cos(2)-cos(2*t)+4)/(2*t.^2); % true solution 
    
    %Get true solution 
    for i = 1:length(t) 
        y_true(i) = F(t(i)); 
    end 
    
    %Now set up AB solving 
    % Initializations: 
    yAB(1) = 2; % Initial value of the ODE IVP. Coincides with y_true(1). 
    
    % Additional points for 3-step AB: 
    yAB(2) = y_true(2); 
    yAB(3) = y_true(3); 
    
    %initialise err vector, because we skip first three values.
     for i=1:3
        err(i)=0; 
    end 
    
    % Adams-Bashforth Loop: 
    for i = 3:length(t) 
           yAB(i+1) = yAB(i) + (h/12)*(23*f(t(i), yAB(i)) - 16*f(t(i-1), yAB(i-1)) + 5*f(t(i-2), AB(i-2))); 
           err(i)=abs(yAB(i)-y_true(i)); 
    end
    
      % log-log plot of absolute error against time. 
    plot(log(t),log(err)) 
    xlabel('Log(time)';); 
    ylabel('Log(abs error)';);
 
 
 
The home of Results and Clearing

2,082

people online now

1,567,000

students helped last year

University open days

  1. Sheffield Hallam University
    City Campus Undergraduate
    Tue, 21 Aug '18
  2. Bournemouth University
    Clearing Open Day Undergraduate
    Wed, 22 Aug '18
  3. University of Buckingham
    Postgraduate Open Evening Postgraduate
    Thu, 23 Aug '18
Poll
A-level students - how do you feel about your results?
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.