How to Solve These Equations In MATLAB

  • MATLAB
  • Thread starter EngWiPy
  • Start date
  • Tags
    Matlab
In summary, the equations given in the conversation are used to solve for the unknown variables mu1, mu2, and mu3 in MATLAB. The solve() function can be used to find the symbolic solutions to a system of equations, where the equations are defined using symbolic variables. The output of solve() will be a structure containing the solutions for each variable. However, it is important to ensure that the equations are consistent and that a solution exists.
  • #1
EngWiPy
1,368
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
  • #2
If the equations are defined using symbolic variables, you should be able to just use solve().
 
  • #3
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
 
  • #4
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:
  • #5
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.
 
  • #6
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
 
  • #7
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.
 
  • #8
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?
 
  • #9
I created a pretty bad example last night, since g = -f.

What is happening is that MATLAB is assuming that [tex]f : x,y \mapsto z[/tex] and [tex]g : x,y \mapsto z[/tex] 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 [tex]f : x,y \mapsto z[/tex] and [tex]g : x,y \mapsto z[/tex] 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:

1. How do I use MATLAB to solve equations?

To solve equations in MATLAB, you can use the built-in function 'solve'. This function takes in two arguments: the equation(s) you want to solve and the variable(s) you want to solve for. You can also use the 'syms' command to define symbolic variables before using the 'solve' function.

2. Can I use MATLAB to solve systems of equations?

Yes, MATLAB has a function called 'linsolve' specifically for solving systems of linear equations. You can also use the 'solve' function for non-linear systems of equations.

3. How can I solve differential equations in MATLAB?

To solve differential equations in MATLAB, you can use the 'ode45' function. This function solves initial value problems for a system of first-order differential equations.

4. What if I have multiple solutions to an equation? Can MATLAB find all of them?

Yes, the 'solve' function in MATLAB can find all solutions to an equation. It returns a vector of all the solutions, including complex solutions if they exist.

5. Is it possible to plot the solutions to an equation in MATLAB?

Yes, you can use the 'ezplot' function in MATLAB to plot the solutions to an equation. This function takes in the equation and the variable to plot against as arguments.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Mechanical Engineering
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Calculus and Beyond Homework Help
Replies
7
Views
499
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top