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?
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Torsten
on 16 Sep 2022
Edited: Torsten
on 16 Sep 2022
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 Comments
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!