0

everyone

Is it possible to solve the following like:

x = np.matrix([[8.3,-20.6],[-20.6,65.8]])

y is a function of P:

y = lambda P: np.matrix([[0.02P,-0.02P], [-0.02P,0.04P]])

I want to find the P value given conditions that:

P>0, det(x-y)==0;

Is there any handy way for this?

Thank you very much! Shawn

Darth BEHFANS
  • 409
  • 6
  • 10

1 Answers1

1

If you don't mind using an additional package, scipy.optimize has a number of methods that would be perfect. Otherwise, you could implement your own zero finding function.

If you want to go the scipy route:

1) Define your problem as a function that takes your unknown parameter (P) as its first argument, and returns the value you want to minimize:

def zerofn(P, x, y):
    return np.linalg.det(x - y(P))

2) Optimize this function, using scipy. The example here uses a simple Newton-Raphson zero finder, but there are many other options which you might want to use to specify parameter bounds (e.g. P > 0).

import scipy.optimize as opt

opt.newton(zerofn, x0=1, args=(x, y))

>> 160.25865914054651

The result of this zero finder is your optimized value of P.

oscarbranson
  • 3,877
  • 1
  • 13
  • 15