Search code examples
matlabsymbolic-mathequation-solving

How to solve a symbolic system of equations in Matlab


I know it has to be a stupid error,but I really can't solve a system of this type:

b =

 a2*cos(q1 + q2) + a1*cos(q1)
 a2*sin(q1 + q2) + a1*sin(q1)
                 d1 + d4 + q3

>> solve(b,[q1,q2,q3,q4])
Warning: The solutions are parametrized by the symbols:
z1 = C_

> In solve at 190 

ans = 

    a1: [1x1 sym]
    d1: [1x1 sym]
    d4: [1x1 sym]
    q1: [1x1 sym]
    q2: [1x1 sym]
    q3: [1x1 sym]
    q4: [1x1 sym]

basically I want my program to see a1,d1,d4 as parameters and q1,q2,q3,q4 as variables. that's why I call solve(b,[q1,q2,q3,q4]) in this form,but it tries to solve even in the symbolic values that I haven't put into the vector.

Ty in advice for your help.


Solution

  • According to tips of solve:

    ... the call [b,a] = solve(eqns,b,a) assigns the solutions for a assigned to a and the solutions for b assigned to b.

    However you probably want to solve, b - [e1 e2 e3]' = 0 for only 3 variables (Let's say q1 q2 q3), you can't solve it for 4 variables, that would be 3 equations and 4 variables which does't make sense.

    Since I think that it is related to some mechanical system you may want to solve for only real values. You can either do this solve (eqn, 'Real', true) or declare real values: syms a1 a2 ... real.

    However you still wouldn't get a pretty result, unless you use the 'IgnoreAnalyticConstraints' option in this case:

    syms q1 q2 q3 d1 d4 a1 a2 e1 e2 e3 real
    
    b = [...
        a2*cos(q1 + q2) + a1*cos(q1)
        a2*sin(q1 + q2) + a1*sin(q1)
        d1 + d4 + q3];
    
    res = solve(b-[e1 e2 e3]', q1, q2, q3, 'IgnoreAnalyticConstraints', true);
    

    Output: (simplified)

    >> simplify(res.q1)
    
    ans =
    
     2*atan((2*a1*e2 + (- a1^4 + 2*a1^2*a2^2 + 2*a1^2*e1^2 + 2*a1^2*e2^2 - a2^4 + 2*a2^2*e1^2 + 2*a2^2*e2^2 - e1^4 - 2*e1^2*e2^2 - e2^4)^(1/2))/(a1^2 + 2*a1*e1 - a2^2 + e1^2 + e2^2))
     2*atan((2*a1*e2 - (- a1^4 + 2*a1^2*a2^2 + 2*a1^2*e1^2 + 2*a1^2*e2^2 - a2^4 + 2*a2^2*e1^2 + 2*a2^2*e2^2 - e1^4 - 2*e1^2*e2^2 - e2^4)^(1/2))/(a1^2 + 2*a1*e1 - a2^2 + e1^2 + e2^2))
    
    >> res.q2
    
    ans =
    
     -2*atan(((- a1^2 + 2*a1*a2 - a2^2 + e1^2 + e2^2)*(a1^2 + 2*a1*a2 + a2^2 - e1^2 - e2^2))^(1/2)/(- a1^2 + 2*a1*a2 - a2^2 + e1^2 + e2^2))
      2*atan(((- a1^2 + 2*a1*a2 - a2^2 + e1^2 + e2^2)*(a1^2 + 2*a1*a2 + a2^2 - e1^2 - e2^2))^(1/2)/(- a1^2 + 2*a1*a2 - a2^2 + e1^2 + e2^2))
    
    >> res.q3
    
    ans =
    
     e3 - d4 - d1
     e3 - d4 - d1