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

Rik
Rik 2019년 1월 25일
What are the classes and sizes of the variables you're using? And what are you trying to achieve as a result?

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2019년 1월 25일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

2019년 1월 25일

댓글:

2019년 1월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by