Problem in converting expression into double array and error in converting from sym to double

Hi,
I have written a code to get Lagrange Interpolation functions used in FInite element analysis. Below is the code.
% lagrange interpolation functions
syms r;
r_e=input('enter the natural co-ordinate values of r in the order of nodes as row vector=');
for i=1:length(r_e)
for j=1:length(r_e)
if j~=i
N_R(j)=((r-r_e(j)))/(r_e(i)-r_e(j));
else
N_R(j)=1;
end
end
N_r(i)=prod(N);
end
But it shows these error when i enter the inputs!
enter the natural co-ordinate values of r in the order of nodes as row vector=[-1 1]
The following error occurred converting from sym to double:
Unable to convert expression into double array.
Error in lagrange (line 8)
N_R(j)=((r-r_e(j)))/(r_e(i)-r_e(j));
Please suggest me a solution.

2 Comments

N_r(j) size is 1*1
what am trying to do is to fromulate this
If size is the Problem then where am i doing wrong, how to maitain vector or matrix size in loops?

Sign in to comment.

 Accepted Answer

Initialize N_R= sym(0) before the loops

2 Comments

Thank you very much.It works flawlessly.
Since the final output of N_R is symbolic, so we are supposed to initilize in the form of symbols. Is this could be the reason?
The type of a variable is determined by the first assignment. In most cases the assignment is an expression involving the symbolic variable r, but you start with i and j both 1 so you first do the else of i~=j and you assign 1 in that case which is numeric not symbolic. With the array not having been initialized that made the whole array numeric.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!