- h and error
- h and h
Help needed with plotting errors of Euler's method vs h on loglog axes
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (1)
Harsha Priya Daggubati
am 11 Mai 2020
Hi,
Could you check on the reason why you intend to plot double-log axes with:
You mentioned you want to plot 'h' and its corresponding 'error' value, it is confusing why you need the above 2nd plot.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!