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 日間)
古いコメントを表示
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));
0 件のコメント
回答 (1 件)
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)
3 件のコメント
参考
カテゴリ
Help Center および File Exchange で Numerical Integration and Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!