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?
+1
A:
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.
gnovice
2010-08-31 01:39:59
Yes. It's also a good idea to note that this will only work if the OP has the MATLAB Symbolic Toolbox installed and that the variables need to be declared as symbolic using the `syms` commmand.
Gilead
2010-08-31 02:39:41
@Gilead: You don't actually have to declare the variables as symbolic if the equations and variables passed to SOLVE are strings, like in the example I used above.
gnovice
2010-08-31 14:30:41
You are quite right. My mistake.
Gilead
2010-08-31 14:58:37