1

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?

HenDoNR
  • 79
  • 1
  • 12

2 Answers2

3

@(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.

Bob
  • 13,867
  • 1
  • 5
  • 27
2

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)
flawr
  • 10,814
  • 3
  • 41
  • 71
  • How about two arrays a and b for ppval(a, b)? – HenDoNR May 18 '22 at 12:21
  • @HenDoNR What exactly do those arrays mean? Have you consulted the documentation? In MATLAB the first argument represents the piecewise polynomial and you defined that to be `a` in python. – flawr May 18 '22 at 12:26