How to Solve These Equations In MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around solving a set of six equations with six unknowns in MATLAB, focusing on the use of symbolic variables and the syntax of the solve() function. Participants explore various approaches to defining and solving equations, as well as addressing potential inconsistencies in the equations presented.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a set of equations and seeks guidance on solving them in MATLAB.
  • Another suggests using the solve() function with symbolic variables, but later requests clarification on the syntax when it does not work as expected.
  • Documentation for the solve() function is shared, including examples of its usage with symbolic variables.
  • A participant points out a potential inconsistency in the equations, specifically regarding the term -1/(L1 - U1) equating to zero, which leads to questions about the validity of the equations.
  • Further discussion reveals confusion about the output of the solve() function when multiple variables are involved, with participants questioning the meaning of the variable z in their solutions.
  • Some participants suggest that having multiple equations does not guarantee a unique solution, and the presence of arbitrary variables may indicate multiple solutions exist.
  • Constraints on the variables are discussed, with suggestions to use assumptions in MATLAB to clarify the nature of the solutions.

Areas of Agreement / Disagreement

Participants express uncertainty about the validity of the equations and whether the number of equations guarantees a unique solution. There is no consensus on the implications of the output from the solve() function or the nature of the variable z.

Contextual Notes

Participants note potential inconsistencies in the equations and the need for further analysis. The discussion also highlights the challenges of using MATLAB for symbolic computations and the importance of defining variables correctly.

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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K