Help needed with plotting errors of Euler's method vs h on loglog axes

14 views (last 30 days)
Hi,
The question I am attempting is applying Euler's Method to solve y' = f(t, y), a <= t b <= y(a) = y0 with time-step h = 2^-(i+1), i = 1, 2, . . . , 16. For each value of h, computing the corresponding maximum of the absolute value of the error. I've also provided a table with two columns containing h and the corresponding maximum errors, and 'plotted' these errors versus h in a figure with double-log axes.
My issue is with the plotting of the errors versus h. Here is my code:
N2 = 16;
h = zeros(N2,1);
error = zeros(N2,1);
%a = 0;
%b = 4;
%T = 4;
y0 = -1;
for i = 1:N2
h(i) = 2^(-(i-1));
N2 = T/h(i);
[t,w] = Euler(0,T,N2,y0,f);
error(i) = abs(ysol(T)-w(end));
end
% creating table
format long
table(h,error,'VariableNames',{'h','error'})
% creating loglog plots
loglog(h,error,'r', 'linewidth',1.5)
hold on
loglog(h, h, 'm','linewidth',1.5)
xlabel('h')
legend('error','O(h)');
title('Plot of errors of Eulers method vs h','fontsize',15)
I've put in bold the part I'm having trouble with. When I plot the graph, i get two straight lines that are far away from each other, but I think i need the lines to closely follow one another. What am I doing wrong?
Thank you kindly for your help in advance!

Answers (1)

Harsha Priya Daggubati
Harsha Priya Daggubati on 11 May 2020
Hi,
Could you check on the reason why you intend to plot double-log axes with:
  1. h and error
  2. h and h
You mentioned you want to plot 'h' and its corresponding 'error' value, it is confusing why you need the above 2nd plot.

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!