conversion to logical from sym is not possible error within a for loop

1 view (last 30 days)
Hi everyone!
I had an issue regarding referring to my variables in a loop. Essentially, above my loop, I have a bunch of functions defined where Csns, Czns, Csnl and Cznl are symbols.
In the for loop, I wanted to input each distinct combination of the 4 variables into a system of 3 equations, and if the equations held true, record those values in solution.
However there is an error repeatedly coming up saying:
Conversion to logical from sym is not possible.
Error in mtrl575prob4attempt5Alyssa (line 183) if dGS_Csns - dGL_Csnl > -.0001
Is there any way to negate this error in the loop?
Far above the loop: GS (Csns, Czns) GL (Csnl, Cznl) dGS_Csns (Csnl, Cznl) dGS_Czns (Csnl, Cznl) dGL_Csnl (Csnl, Cznl) dGL_Cznl (Csnl, Cznl) are all set as complicated functions of the variables shown in brackets (they are really large so I did not include them).
%system of equations we want to solve:
eq1 = dGS_Csns - dGL_Csnl == 0;
eq2 = dGS_Czns - dGL_Cznl == 0;
eq3 = dGL_Csnl*Csnl+dGL_Cznl*Cznl-dGS_Csns*Csns+dGS_Czns*Czns-(GL-GS) == 0;
solution = zeros(1,4);
for i = 0.001:0.001:1
Csns = i;
for j = 0.001:0.001:1;
Czns = j;
for k = 0.001:0.001:1
Csnl = k;
for z = 0.001:0.001:1
Cznl = z;
if eq1 > -.0001 && eq1 <.0001
if eq2 >-.0001 && eq2 <.0001
if eq3 >-.0001 && eq3 <.0001
n = 1;
solution(n,:) = [Csns,Czns,Csnl,Cznl];
n = n + 1;
end
end
end
end
end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 21 Dec 2013
Edited: Walter Roberson on 21 Dec 2013
[Csns, Czns, Csn, Cznl] = ndgrid( 0.001:0.001:1, 0.001:0.001:1, 0.001:0.001:1, 0.001:0.001:1);
TGS = GS(Csns, Czns);
TGL = GL(Csnl, Cznl);
TdGS_Csns = dGS_Csns(Csnl, Cznl);
TdGS_Czns = dGS_Czns(Csnl, Cznl);
TdGL_Csnl = dGL_Csnl(Csnl, Cznl);
TdGL_Cznl = dGL_Cznl (Csnl, Cznl);
eq1 = TdGS_Csns - TdGL_Csnl;
eq2 = TdGS_Czns - TdGL_Cznl;
eq3 = TdGL_Csnl .* Csnl + TdGL_Cznl .* Cznl - TdGS_Csns .* Csns + TdGS_Czns .* Czns - (TGL - TGS);
eq1holds = abs(eq1) < 0.001;
eq2holds = abs(eq2) < 0.001;
eq3holds = abs(eq3) < 0.001;
alleqhold = eq1holds & eq2holds & eq3holds;
n = nnz(alleqhold); %number of non-zeros is count of trues
where_holds = [Csns(alleqhold), Czns(alleqhold), Csnl(alleqhold), Cznl(alleqhold)];
Expect this to take rather some time to calculate all 10^12 calculations. And hope you have a lot of memory.
  1 Comment
Alyssa
Alyssa on 21 Dec 2013
Hi Walter,
I like the idea of the abs to clean it up a bit.
I am running into issues with ndgrid though. I had initially tried using it but encountered memory problems, which is why I resorted to looping.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!