You are Here: Home >< Maths

# log-log plot in MATLAB comparing Adams-Bashforth watch

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

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')
2. 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

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)';);```

### Related university courses

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: November 3, 2016
The home of Results and Clearing

### 2,082

people online now

### 1,567,000

students helped last year
Today on TSR

### University open days

1. Sheffield Hallam University
Tue, 21 Aug '18
2. Bournemouth University
Wed, 22 Aug '18
3. University of Buckingham
Thu, 23 Aug '18
Poll
Useful resources

### 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