UNABLE TO PERFORM THE ASSIGNMENT
조회 수: 2 (최근 30일)
이전 댓글 표시
syms a x z Finaleq_1 K alpha A
C=zeros(1,'sym')
Finaleq_1=zeros(1,2,'sym')
A=zeros(1,'sym')
series(x)=sym(zeros(1,1));
C(1)=a;
C(2)=0;
for i=1:2
T1=0;
T2=0;
T3=0;
T4=0;
C(i+2)=z;
for r=1:i
T2=T2+kroneckerDelta(sym(r-2))*(i-r+1)*(i-r+2)*C(i-r+3);
T3=T3+C(r)*(i-r+1)*C(i-r+2);
T4=T4+kroneckerDelta(sym(r-2))*C(i-r+1);
for m=1:r
%
T1=T1+kroneckerDelta(sym(m-2))*C(r-m+1)*(i-r+1)*(i-r+2)*C(i-r+3)
end
% fprintf('*******')
end
% fprintf('%%%%%%')
Finaleq_1=T1+T2+2*T3+2*K*i*C(i+1)-alpha*T4;
A=solve(Finaleq_1,z) % tHE PROBLEM IS HERE .
C(i+2)=A
end
Solving the equation will give a sclar quantity but it will be in variable form and A is also a scalar .Then why it is showing an error the following error:
Unable to perform assignment because the left and right sides have a different number of elements.
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
C = privsubsasgn(L,R,inds{:});
댓글 수: 5
Dyuman Joshi
2023년 4월 3일
It is initially defined as a 1x1 sym array, but you are over-writing it with this command -
A=solve(Finaleq_1,z)
And you can see above as well, A is no longer 1x1 sym
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1343769/image.png)
채택된 답변
Walter Roberson
2023년 4월 5일
이동: Walter Roberson
2023년 4월 5일
Your equation has no solutions, at least not for i = 1:2 .
Your T values come out 0 when i = 1. That leads to Finaleq_1 = 0, and then you solve() that 0 for z. MATLAB says "Oh, okay, that is solved if z = 0, then you would have 0 for the equation and 0 for z" so it returns 0 the first time, which you store into A. Then at the end of the cycle your C(i+2)=A; stores that 0 into C(3)
Then when i = 2, when r becomes 2, (i-r+3) becomes 2-2+3 = 3 so C(i-r+3) recalls that 0 (the A from the previous round.) Put that into the kroneckerDelta(sym(r-2))*(i-r+1)*(i-r+2)*C(i-r+3) and although the sym(r-2) is sym(0) and kroneckerDelta(0) is 1, with the C(i-r+3) being 0, then 0 is added to T2. Conditions for the result of the Finaleq_1 expression are slightly different this round, so you get an α coming in from a different variable, but all of the multiples of z (the variable you are solving for) are 0, so your Finaleq_1 becomes solve(constant times alpha, z) . Which has no solutions unless alpha just happens to equal 0.
So, no solution, at least not for i = 1 and i = 2.
추가 답변 (1개)
Steven Lord
2023년 4월 3일
The problem is not with the solve call. The problem is on the next line. You're assuming that the equation you're solving has exactly one solution. What happens if it has multiple solutions or if it has no solutions?
syms x
solNone = solve(x == 1, x == 2, x) % no number is simultaneously equal to 1 and equal to 2
solMultiple = solve(x^2 == 4, x)
What happens if you were to try to assign either solNone (with zero elements) or solMultiple (with two elements) to one element of another vector? MATLAB throws an error.
try
solutionVector(1) = solNone; % error, too few solutions
catch ME
fprintf("This assignment failed with error '%s'\n", ME.message)
end
try
solutionVector(1) = solMultiple; % error, too many solutions
catch ME
fprintf("This assignment failed with error '%s'\n", ME.message)
end
Check the size of the vector you're trying to assign into the one element of C. You need to determine how to handle the cases where it has more or fewer elements than the part of the vector into which you're trying to store it.
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!