Error inserting symbolic expression in for-loop

A have some expressions outside a loop and want to use them inside the loop like
p=P(i,j) %some expressions that must be calculated outside the loop
q=Q(i,j) %some expressions that must be calculated outside the loop
for i=1:10
for j=1:10
W(i,j)=P+i+j
V(i,j)=Q+i-j
end
end
i keep recieving this error:
The following error occurred converting from sym to double:
Unable to convert expression into double array.

1 Comment

What are the classes and sizes of the variables you're using? And what are you trying to achieve as a result?

Sign in to comment.

 Accepted Answer

Your P or Q appear to be a symbolic function, and you appear to be invoking them with specific arguments i and j to produce p and q. But then you use the full P and Q functions inside the loop instead of p and q. With P and Q being symbolic functions, it would not be possible to convert the right hand side to double. It might be possible to convert p and q to double.
If it turns out not to be possible to convert p and q to double, then you will need to initialize W and V to symbolic instead of numeric. It could be the case that you had previously used W and V in your calculations and so even though you might be overwriting each entry of W and V, that would have left them lying in memory as numeric.
W = zeros(10, 10, 'sym');
V = zeros(10, 10, 'sym');

4 Comments

That code attempts to add Q+q(n) etc before Q is defined.
For testing purposes, it would help to know what some inputs should be fore the number of springs and the positions and M and kl^3 values.
The i initialized P and Q, the error in inside the second loop. it does not use the expression of P and Q inside second loop. it uses just letters 'P' and 'Q'.
Your code has
syms i j
so when you calculate sin(i*pi*L(n)/l) and sin(j*pi*L(n)/l) then those are symbolic i and j that are used in the expression. Q will therefore come out including expressions such as
11*sin((pi*i)/5)*sin((pi*j)/5)
where the i and j are unresolved symbolic numbers.
Then when you loop for i and for j, and reference Q, then the i and j inside of Q will not be replaced by the i and j loop variables. Symbolic variables are not "pointers" to variables in the MATLAB workspace. Symbolic variables are references to items that live in a different process (the MuPAD symbolic engine), and no connection is made between the engine and the MATLAB workspace variables unless you request substituation using subs()
syms x l1 l q w_2 i j
format short;
Q = sym(0);
P = sym(0);
nms=input('Enter number of mass-springs attached to the beam: ');
for n=1:nms
fprintf('Enter the position of mass-spring #%d Ln/L: ',n)
t(n)=input('\n');
L(n)=l*t(n);
fprintf('Enter the mass-spring #%d M/ml: ',n)
p(n)=input('\n');
fprintf('Enter the mass-spring #%d kl^3/EI: ',n)
q(n)=input('\n');
Q=Q+q(n)*sin(i*pi*L(n)/l)*sin(j*pi*L(n)/l);
P=P+p(n)*sin(i*pi*L(n)/l)*sin(j*pi*L(n)/l);
end
P
Q
k(1:6,1:6)=0;
m(1:6,1:6)=0;
for i=1:6
for j=1:6
if i==j
k(i,j)=subs(Q)+(0.5*(pi*i)^4);
m(i,j)=subs(P)+0.5;
else
k(i,j)=subs(Q)+(pi^3*(i*j)^2/2)*((sin((i-j)*pi)/(i-j))-(sin((i+j)*pi)/(i+j)));
m(i,j)=subs(P)+(1/(2*pi))*((sin((i-j)*pi)/(i-j))-(sin((i+j)*pi)/(i+j)));
end
end
end
that worked, great
THanks

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!