MATLAB Answers

How to solve for three equations from a large array of equations?

3 views (last 30 days)
Osman Ajmal
Osman Ajmal on 2 Aug 2018
Edited: Eduard Reitmann on 3 Aug 2018
I've got a large array of equations (linear and non-linear) all defined as f(x,y,z) and i would like to solve for all three variables x,y,z where the program outputs all x,y,z values that solve for any combination of three equations from the large array of equations. Is there any way to do this other than using the solve funtion within loops to iteratively go through all combinations of equations?

  4 Comments

Show 1 older comment
Osman Ajmal
Osman Ajmal on 2 Aug 2018
for example for an array of 5 equations, solving for any 3 in the form of:
syms x y z
eqn_A(1) = z == 2;
eqn_A(2) = z == 5;
eqn_A(3) = (x.^2)+(y.^2) == 4;
eqn_A(4) = (x.^2)+(y.^2) + (z.^2) == 3;
eqn_A(5) = (x-1).^2 + (y-1).^2 + (z-1).^2 == 4;
[X,Y,Z] = solve(eqn_A,[x,y,z])
which wont give a solution because it is trying to solve all equations. I'm trying to get the program to solve for example
[X,Y,Z] = solve([eqn_A(1),eqn_A(3),eqn_A(5)],[x,y,z])
which gives two solutions for x and y and one solution for z, but for all combinations of three equations stated in array eqn_A. Ideally i wouldn't like to do so using a bunch of if conditions, so i was wondering if there was a better way, as for the real problem i'm trying to solve i have 100s of equations.
Aquatris
Aquatris on 3 Aug 2018
Obivously you cannot have a variable that is both equal to 2 and equal to 5. So if you try to solve all of it it is gonna give you no answer.
For the second part it is gonna give you 2 solutions since x and y are interchangeable in equation 3 and 5. So essentially you have 2 equations 3 unknowns, underdetermined system of equations. I do not get what your problem is?
Osman Ajmal
Osman Ajmal on 3 Aug 2018
I intentionally put Z==2 and z==5 for this example. I'm looking for a way to solve for all possible solutions that satisfy three of the five equations.
the question rephrased: Is there a function in Matlab to solve for any combination of three of the five equations in eqn_A?
I realise at this scale it is easier just trying to solve for all combinations of three equations in loops, but i want to use this for an array with 100's of equations.

Sign in to comment.

Answers (1)

Eduard Reitmann
Eduard Reitmann on 3 Aug 2018
Edited: Eduard Reitmann on 3 Aug 2018
Step 1: Rewrite each equation to add to zero. For example:
  1. 5*x^2 + x*y = 4
  2. y^2 + z = 2
  3. x + 2*z = 0
becomes:
  1. 5*x^2 + x*y - 4 = 0
  2. y^2 + z - 2 = 0
  3. x + 2*z = 0
Write to function.
f = @(x,y,z) [5*x.^2+x.*y-4;
y.^2+z-2;
x+2*z];
Step 2: Iteratively solve using fminsearch.
fun = @(x) sum(f(x(1),x(2),x(3)).^2);
x0 = [0;0;0];
x = fminsearch(fun,x0)

  0 Comments

Sign in to comment.

Tags

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!