MATLAB symbolic to double error

Hi, I am trying to run this code but there are errors. I don't know how to remove them. Here is code: This error comes when converting symbolic to double. Any help much appreciated.
clear all;
clc;
nel=4; % number of elements
totlength=10;
nne=3;
nnp=2*nel+1;
le=totlength/nel;
x=0:le:totlength;
element_nodes=[1:2:(nnp-2);2:2:(nnp-1);3:2:nnp]';
%element_nodes=[1:nel;2:nnp]';
B=[];
syms xlc
%Finding out B matrix
for i=1:1:nel
j=element_nodes(i,1)
B(i)=[(2*xlc-x(i+2)-x(j+1))/(2*le^2),(2*xlc-x(j+2)-x(i))/(-1*le^2),(2*xlc-x(j+1)-x(i))/(2*le^2)];
end
Any help or suggestions? Thanks.

Respuestas (1)

Walter Roberson
Walter Roberson el 7 de Nov. de 2013
Experiment with starting with
B = sym([]);
I think you are going to have trouble with your assignment to B(i) anyhow, as you appear to be attempting to assign a list of 3 elements into a single storage location.

5 comentarios

Andrew
Andrew el 7 de Nov. de 2013
This gives different error, some index mismatch.
Walter Roberson
Walter Roberson el 7 de Nov. de 2013
Right. Because you are trying to store three elements into a single location. Notice the [] on the right hand side of your B(i): it indicates you are building a list of values. You think more about what your want your B to look like at the end.
Andrew
Andrew el 7 de Nov. de 2013
I want B to be matrix at the end, n rows by 3 columns. If I write code,
[(2*xlc-x(i+2)-x(j+1))/(2*le^2),(2*xlc-x(j+2)-x(i))/(-1*le^2),(2*xlc-x(j+1)-x(i))/(2*le^2)]
it gives correct answer, but I want to build a matrix.
Andrew
Andrew el 7 de Nov. de 2013
I modified my code. Instead of writing 3 values in 1 row, I created two nested loops to write values at each location. But still error is present. Here is code:
clear all;
clc;
nel=4; % number of elements
totlength=10;
nne=3;
nnp=2*nel+1;
le=totlength/nel;
x=0:le:totlength;
element_nodes=[1:2:(nnp-2);2:2:(nnp-1);3:2:nnp]';
%element_nodes=[1:nel;2:nnp]';
B=[];
syms xlc
%Finding out B matrix
for i=1:1:nel
k=1:1:nne
j=element_nodes(i,1);
if(k==1)
B(i,k)=vpa((2*xlc-x(i+2)-x(j+1))/(2*le^2));
elseif (k==2)
B(i,k)=vpa((2*xlc-x(j+2)-x(i))/(-1*le^2));
else
B(i,k)=vpa((2*xlc-x(j+1)-x(i))/(2*le^2));
end
%B(i)=[(2*xlc-x(i+2)-x(j+1))/(2*le^2),(2*xlc-x(j+2)-x(i))/(-1*le^2),(2*xlc-x(j+1)-x(i))/(2*le^2)];
end
B = sym([]);
And in the loop,
B(i,:) = [(2*xlc-x(i+2)-x(j+1))/(2*le^2),(2*xlc-x(j+2)-x(i))/(-1*le^2),(2*xlc-x(j+1)-x(i))/(2*le^2)];

Iniciar sesión para comentar.

Categorías

Más información sobre Symbolic Math Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 7 de Nov. de 2013

Comentada:

el 7 de Nov. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by