how to fix the error"Conversion to double from function_handle is not possible." for the code given below

n=input('Enter the number of points\n');
x=zeros(n,1);
f=zeros(n,1);
i=1;
j=1;
p=0;
l=zeros(n,1);
for i=1:n
x(i)=input('enter number\n');
end
for i=1:n
f(i)=input('Enter function value \n');
end
t=input('Enter the point whose function value is to be calculated\n');
for i=1:n
l(i)=1;
for j=1:n
if(i==j)
continue
end
syms y
l(i)=@(y)((y-x(j))/(x(i)-x(j)))*l(i);
end
p=p+l(i)*f(i);
end
fprintf('Value at given point is: %d',p(t))

 采纳的回答

I have no idea what youir code is doing. However, to calcuate with a function, it is necessary to evaluate it and then calculate with the returned value.
Try this:
n=input('Enter the number of points\n');
x=zeros(n,1);
f=zeros(n,1);
i=1;
j=1;
p=0;
l=zeros(n,1);
for i=1:n
x(i)=input('enter number\n');
end
for i=1:n
f(i)=input('Enter function value \n');
end
t=input('Enter the point whose function value is to be calculated\n');
l=@(y,xi,xj)((y-xj)./(xi-xj));
for i=1:n
lv(i)=1;
for j=1:n
if(i==j)
continue
end
lv(i)=l(t,x(i),x(j)).^2;
end
p=p+lv(i)*f(i);
end
fprintf('Value at given point is: %d\n',p)
Also, ‘y’ does not exist in the code, so I assume here ‘t’ is intended. Make the appropriate changes if ‘y’ exists somewhere else.

2 个评论

Well this is the code for lagrange interpolation problem, and i've figured out the solution for the problem, actually i was storing function handle in a vector/array which was tottaly wrong.
But i checked your modified code also and its working fine but the output is not as expected, i gues that's beacause you didn't know what this code was all about.
By the way thanks for the review.
My pleasure!
I have no idea what the code result is supposed to be. You will need to troubleshoot that.
If my Answer helped you solve your problem, please Accept it!

请先登录,再进行评论。

更多回答(0 个)

类别

Community Treasure Hunt

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

Start Hunting!

Translated by