Euler method table problem.Why my fprintf(y(i+1)) only has the last ans, my y1-y(i+1) is incorrect and how to let the table be tidy?

4 ビュー (過去 30 日間)
Chen
Chen 2022 年 9 月 16 日
コメント済み: Chen 2022 年 9 月 16 日
h=0.2;
x=0:h:1;
y=zeros(size(x));
y(1)=0;
n=numel(y);
y1=exp(x)-x-1;
fprintf('xn \t yn\t y(xn)\t error\n ');
for i=1:n-1
f = x(i)+y(i);
y(i+1) = y(i) + h * f;
end
fprintf('%f\t %f\t %f\t %f\n',x,y(i+1),y1,y1-y(i+1));

回答 (1 件)

Torsten
Torsten 2022 年 9 月 16 日
編集済み: Torsten 2022 年 9 月 16 日
h=0.002;
x=0:h:1;
y=zeros(size(x));
y(1)=0;
n=numel(y);
y1=exp(x)-x-1;
for i=1:n-1
f = x(i)+y(i);
y(i+1) = y(i) + h * f;
end
hold on
plot(x,y)
plot(x,y1)
hold off
Xn = x.';
Yn = y.';
Exact_Yn = y1.';
Error = abs(y.'-y1.');
T = table(Xn,Yn,Exact_Yn,Error)
T = 501×4 table
Xn Yn Exact_Yn Error _____ __________ __________ __________ 0 0 0 0 0.002 0 2.0013e-06 2.0013e-06 0.004 4e-06 8.0107e-06 4.0107e-06 0.006 1.2008e-05 1.8036e-05 6.0281e-06 0.008 2.4032e-05 3.2086e-05 8.0535e-06 0.01 4.008e-05 5.0167e-05 1.0087e-05 0.012 6.016e-05 7.2289e-05 1.2129e-05 0.014 8.4281e-05 9.8459e-05 1.4178e-05 0.016 0.00011245 0.00012869 1.6236e-05 0.018 0.00014467 0.00016298 1.8302e-05 0.02 0.00018096 0.00020134 2.0377e-05 0.022 0.00022133 0.00024378 2.2459e-05 0.024 0.00026577 0.00029032 2.455e-05 0.026 0.0003143 0.00034095 2.6649e-05 0.028 0.00036693 0.00039568 2.8756e-05 0.03 0.00042366 0.00045453 3.0872e-05
  3 件のコメント
Chen
Chen 2022 年 9 月 16 日
Thank you for your help!!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by