Conversion to function_handle from double is not possible.

The error is "The following error occurred converting from function_handle to double:" and it has problem on line 20 of code that I attached. Does anyone can help to fix this code? please.
function [x,w]=gauss_legendre(n)
syms p%legendre polynomial vector
syms polmat % Jacobi matrix
syms y% independent variable
p=zeros(n,1);
polmat=zeros(n,n);
i=1;
while(i<=n) %generate Jacobi matrix
if(i<=(n-1))
polmat(i,i+1)=sqrt((i^2)/((4*(i^2)) -1));
end
if(1<i)
polmat(i,i-1)=sqrt(((i-1)^2)/((4*((i-1)^2)) -1));
end
i=i+1;
end
i=3;
p(1,1)=1/sqrt(2); %first Legendre polynomial
p(2,1)=@(y) sqrt(3/2)*y; %second Legendre polynomial
while(i<=n) %generate Legendre polynomial by induction
p(i,1)=@(y) (sqrt((4*(i-1)^2) -1)/(i-1)) *(y*p(i-1,1)-(i-2)*p(i-2,1)/sqrt((4*(i-2)^2) -1));
i=i+1;
end
x=eig(polmat); %get eigenvalues as vector form
i=1;
w=zeros(n,1); %initialize weights as vector form
while(i<=n)
y=x(i,1); %input value to y
w(i,1)=(norm(p(y)))^(-2);%This form is calculated at problem 3
i=i+1;
end
end

 Accepted Answer

You create p as a double array at the start of your code:
p=zeros(n,1);
and then later you try to put a function handle into the double array:
p(2,1)=@(y)...
Is a function handle a numeric values that can be put into a double array? No, it is not. So you need to fix your algorithm. It is possible to store function handles in cell arrays, so perhaps this might do what you need:
p = cell(n,1);
...
p{2} = @(y)...
and call it like this:
p{k}(y)
I have no idea if this works for your algorithm: you are the one who is responsible for deciding that.
PS: you should align your code using the default Editor settings: select all code and press ctrl+i. This will make the code much easier to follow.

2 Comments

Thanks. Your suggestion solved almost all of errors of my problem. I'd like to ask one more about your algorithm.
while(i<=n)
y=x(i,1); %input value to y
w(i,1)=(norm(p(y)))^(-2);%This form is calculated at problem 3
i=i+1;
end
I change my code to
i=1;
j=1;
w=cell(n,1); %initialize weights as vector form
while(i<=n)
summ=0;
while(j<=n)
y=x(i,1);
summ=summ+(p{j}(y))^2;
j=j+1;
end
w{i}=1/summ;%This form is calculated at problem 3
i=i+1;
end
("summ" is variable to sum square of elements.) This code makes error " subscript indices must either be real positive integers or logicals" on line
summ=summ+(p{j}(y))^2;
can you explain why this dummy index "j" makes error?
@Jinman Park: you should set a breakpoint on that line, and check:
  1. the size of p when it reaches that line,
  2. the size and exact value of j:
fprintf('%.30f\n',j)

Sign in to comment.

More Answers (1)

x1=0.1
x2=0.5
x3=5
f(x1)=30.20
f(x2)=7
f(x3)=10.6
x4=x2-0.5*((((x2-x1)^2)*(f(x2)-f(x3)))-((x2-x3^2)*(f(x2)-f(x1)))/(((x2-x1))*(f(x2)-f(x3)))-((x2-x3)*(f(x2)-f(x1))))
Conversion to function_handle from double is not possible.
what can ı do

Products

Community Treasure Hunt

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

Start Hunting!