edit python function

Asked

Viewed 45 times

-3

I’m writing an algorithm that calculates the average of continuous variables and I pass to it a function with the specific function:

def media_variavel_continua(func, ini, fim):
    return quad(func, ini, fim)


resp = media_variavel_continua(lambda x: (x**2)/3, 1, 3)

It turns out that the average of a continuous variable is the function itself times 'x', so I wanted to be able to rewrite in media_variavel_continua() the passed function.

In the example above the function (x 2)/3 at the end would have to be x*(x 2)/3.

Is there any way to do that?

  • In that case I believe you can try to make a new lambda x: x*func(x). But I believe that modifying a lambda generically speaking is more complicated.

  • @Naslausky that worked, thank you!

1 answer

0


As commented, you do not need edit function, just create a new one that does the proposed:

def media_variavel_continua(func, ini, fim):
    return quad(lambda x: x*func(x), ini, fim)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.