I need help as soon as possible

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 Comments

What is w? A vector? What is yexact? A function handle, to a function that apparently represnts the exact solution to that problem?
yexact is NOT the vector of true values form that function. It is a function handle.
IF you want to be able to subtract the two, then you need to EVALUATE that function at a set of points for t. I don't know why you think yexact needs to be passed y itself. It does not, because it does not use y itself.
yexact is a function handle that has to be invoked
how to do so?
can any one solve the code problems and send here ?
please see this: function_handle
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.

Sign in to comment.

Answers (1)

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

Asked:

on 26 Mar 2020

Edited:

on 26 Mar 2020

Community Treasure Hunt

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

Start Hunting!