Syms variable cannot be classified?

2 vues (au cours des 30 derniers jours)
Eric Chadwick
Eric Chadwick le 10 Août 2018
Commenté : Eric Chadwick le 13 Août 2018
In the code below, the variable Qv cannot be classified, and I have no idea why. From the documentation at https://www.mathworks.com/help/distcomp/troubleshoot-variables-in-parfor-loops.html it looks like I am following all of the rules. My code looks like the one on the top right example and I reset my variable using "syms Qv". The only fix I noticed was making the variable not symbolic, but that is a requirement for the variables since I am building a matrix of linear equations that I intend to solve.
I can attach files with the variables needed to run this but basically dd is just a cell array of diameters, ll is a cell array of lengths, CC is a cell connectivity matrix, and pns is a symbolic array with some unknown values and some known values.
Anyone know what is wrong here? Thanks in advance.
parfor i=1:size(CC,1)
h = 0;
syms Qj
for j=1:size(CC)
syms Qv;
if ~isempty(CC(i,j))
k = 0;
h = h+1;
k(h) = j;
for vess = 1:size(CC{i,j})
Qv(vess,:) = -((dd{i,j}(vess,:)^3)/(12*mu))*((pns(i)-pns(j))/ll{i,j}(vess,:));
end
Qj(j,:) = Qv;
else
Qj(j,:) = 0;
end
end
QQ(i,:) = num2cell(Qj);
end

Réponse acceptée

Walter Roberson
Walter Roberson le 11 Août 2018
(A) Your code has too much confusion over how you use size(). When you do not pass in a dimension number, size() returns a vector, and when you count on using a vector as one of the bounds of a "for" loop then you likely have a bug.
(B) Do not syms up a scalar variable and then index into it. Do one of the following instead:
1)
variable = sym('prefix', [Size])
such as
qv = sym('qv', size(CC{i,j}));
This initializes the output as an array of symbol names that start with the prefix and have array indices built in, such as qv3_5 . You probably do not want this in your case;
2)
variable = sym(zeros(Size))
such as
qv = sym( zeros(1, size(CC{i,j},2)) );
or
3)
variable = zeros(Size, 'sym');
such as
qv = zeros(1, size(CC{i,j},2), 'sym');
This is more efficient than (2) but requires a newer MATLAB.
  1 commentaire
Eric Chadwick
Eric Chadwick le 13 Août 2018
Hi there! Thank you so much for your answer! I have made the changes you suggested and used solution (B) 3) and it works great!
Cheers,
Eric

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by