Search code examples
matlabsymbolic-math

Solve for only certain variables with symbolic solver


I am trying to solve a system of equations in MATLAB with 3 variables and 5 constants. Is it possible to solve for the three variables with solve while keeping the constants as symbolic and not replacing them with numerical values?


Solution

  • When you use the SOLVE function (from the Symbolic Toolbox) you can specify the variables you want to solve for. For example, let's say you have three equations with variables x, y, and z and constants a and b. The following will give you a structure S with fields 'x', 'y', and 'z' containing symbolic equations for those variables which include the constants a and b:

    >> S = solve('x+y=a','x-y=b','z=x^2+y^2','x','y','z');  %# Solve for x, y, and z
    >> [S.x; S.y; S.z]  %# Get the equations from the structure
    
    ans =
    
         a/2 + b/2  %# Equation for x
         a/2 - b/2  %# Equation for y
     a^2/2 + b^2/2  %# Equation for z
    

    If symbolic solutions can't be found for a system of equations, numeric solutions will be returned instead.