linear equation cant solve

1 Ansicht (letzte 30 Tage)
Waritwong Sukprasongdee
Waritwong Sukprasongdee am 20 Sep. 2020
Kommentiert: Walter Roberson am 20 Sep. 2020
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] ;

Antworten (2)

Alan Stevens
Alan Stevens am 20 Sep. 2020
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 Kommentare
Waritwong Sukprasongdee
Waritwong Sukprasongdee am 20 Sep. 2020
thx but why does matlab interpret it as a 1 x 2 vector not 1 x 3 vector
John D'Errico
John D'Errico am 20 Sep. 2020
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.

Melden Sie sich an, um zu kommentieren.


Alan Stevens
Alan Stevens am 20 Sep. 2020
"... 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 Kommentare
John D'Errico
John D'Errico am 20 Sep. 2020
Bearbeitet: John D'Errico am 20 Sep. 2020
(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 am 20 Sep. 2020
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.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by