- h and error
- h and h
Help needed with plotting errors of Euler's method vs h on loglog axes
6 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Harsha Priya Daggubati
2020 年 5 月 11 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!