MATLAB How to Solve These Equations In MATLAB

  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
To solve a system of six equations with six unknowns in MATLAB, users should define the equations using symbolic variables and utilize the `solve()` function. The syntax involves specifying the equations and the variables to solve for, and it can return solutions in terms of other variables if the system has infinite solutions. Users have noted inconsistencies in the original equations, particularly regarding the implications of division by zero, which suggests a need for reevaluation of the equations. Additionally, when multiple variables are involved, the output may include arbitrary constants, indicating multiple solutions exist. Constraints on the variables can be set to ensure real and positive solutions, but having seven equations for seven variables does not guarantee a unique solution.
EngWiPy
Messages
1,361
Reaction score
61
Hi,

After some programming in MATLAB I arrived to the following equations:

Code:
 mu1 + 3*mu2 - 1/(L1 - U1)
       2*mu1 - 1/(L2 - U2)
              -1/(L1 - U1)
       2*mu2 - 1/(L2 - U2)
             U1 + 2*U2 - 2
           3*U1 - 2*L2 + 5

which all equal to zero. How can I solve these 6 equations with 6 unknowns in MATLAB?

Thanks in advance
 
Physics news on Phys.org
If the equations are defined using symbolic variables, you should be able to just use solve().
 
jhae2.718 said:
If the equations are defined using symbolic variables, you should be able to just use solve().

Can you please tell me the syntax? It is not working with me.

Thanks
 
Here's the MathWorks' documentation: http://www.mathworks.com/help/toolbox/symbolic/solve.html"

Code:
solve(eq1, eq2, ..., eqn, var1, var2, ..., varn)

Basically, solve() takes either functions defined in strings (e.g. 'x^2 + y^2') or functions/anonymous functions, and solves for the sym variables (FYI you can check variable type using class() ). You can optionally specify which variable to solve for.

So, IIRC, for a simple system of quadratic equations, you could do:
Code:
syms x;
f = x.^2 + 2*x + 1;
g = -x.^2 - 2*x - 1; 
roots = solve(f, g);

Those examples should work, but my brain fails me after 11 PM, so my apologies if they don't...(unfortunately, I don’t have MATLAB to check right now. C'mon, guys: release r2011a!)
 
Last edited by a moderator:
S David: In looking at the eqns. in the OP, you have -1/(L1 - U1) = 0.
This implies (L1 - U1) = -1/0. I think there is some inconsistency in the set
of those six equations.
 
SteamKing said:
S David: In looking at the eqns. in the OP, you have -1/(L1 - U1) = 0.
This implies (L1 - U1) = -1/0. I think there is some inconsistency in the set
of those six equations.

You are absolutely right. This means either L1-U1=infinity, or -1=0, where both are impossible because in my problem, both L1 and U1 are dimensions of a rectangle in a polyhedron. I think I have to re-check my analysis.

jhae2.718: when I tried to solve the equations you gave, MATLAB gives me

Code:
syms x y;
f = x.^2 + 2*y + 1;
g = -x.^2 - 2*y - 1; 
roots = solve(f, g) 

roots = 

    x: [2x1 sym]
    y: [2x1 sym]

assuming that you have two variables not one, because you have two equations. right? What is this output and why?

Thanks
 
Yes, that's because of multiple variables. If you type roots.x into the Command Window, it will display x, and likewise for roots.y.

You could also do:
Code:
[x y] = solve(f, g);
which would give you x and y as separate variables.
 
jhae2.718 said:
Yes, that's because of multiple variables. If you type roots.x into the Command Window, it will display x, and likewise for roots.y.

You could also do:
Code:
syms x y;
f = x.^2 + 2*y + 1;
g = -x.^2 - 2*y - 1; 
[x y] = solve(f, g);
which would give you x and y as separate variables.


Now I got:

Code:
 syms x y;
f = x.^2 + 2*y + 1;
g = -x.^2 - 2*y - 1; 
[x y] = solve(f, g)
 
x =
 
  (- 2*z - 1)^(1/2)
 -(- 2*z - 1)^(1/2)
 
 
y =
 
 z
 z

what is z here?
 
I created a pretty bad example last night, since g = -f.

What is happening is that MATLAB is assuming that f : x,y \mapsto z and g : x,y \mapsto z in an xyz coordinate system.

The system x+2y=0, 3x+y=1 might be a better example.

I believe that in solve() you can set syms equal to zero as in solve(f=0, g=0).
 
  • #10
jhae2.718 said:
I created a pretty bad example last night, since g = -f.

What is happening is that MATLAB is assuming that f : x,y \mapsto z and g : x,y \mapsto z in an xyz coordinate system.

when I solve my problem, I get the same variable z in the solution. What does this mean exactly?
 
  • #11
What was the system you were working with?

solve() should work if a set of solutions to the system exists. The example I posted used essentially the same equation twice, so there was an infinitude of solutions.

Try this, and you should see a working example (finally, and sorry!):
Code:
syms x y;
f = 3*x - 2*y -1;
g = x + 2*y;
[x y] = solve(f, g);
 
  • #12
jhae2.718 said:
What was the system you were working with?

solve() should work if a set of solutions to the system exists. The example I posted used essentially the same equation twice, so there was an infinitude of solutions.

Try this, and you should see a working example (finally, and sorry!):
Code:
syms x y;
f = 3*x - 2*y -1;
g = x + 2*y;
[x y] = solve(f, g);


After minor modification, I got the following equations ans solution:

Code:
 U1 - L1 - (L1 - U1)*(mu1 + 3*mu2)
           U2 - L2 - 2*mu1*(L2 - U2)
           U1 - L1 - 6*mu3*(L1 - U1)
 U2 - L2 - (L2 - U2)*(2*mu2 + 4*mu3)
                       U1 + 2*U2 - 2
                     3*U1 - 2*L2 + 5
                   - 6*L1 - 4*L2 - 6
 

roots = 

     L1: [3x1 sym]
     L2: [3x1 sym]
     U1: [3x1 sym]
     U2: [3x1 sym]
    mu1: [3x1 sym]
    mu2: [3x1 sym]
    mu3: [3x1 sym]

where if you type roots.L1 for example it will give you a solution of z variable.
 
  • #13
I believe z is any arbitrary number; that is, there exists multiple solutions to the equation. You can try different vales for z using the subs() command; you'll probably have to determine the solution based on the problem you are solving.
 
  • #14
jhae2.718 said:
I believe z is any arbitrary number; that is, there exists multiple solutions to the equation. You can try different vales for z using the subs() command; you'll probably have to determine the solution based on the problem you are solving.

But I have 7 equations and 7 variables. There must be a distinct solution. Right?
 
  • #15
Are there any constraints on the variables (e.g., positive and real)? If so you could declare those assumptions using the real and positive keywords for http://www.mathworks.com/help/toolbox/symbolic/syms.html".

As far as seven equations and seven variables leading to a unique solution, I don't think that's necessarily true. A mathematician could probably answer that more definitively, though.

I have found using MATLAB to evaluate symbolic expressions to be rather frustrating at times.
 
Last edited by a moderator:
Back
Top