I need help as soon as possible

조회 수: 1 (최근 30일)
Mahdi Almahuzi
Mahdi Almahuzi 2020년 3월 26일
편집: Walter Roberson 2020년 3월 26일
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
darova
darova 2020년 3월 26일
please see this: function_handle
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