linear equation cant solve

2 ビュー (過去 30 日間)
Waritwong Sukprasongdee
Waritwong Sukprasongdee 2020 年 9 月 20 日
コメント済み: Walter Roberson 2020 年 9 月 20 日
rcd = [-1 - 1 2] ;
ucd = rcd/norm(rcd);
syms Ax Ay Az Bx Bz T ;
eqn1 = [Ax Ay Az]+[0 0 -2*50*9.80665]+[Bx 0 Bz]+ T*ucd == [0 0 0] ;
eqn2 = cross([0 -2 0],[Ax Ay Az])+cross([2 0 0],[0 0 -50*9.80665])+cross([1 1 0],T*ucd)+cross([0 2 0],[Bx 0 Bz]) == [0,0,0] ;
solve = solve(eqn1,eqn2,[Ax Ay Az Bx Bz T])
//here the error message
Error in + (line 7)
X = privBinaryOp(A, B, 'symobj::zipWithImplicitExpansion', '_plus');
Error in myfisttest (line 4)
eqn1 = [Ax Ay Az]+[0 0 -2*50*9.80665]+[Bx 0 Bz]+ T*ucd == [0 0 0] ;

回答 (2 件)

Alan Stevens
Alan Stevens 2020 年 9 月 20 日
Like so
rcd = [-1, - 1, 2] ;
ucd = rcd/norm(rcd);
syms Ax Ay Az Bx Bz T ;
eqn1 = [Ax Ay Az]+[0 0 -2*50*9.80665]+[Bx 0 Bz]+ T*ucd == [0 0 0] ;
eqn2 = cross([0 -2 0],[Ax Ay Az])+cross([2 0 0],[0 0 -50*9.80665])+cross([1 1 0],T*ucd)+cross([0 2 0],[Bx 0 Bz]) == [0,0,0] ;
soln = solve(eqn1,eqn2,[Ax Ay Az Bx Bz T])
  4 件のコメント
Waritwong Sukprasongdee
Waritwong Sukprasongdee 2020 年 9 月 20 日
thx but why does matlab interpret it as a 1 x 2 vector not 1 x 3 vector
John D'Errico
John D'Errico 2020 年 9 月 20 日
Be careful when you write something like this:
rcd = [-1 - 1 2]
Separating the minus sign from the number can introduce a bug into your code. (Which it did.)
>> rcd = [-1 - 1 2]
rcd =
-2 2
>> rcd = [-1 -1 2]
rcd =
-1 -1 2
Do you see the difference above? Note that it is perfectly valid to use spaces instead of a comma to separate terms in a vector, yet we get two different sized vectors as a result.
As it turns out, that the cause of your problem. What does this do:
rcd = [-1 - 1 2] ;
MATLAB sees the minus there, when distinct from the number as telling it to perform a SUBTRACTION. So it first subtracts 1 from -1, getting -2 as the result. Then it sees the number 2, and decides it needs to create a vector of length 2 because there is no operator between the elements.
Alan fixed it by putting in commas to delineate the numbers in the vector, though I would argue that explaining the problem is more important, regardless of whether a comma or a space is used to delineate the vector elements.

サインインしてコメントする。


Alan Stevens
Alan Stevens 2020 年 9 月 20 日
"... though I would argue that explaining the problem is more important, ..."
I agree. I didn't notice the extra space, just that the result was 1x2, so I didn't realise the explanation!
  2 件のコメント
John D'Errico
John D'Errico 2020 年 9 月 20 日
編集済み: John D'Errico 2020 年 9 月 20 日
(Please use comments, not answers for comments.)
Anyway, that is why I added my comment, to explain why your solution worked.
The minus sign "feature" in MATLAB is a dangerous thing, because it can be so often misunderstood what is happening. Here is another case where you need to be incredibly careful with minus signs.
>> -1^4
ans =
-1
Yeah, right. Like raising a negative number to an even integer power can ever result in a negative number? But it does. And that is because of operator precedence rules.
Concatenation operators fall at the very bottom of that list, apparently so low they fell completely off the bottom. :)
Anyway, in the example I just gave, power appears on a higher rung even than does negation (unary minus). So MATLAB essentially sees - (1)^4, which is -1. Of course, this gives a different result, the expected value of 1.
>> (-1)^4
ans =
1
In the original case of
1 - 1
MATLAB parses this as using the dyadic minus operator, because it sees a number on either side of an operator. But when we write
1, - 1
here MATLAB has no choice but to see the - as a unary (or monadic) minus, so it negates the 1, and only then performs a concatenation.
As I said, you need to be very careful when playing with minus signs in MATLAB. But I'd not want to appear as negative. :)
Walter Roberson
Walter Roberson 2020 年 9 月 20 日
There are other computer languages in which -1^4 would be interpreted as (-1)^4 because in those languages the unary operations are considered to bind very tightly. There are also languages where 10^-4 would be considered to be an error because the - is interpreted as a subtraction instead of a unary minus.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by