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 comentarios

What should be the final size?
SUBASH K
SUBASH K el 12 de Abr. de 2019
Editada: SUBASH K el 12 de Abr. de 2019
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?

Iniciar sesión para comentar.

 Respuesta aceptada

Initialize N_R= sym(0) before the loops

2 comentarios

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.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 12 de Abr. de 2019

Comentada:

el 12 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by