from scipy.integrate import quad
def integrand(a, b):
return a * x ** 2 + b
a = 2
b = 1
I = quad(integrand, 0, 1, args=(a,b))
I
This is my program. When I tried to run it, it was showing error :
integrand () takes 2 positional arguments but 3 were given ....
I didn't understand why it is asking for 3 arguments when there are only two variables, i.e. a and b.
Can anyone help me? Can anyone clarify my doubts?
Just replace
def integrand(a, b):
by
def integrand(x, a, b):
The problem is that in the function, you use a variable x but you do not pass the variable x as an argument to the function. 0 and 1 acts as the limits of the integral but since you are integrating w.r.t. x, you get this error.
Output
(1.6666666666666667, 1.8503717077085944e-14)
a*x**2+b This function you are using contain a variable x, in your case def integrand(a, b): does not contain a variable x which uses 0 to 1 limits of the integral you are using on I=quad(integrand,0,1,args=(a,b)).
So all you have to do is add x to your def;
def integrand(x, a, b):
return a * x ** 2 + b
a = 2
b = 1
I = quad(integrand,0,1,args=(a,b))
I
Related
Let us say I have a function called my_func(a,b,s,t). Suppose, I want a and b to be passed by value, but I want s and t to be passed by reference. As in, I want to some how pass in let us say (4,5,s',t'). The function performs computations by calling my_func(a/2,b/2,s/2,t/2). The thing is, there is a base case at the "bottom" of the recursion that gives concrete values to s and t.
Let me give a mini example:
def e_euclid(a,b,s,t):
if (a == b):
s = 4
t = -3
return a
if (a%2 == 0 and b%2 == 0):
if (s%2 == 0 and t%2 == 0):
return 2*e_euclid(a/2,b/2,s/2,t/2)
else:
return 2*e_euclid(a/2,b/2,(s+b)/2,(t-a)/2)
...
So, I would call this function as e_euclid(a,b, something, something) but then I would have to supply concrete values for s and t. Can you guys kind of see what I'm trying to do here?
Doing recursion where I return (s,t) would lead to a tough computation that I don't wish to perform, so I would like to do it this way.
Your code seems broken, already that base case (?) with a == b and s = 4 and t = -3 doesn't make sense. But see this C++ implementation and my Python translation using single-element lists instead of C++'s references:
def gcd(a, b, x=[None], y=[None]):
if b == 0:
x[0] = 1
y[0] = 0
return a
x1, y1 = [None], [None]
d = gcd(b, a % b, x1, y1)
x[0] = y1[0]
y[0] = x1[0] - y1[0] * (a // b)
return d
a, b = 123, 321
x, y = [None], [None]
print(gcd(a, b, x, y), x, y, a*x[0] + b*y[0])
Output (Try it online!):
3 [47] [-18] 3
I think you're trying to use the binary version of the algorithm, that should be doable the same way.
Python beginner here trying to learn by doing, here i have two functions 'main','area'.
the one which has two arguments should print y = math.pi *a*b and the one which does not have two arguments should print x = math.pi *a**2 in my code it is currently printing like this " First (153.93804002589985, 0.0)
Second (78.53981633974483, 62.83185307179586) " (why its printing these 0.0 and 78.53981633974483?), how to make it check that if one parameter is given do this and if two do that ?
import math
def area(a,b=0):
y = math.pi *a*b
x = math.pi *a**2
return x,y
def main():
print("First", area(7))
print("Second", area(5, 4))
main()
If I understand you correctly, you want something like this:
import math
def area(a, b=None):
if b is None:
# b not specified:
x = math.pi * a**2
else:
# b specified:
x = math.pi * a*b
return x
def main():
print("First", area(7))
print("Second", area(5, 4))
main()
Output is
First 153.93804002589985
Second 62.83185307179586
When b is not specified, it is set to None. Then you test for that in the function.
The reason it prints e.g. (153.93804002589985, 0.0) in your original example is that you return a tuple (x, y) from the function with return x,y.
Your function returns a tuple, but the caller of the functions does not know which value to use. Instead, your function should just return either one or the other value, depending on the provided inputs. Also, I would suggest using None as the default for b since 0 might be a valid value.
def area(a, b=None):
if b is None:
return math.pi * a**2
else:
return math.pi * a * b
Alternatively, you could also use a ternary ... if ... else ... for either the entire expression or just the part that is different:
def area(a, b=None):
return math.pi * a * (b if b is not None else a)
The output will then be just
First 153.93804002589985
Second 62.83185307179586
This question already has answers here:
How do I write a function that returns another function?
(5 answers)
Closed 2 years ago.
I am making a function on python3 that solves ax^2+bx+c so a quadratic equation
My code looks like this:
def quadratic(a, b, c):
return a*x**2 + b*x + c
But it wont let me do this because x is undefined. I want to take the argument x on a test code
that looks like this:
def testQuadratic(a, b, c, x):
try:
return quadratic(a, b, c)(x)
except TypeError:
return None
Can anyone tell me how I can fix this?
Thank you!!
You can make use of the fact that Python supports first-class functions, that can be passed into and returned from other functions.
def make_quadratic(a, b, c):
def f(x):
return a*(x**2) + b*x + c
return f
# You would call the returned function
my_quadratic = make_quadratic(a, b, c)
# You can then call my_quadratic(x) as you would elsewhere
Your quadratic function should... return a function!
def quadratic(a, b, c):
def calculate_quadratic(x):
return a*x**2 + b*x + c
return calculate_quadratic
It's unclear whether you intend 'solve' to mean
find a root of the quadratic equation, or
produce an output for a given value of x
Since you're taking x as an input argument, I'll assume the second option (snatchysquid gave an answer for the first option):
def quadratic(a, b, c):
return a*x**2 + b*x + c
def testQuadratic(a, b, c, x):
try:
return quadratic(a, b, c, x)
except TypeError:
return None
of course unless you need def quadratic for some other reason, then it would be simpler to use:
def testQuadratic(a, b, c, x):
try:
return a*x**2 + b*x + c
except TypeError:
return None
You still might want to handle other errors besides TypeError.
You can read about quadratic equations here and in wikipedia.
There is a formula for solving quadratic equations so you can use this code:
import math
def quadratic_equation(a, b, c):
"""
:params a,b,c: the coefficients of the equation ax^2+bx+c=0
:return: a tuple (of len 2) with the solutions to the equation
:rtype: tuple
"""
discriminant = b ** 2 - (4 * a * c)
if discriminant < 0:
return None, None
if discriminant == 0:
return -b / (2 * a), None
return (-b + math.sqrt(discriminant)) / (2 * a), (-b - math.sqrt(discriminant)) / (2 * a)
The reason your code doesn't work is that python does not understand "missing variables", and thinks that x is some defined variable, for example x = 2, but you use the math notation which python does not understand.
I'm curious if it is possible to avoid lambda expressions in the following case.
For example using lambda expression I can simple define a function that returns a lambda exp.
def square_fun(a,b,c):
return lambda x: a*x**2 + b*x + c
After we can call it using:
f = square_fun(1,2,3)
f(1) # here x = 1
How can I get the same behaviour avoiding lambda expression?
for example, square_fun must return another function
def square_fun(a,b,c):
return f...
In python you can define a function inside another function, so the following snippet should give you the same behavior:
def square_fun(a, b, c):
def result(x):
return a*x**2 + b*x + c
return result
f = square_fun(1, 2, 3)
print(f(1))
# Should print 6
So, I'll try to explain what's going on here:
In the line f = square_fun(1, 2, 3), f will actually be mapped to the internal function (aka. result)
Please note that return result does not have the () at the end, hence the function is not called yet
The function gets called in print(f(1)) and the variable x takes 1 as its value
Finally the result function returns the computed value using x, and a, b and c (from square_fun(1, 2, 3))
You can find additional info here => https://www.learnpython.org/en/Closures
You can also use functools.partial:
from functools import partial
def square_fun(x, a, b, c):
return a * x ** 2 + b * x + c
f = partial(square_fun, a=1, b=2, c=3)
print(f(1))
# should print 6
from math import cos
def diff1(f, x): #approximates first derivative#
h = 10**(-10)
return (f(x+h) - f(x))/h
def newtonFunction(f,x):
return x - f(x)/float(diff1(f,x))
y = cos
x0 = 3
epsilon = .001
print diff1(newtonFunction(y,x0), x0)
This is just a portion of the code, but I want to calculate diff1(f,x) where f is newtonFunction but uses the argument f passed to NewtonMinimum. diff1 already takes f and x as an argument and I get an error saying it expects two arguments for newtonFunction.
I think what you're looking for is functools.partial.
The problem is that f is not newtonFunction, rather it's the value returned by newtonFunction(y,x0). In this example that's a floating point number, hence the 'float' object not callable.
If you want to pass a function as a parameter to another function, you need to use just its name:
diff1(newtonFunction, x0)
Note also that you will then have another problem: in diff1 you're calling f with only one parameter, but newtonFunction takes two parameters.
In diff1, you are missing a * in f(x+h) and f(x) and in newtonFunction. You are also leaving y as a built-in function, so I assumed you wanted the cos of x0. Here is your edited code:
from math import cos
def diff1(f, x): #approximates first derivative#
h = 10**(-10)
return (f*(x+h) - f*(x))/h
def newtonFunction(f,x):
return x - f*(x)/float(diff1(f,x))
y = cos
x0 = 3
epsilon = .001
print diff1(newtonFunction(y(x0),x0), x0)