I need help as soon as possible

1 次查看(过去 30 天)
i write this code it says there is error in line 17 how to solve it ?
clc
clear
close all
N=10;
a=0;b=2;alp=0.5;
f=@(t,y)y-t^2+1;
yexact=@(y,t)(1+t)^2-0.5*exp(t);
h=(b-a)/N;
w=[alp];
i=1;
t=a:h:b;
for i=1:N
w(i+1)=w(i)+h*f(t(i),w(i));
i=i+1;
end
error=abs(yexact-w); %here is the error
w;
disp('-------------------------------------------------');
disp('t EXACT NUMERICAL ERROR');
disp('-------------------------------------------------');
fprintf("%3.0f %6.2f %5.6f %5.6f \n",N,yexact,w,error); %and here also
  6 个评论
Adam
Adam 2020-3-26
For future reference, question titles like 'I need help as soon as possible' or 'Urgent...' just annoy people and are actually less likely to get you fast help than just giving a question title that says what your question is about. If people can help they will, but telling people your question is somehow more urgent than everyone else's will sometimes just cause people to ignore it.

请先登录,再进行评论。

回答(1 个)

Stephan
Stephan 2020-3-26
Try to work with a table to make life easier:
N=1:10;
a=0;b=2;alp=0.5;
f=@(t,y)y-t^2+1;
yexact=@(y,t)(1+t).^2-0.5*exp(t);
h=(b-a)/N(end);
w=alp;
t=a:h:b;
for i=N
w(i+1)=w(i)+h*f(t(i),w(i));
i=i+1;
end
error=yexact(w,t)-w;
yexact_vals = yexact(w,t)';
result = table(t(:), yexact_vals, w(:), error(:),'VariableNames',...
{'t','EXACT','NUMERICAL','ERROR'})
results in:
result =
11×4 table
t EXACT NUMERICAL ERROR
___ ______ _________ ________
0 0.5 0.5 0
0.2 0.8293 0.8 0.029299
0.4 1.2141 1.152 0.062088
0.6 1.6489 1.5504 0.098541
0.8 2.1272 1.9885 0.13875
1 2.6409 2.4582 0.18268
1.2 3.1799 2.9498 0.23013
1.4 3.7324 3.4518 0.28063
1.6 4.2835 3.9501 0.33336
1.8 4.8152 4.4282 0.38702
2 5.3055 4.8658 0.43969

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by