Home > OS >  How translate ppval and @() in Matlab to Python equivalent?
How translate ppval and @() in Matlab to Python equivalent?

Time:05-19

I have the following code:

from scipy.interpolate import UnivariateSpline
from scipy.optimize import fmin

a = UnivariateSpline(b,c) #Python

d = fmins(@(t) -ppval(a,t), someexpression); #Matlab where fmin is equivalent to fmins

How translate it to Python3?

CodePudding user response:

@(t) -ppval(a,t) in Matlab is an anonymous function

You can denote things similarly using a lambda function in python.

By teh example here I see that the output of the UnivariateSpline is callable, then the python analogous is lambda t: -a(t).

But you will have more problem fmins is not define then you may want to check alternatives in scipy.optimize package.

CodePudding user response:

The documentation of fmin tells us that its first argument must be the function, the second argument the initial value, so it's exactly the same as in MATLAB. In that case you should be able to call

d = fmin(lambda x: -a(x), someexpression)
  • Related