0

Suppose I have two equations with only one variable (free parameter) x and that k1 and k2 are constants. I would like to solve for:

f(x) + k1 = 0
&
g(x) + k2 = 0
...
h(x) + kn = 0

Of course there is no value of x that satisfies all of these equations. I basically would like the value of x that minimizes the output of each of these equations.

'solve' in matlab looks for an exact answer and returns an error, here's an example to demonstrate:

syms x
solution = solve(0.5*(x-k1)/sqrt(2) == 0, 0.5*(x-k2)/sqrt(2) == 0);

1 Answers1

0

You can try using Unconstrained Optimization method such as fminsearch, for example:

h=@(x) x^2;
g=@(x) x^3;
k1=2;
k2=4;
inital_guess=3;

f = @(x) sum(abs([h(x)+k1; g(x)+k2]));
[x,fval]  = fminsearch(f,inital_guess)

Note that I represent both eq in matrix form, and the minimization is by looking at the sum of their absolute values.

For the values I entered the value of x that minmize these eq is given by the output x = -1.5874

bla
  • 25,846
  • 10
  • 70
  • 101
  • Thank you. Could you clarify why you used the sum of the absolute value for the minimization? Is it because both equations are equal to zero? I'm just not sure about the answer it returns if your functions are x^2 + k1 == 0 and x^3 + k1 == 0 shouldn't the answer be some decimal and not such a large negative value? – user5091415 Jul 07 '15 at 23:34
  • you want that each eq will be as close to zero as possible (either positive or negative). So you want to treat `x^2+k1= - 0.5` similar to `x^2+k1= 0.5` . thats why you take the `abs` of each eq. The sum is to add one to the other, all `fminsearch` does is to minimize `f`, so instead of sum you can use whatever operation you want, just understand that fminsearch will need to minimize a scalar that represents your goal. I think that sum is a reasonable choice. – bla Jul 07 '15 at 23:42