Clear Filters
Clear Filters

Solve in a loop - Unable to perform assignment because the indices on the left side are not compatible with the size of the right side

4 views (last 30 days)
Hello,
I wrote this code in order to estimate the value of x :
for i=2:length(TimeReel)
syms x
eqn = x == (Qin*(Cin-GpureFluo(i))-GpureFluo(i)*(pi()*Qcr.*sin(acos((Qin+x-Qp)./Qcr))-(Qin+x-Qp)./2/pi().*(2*acos((Qin+x-Qp)./Qcr)))-hw*pi()*rw*rw*(GpureFluo(i)-GpureFluo(i-1))/dt(i))/GpureFluo(i);
S(i) = solve(eqn);
end
I have to use solve because the value of x is found in the acos. All others values are known
If I run this, the following message appears :
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in sym/privsubsasgn (line 1133)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 970)
C = privsubsasgn(L,R,inds{:});"
Could you help ?
Thanks :)
N.S.
  1 Comment
Walter Roberson
Walter Roberson on 13 Oct 2023
Side note:
You are going to run into precision problems due to floating point round-off. I suggest something closer to
Q = @(v) sym(v);
Qin_s = Q(Qin);
Cin_s = Q(Cin);
GpureFluo_s = Q(GpureFluo);
Pi = Q(pi);
Qcr_s = Q(Qcr);
Qin_s = Q(Qin);
Qp_s = Q(Qp);
hw_s = Q(hw);
rw_s = Q(rw);
dt_s = Q(dt);
syms x
S = cell(length(TimeReel),1);
for i=2:length(TimeReel)
eqn = x == (Qin_s*(Cin_s-GpureFluo_s(i))-GpureFluo_s(i)*(Pi*Qcr_s.*sin(acos((Qin_s+x-Qp_s)./Qcr_s))-(Qin_s+x-Qp_s)./2/Pi.*(2*acos((Qin_s+x-Qp_s)./Qcr_s)))-hw_s*Pi*rw_s^2*(GpureFluo_s(i)-GpureFluo_s(i-1))/dt_s(i))/GpureFluo_s(i);
S{i} = solve(eqn);
end

Sign in to comment.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 13 Oct 2023
Edited: Dyuman Joshi on 13 Oct 2023
It is most likely that there are multiple solutions or no solutions for equations, which can't be stored in a single placeholder.
Remember that trignometric functions are periodic in nature and multiple solutions for an equation is a possibility.
You can initialize the array S as a cell array to store the solutions and use indexing to access them -
n = length(TimeReel);
S = cell(n-1,1);
for i=2:n
syms x
eqn = x == (Qin*(Cin-GpureFluo(i))-GpureFluo(i)*(pi()*Qcr.*sin(acos((Qin+x-Qp)./Qcr))-(Qin+x-Qp)./2/pi().*(2*acos((Qin+x-Qp)./Qcr)))-hw*pi()*rw*rw*(GpureFluo(i)-GpureFluo(i-1))/dt(i))/GpureFluo(i);
S{i,1} = solve(eqn);
end
Note the answers stored are symbolic numbers. Use double() to convert the solutions to double precision numbers.

More Answers (1)

Walter Roberson
Walter Roberson on 13 Oct 2023
You would get that error if solve does not return exactly one solution, such as if it returns no solutions or returns two solutions pi apart. Store the output in a cell array

Community Treasure Hunt

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

Start Hunting!