why do I face problem with matlab solve function?
13 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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 个评论
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
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 个评论
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!