why do I face problem with matlab solve function?

조회 수: 14 (최근 30일)
Qinglin Mok
Qinglin Mok 2013년 1월 19일
hi, I'm trying to use the solve function in matlab and I have no idea why I kept getting errors as such:
"??? Error using ==> solve>getEqns at 182 ' L1*cos(pi/2)+L2.*cos(q2)-L3*cos(pi)==d1.*cos(q1) ' is not a valid expression or equation.
Error in ==> solve at 67 [eqns,vars] = getEqns(varargin{:});
Error in ==> test1 at 69 solve('L1*cos(pi/2)+L2.*cos(q2)-L3*cos(pi)==d1.*cos(q1)','L1*sin(pi/2)+L2.*sin(q2)-L3*sin(pi)==d1.*sin(q1)'); "
d1 and q1 are unknowns. L1=800; L2=150; L3=35; q2 is a 10*1 matrix
program:
syms d1 q1
solve('L1*cos(pi/2)+L2.*cos(q2)-L3*cos(pi)==d1.*cos(q1)','L1*sin(pi/2)+L2.*sin(q2)-L3*sin(pi)==d1.*sin(q1)');
Thanks

답변 (2개)

Walter Roberson
Walter Roberson 2013년 1월 19일
When you use a quoted string as the argument to solve(), then you must use MuPAD syntax, which does not recognize "==" and requires "=" instead.
Caution: if you set values for those constants but use quoted strings as the arguments to solve() then the values will not be substituted in. You need to either subs() on the quoted strings or not use quoted strings.
  댓글 수: 2
Qinglin Mok
Qinglin Mok 2013년 1월 19일
sorry i dont understand. i have not studied matlab that in depth. so does it mean that I have to change all the == to =? I tried that and I got error as such:
??? Error using ==> solve>getEqns at 182 ' L1*cos(pi/2)+L2.*cos(q2)-L3*cos(pi)=d1.*cos(q1) ' is not a valid expression or equation.
Error in ==> solve at 67 [eqns,vars] = getEqns(varargin{:});
Error in ==> test1 at 79 solve('L1*cos(pi/2)+L2.*cos(q2)-L3*cos(pi)=d1.*cos(q1)','L1*sin(pi/2)+L2.*sin(q2)-L3*sin(pi)=d1.*sin(q1)'); >>
Walter Roberson
Walter Roberson 2013년 1월 19일
편집: Walter Roberson 2013년 1월 19일
solve(subs('L1*cos(pi/2)+L2*cos(q2)-L3*cos(pi)=d1*cos(q1)'), subs('L1*sin(pi/2)+L2*sin(q2)-L3*sin(pi)=d1*sin(q1)'), d1, q1 );
Notice the "." have been removed, and subs() has been added to import the values of the constants.
Alternately
solve( L1*cos(pi/2)+L2.*cos(q2)-L3*cos(pi)==d1.*cos(q1), L1*sin(pi/2)+L2.*sin(q2)-L3*sin(pi)==d1.*sin(q1), d1, q1);
Notice that there are no quotation marks here.
However, this second form will only work from R2011b onwards. Before that you would use
solve(L1*cos(pi/2)+L2.*cos(q2)-L3*cos(pi) - d1.*cos(q1), L1*sin(pi/2)+L2.*sin(q2)-L3*sin(pi) - d1.*sin(q1), d1, q1);

댓글을 달려면 로그인하십시오.


Roger Stafford
Roger Stafford 2013년 1월 19일
Your equations are of the form
d1*cos(q1) = K1
d1*sin(q1) = K2
for which the solutions can easily be determined without using 'solve'.
q1 = atan2(K2,K1);
d1 = sqrt(K1^2+K2^2);
By replacing K1 and K2 by their equivalent in terms of your known parameters you can find all solutions for d1 and q1.
  댓글 수: 2
Qinglin Mok
Qinglin Mok 2013년 1월 19일
편집: Walter Roberson 2013년 1월 19일
hi.. thanks for that alternative solution. I tried but I got a 1x1 sym for d1 and q1 when I'm suppose to get the same size as q2?
syms d1 q1
k1=L1*cos(pi/2)+L2*cos(q2)-L3*cos(pi);
k2=L1*sin(pi/2)+L2*sin(q2)-L3*sin(pi);
k1=d1*cos(q1);
k2=d1*sin(q1);
q1 = atan(k2,k1);
d1 = sqrt(k1^2+k2^2);
Roger Stafford
Roger Stafford 2013년 1월 19일
It isn't necessary to declare q1 and d1 as sym. The following will solve for them as vectors the same size as q2.
K1 = L1*cos(pi/2)+L2*cos(q2)-L3*cos(pi);
K2 = L1*sin(pi/2)+L2*sin(q2)-L3*sin(pi);
q1 = atan2(K2,K1);
d1 = sqrt(K1.^2+K2.^2);

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by