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));

回答(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 个评论

请先登录,再进行评论。

类别

Help CenterFile 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!

Translated by