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 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2023년 10월 13일
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
채택된 답변
Dyuman Joshi
2023년 10월 13일
편집: Dyuman Joshi
2023년 10월 13일
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.
추가 답변 (1개)
Walter Roberson
2023년 10월 13일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!