python integration issue, the function with exponential - python

I would like to integrate the function e^(-x**2/2) by simpson rule
but it is keep having an error and I don't know what is the problem.
a=eval(input('a:'))
b=eval(input('b:'))
n=eval(input('n:'))
def f(x):
e**(-x**2/2)
h=(b-a)/n
s= f(a)+f(b)
def simpson_rule(f(x),a,b,n):
#Approximation by Simpson's rule
c=(a+b)/2.0
h=abs(b-a)/2.0
return h*(f(a)+4.0*f(c)+f(b))/3.0
def simpson_rule(f(x),a,b,n):
"""Approximates the definite integral of f from a to b by the composite Simpson's rule, using n subintervals"""
for i in range (1,n,2):
s+=4*f(a+i*h)
for i in range(2,n-1,2):
s+=2*f(a+i*h)
return s*h/3
print simpson_rule(f(x),a,b,n)

You define 2 integration routines with the same name. Which one do you expect to run if you call simpson_rule()?
The first one is the special case where n=1. You might rename it accordingly.
Second, your call is print simpson_rule(f(x),a,b,n) but you only need to hand over f() to the function, like this print simpson_rule(f,a,b,n).
You can see that f is a function, and f() is a function return value:
def f(x):
return x + 13
f
<function f at 0x0000000002253D68>
f(5)
18
Try it out and if you still have errors please post the error message(s).

Related

Pass function that takes kwargs to solve_ivp

For a class, I am using solve_ivp to solve a differential equation function that I customize using keyword arguments. Here is my differential equation function integrate and the function sineCurrent I pass to it to help generate the differential equation. I already know that calling sineCurrent within integrate works. My issue arises when I try to pass integrate and by extension sineCurrent to solve_ivp. My class requires that they all remain separate functions.
def integrate(time,**kwargs):
func = kwargs['current']
V = kwargs['voltage']
Cm = 0.2 # membrane capacitance in nF
R = 100 # membrane resistance in mega-ohms
V_rest = -60 # resting membrane voltage in mV
I = func(time,**kwargs) # passing arguments current input function
s = I*R
dVdt = (V_rest-V+s)/(Cm*R)
return dVdt
def sineCurrent(time,**kwargs):
# Passing frequency and current arguments, setting defaults to f=0.5 kHz and I=2 nA
defaultKwargs = { 'freq': float("0.5"), 'Imax': float("2"),}
kwargs = { **defaultKwargs, **kwargs }
freq = kwargs['freq']
Imax = kwargs['Imax']
# Error message if time vector not included
if len(time) == 0:
return "sineCurrent requires at least one input parameter, time"
# Calculating sineCurrent
I = Imax*np.sin(2*pi*freq*time)
return I
I tried several methods to pass integrate to solve_ivp. First, I tried using the basicsolve_ivp syntax.
t = np.arange(-10,100,0.1)
V_init= np.array([50])
soln = solve_ivp(integrate,t,current=sineCurrent,[t[0], t[-1]],V_init)
This yielded the error SyntaxError: positional argument follows keyword argument. Next I tried using a lambda function.
t = np.arange(-10,100,0.1)
V_init= np.array([50])
soln = solve_ivp(integrate=lambda t,current=sineCurrent: [t[0], t[-1]],V_init)
This led to the same error message as before. So I moved the keyword arguments to the end.
t = np.arange(-10,100,0.1)
V_init= np.array([50])
soln = solve_ivp(integrate,[t[0], t[-1]],V_init,args=(t,current=sineCurrent,voltage=V_init))
This led to the error message SyntaxError: invalid syntax. I am at a loss as to what to try next. I know that my integrate function can produce an np.ndarray when arguments are passed to it properly, shown below, but I cannot figure out how to pass integrate to solve_ivp.
dVdt = integrate(t,current=sineCurrent,voltage=V_init)

Cost function using absolute value and division by decision variables

I am trying to implement a cost function in a pydrake Mathematical program, however I encounter problems whenever I try to divide by a decision variable and use the abs(). A shortened version of my attempted implementation is as follows, I tried to include only what I think may be relevant.
T = 50
na = 3
nq = 5
prog = MathematicalProgram()
h = prog.NewContinuousVariables(rows=T, cols=1, name='h')
qd = prog.NewContinuousVariables(rows=T+1, cols=nq, name='qd')
d = prog.NewContinuousVariables(1, name='d')
u = prog.NewContinuousVariables(rows=T, cols=na, name='u')
def energyCost(vars):
assert vars.size == 2*na + 1 + 1
split_at = [na, 2*na, 2*na + 1]
qd, u, h, d = np.split(vars, split_at)
return np.abs([qd.dot(u)*h/d])
for t in range(T):
vars = np.concatenate((qd[t, 2:], u[t,:], h[t], d))
prog.AddCost(energyCost, vars=vars)
initial_guess = np.empty(prog.num_vars())
solver = SnoptSolver()
result = solver.Solve(prog, initial_guess)
The error I am getting is:
RuntimeError Traceback (most recent call last)
<ipython-input-55-111da18cdce0> in <module>()
22 initial_guess = np.empty(prog.num_vars())
23 solver = SnoptSolver()
---> 24 result = solver.Solve(prog, initial_guess)
25 print(f'Solution found? {result.is_success()}.')
RuntimeError: PyFunctionCost: Output must be of .ndim = 0 (scalar) and .size = 1. Got .ndim = 2 and .size = 1 instead.
To the best of my knowledge the problem is the dimensions of the output, however I am unsure of how to proceed. I spent quite some time trying to fix this, but with little success. I also tried changing np.abs to pydrake.math.abs, but then I got the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-56-c0c2f008616b> in <module>()
22 initial_guess = np.empty(prog.num_vars())
23 solver = SnoptSolver()
---> 24 result = solver.Solve(prog, initial_guess)
25 print(f'Solution found? {result.is_success()}.')
<ipython-input-56-c0c2f008616b> in energyCost(vars)
14 split_at = [na, 2*na, 2*na + 1]
15 qd, u, h, d = np.split(vars, split_at)
---> 16 return pydrake.math.abs([qd.dot(u)*h/d])
17
18 for t in range(T):
TypeError: abs(): incompatible function arguments. The following argument types are supported:
1. (arg0: float) -> float
2. (arg0: pydrake.autodiffutils.AutoDiffXd) -> pydrake.autodiffutils.AutoDiffXd
3. (arg0: pydrake.symbolic.Expression) -> pydrake.symbolic.Expression
Invoked with: [array([<AutoDiffXd 1.691961398933386e-257 nderiv=8>], dtype=object)]
Any help would be greatly appreciated, thanks!
BTW, as Tobia has mentioned, dividing a decision variable in the cost function could be problematic. There are two approaches to avoid the problem
Impose a bound on your decision variable, and 0 is not included in this bound. For example, say you want to optimize
min f(x) / y
If you can impose a bound that y > 1, then SNOPT will not try to use y=0, thus you avoid the division by zero problem.
One trick is to introduce another variable as the result of division, and then minimize this variable.
For example, say you want to optimize
min f(x) / y
You could introduce a slack variable z = f(x) / y. And formulate this problem as
min z
s.t f(x) - y * z = 0
Some observations:
The kind of cost function you are trying to use does not need the use of a python function to be enforced. You can just say (even though it would raise other errors as is) prog.AddCost(np.abs([qd[t, 2:].dot(u[t,:])*h[t]/d])).
The argument of prog.AddCost must be a Drake scalar expression. So be sure that your numpy matrix multiplications return a scalar. In the case above they return a (1,1) numpy array.
To minimize the absolute value, you need something a little more sophisticated than that. In the current form you are passing a nondifferentiable objective function: solvers do not quite like that. Say you want to minimize abs(x). A standard trick in optimization is to add an extra (slack) variable, say s, and add the constraints s >= x, s >= -x, and then minimize s itself. All these constraints and this objective are differentiable and linear.
Regarding the division of the objective by an optimization variable. Whenever you can, you should avoid that. For example (I'm 90% sure) that solvers like SNOPT or IPOPT set the initial guess to zero if you do not provide one. This implies that, if you do not provide a custom initial guess, at the first evaluation of the constraints, the solver will have a division by zero and it'll crash.

Question about mathematical integration for a differential equation

I'm new to coding, so have patience please. I have an exercise where the aim is to solve a differential equation, where there is a function that has an integral of the form dx cos(x)**2 between 0 and t.
I've tried defining a function, then using the definition to attempt to get a final value at the end of the programme.
import numpy
from scipy import integrate
def dydt(C,y,t):
return -C*y
def f_2(x):
return numpy.sin(x)**2
def C(t):
return 1+ integrate.quad(f_2,0,t)
t=numpy.linspace(0,10,100)
y=integrate.odeint(dydt,[1.0],t)
This is the error I'm getting: TypeError: dydt() missing 1 required positional argument: 't' --> I know I have clearly defined my t, so what am I doing wrong? Cheers!
dydt(C,y,t) should be dydt(C,y)
import numpy
from scipy import integrate
def dydt(C,y):
return -C*y
def f_2(x):
return numpy.sin(x)**2
def C(t):
return 1+ integrate.quad(f_2,0,t)
t=numpy.linspace(0,10,100)
y=integrate.odeint(dydt,[1.0],t) # works fine
print(y)

Python Updating Source Code wrt Output of a Function

As example, my first module is
from sympy import *
x,y=symbols('x y')
def A():
equation=2*x+y
return equation
print(A())
the output is
2*x + y
my second module is
def B(x,y):
equation=2*x + y
return equation
I have to copy past the output of first module to second one each time from the terminal. Is there a way to pass this output automatically to the source code of B(x,y)?
Thank you for your attention

Dynamic Semantic errors in Python

i came across this as an interview question. This question seemed interesting. So, i am posting it here.
Consider the operation which gives semantic error like division by zero. By default, python compiler gives output like "Invalid Operation" or something. Can we control the output that is given out by Python compiler, like print some other error message, skip that division by zero operation, and carry on with rest of the instructions?
And also, how can i evaluate the cost of run-time semantic checks?
There are many python experts here. I am hoping someone will throw some light on this. Thanks in advance.
Can we control the output that is given out by Python compiler, like print some other error message, skip that division by zero operation, and carry on with rest of the instructions?
No, you cannot. You can manually wrap every dangerous command with a try...except block, but I'm assuming you're talking about an automatic recovery to specific lines within a try...except block, or even completely automatically.
By the time the error has fallen through such that sys.excepthook is called, or whatever outer scope if you catch it early, the inner scopes are gone. You can change line numbers with sys.settrace in CPython although that is only an implementation detail, but since the outer scopes are gone there is no reliable recorvery mechanism.
If you try to use the humorous goto April fools module (that uses the method I just described) to jump blocks even within a file:
from goto import goto, label
try:
1 / 0
label .foo
print("recovered")
except:
goto .foo
you get an error:
Traceback (most recent call last):
File "rcv.py", line 9, in <module>
goto .foo
File "rcv.py", line 9, in <module>
goto .foo
File "/home/joshua/src/goto-1.0/goto.py", line 272, in _trace
frame.f_lineno = targetLine
ValueError: can't jump into the middle of a block
so I'm pretty certain it's impossible.
And also, how can i evaluate the cost of run-time semantic checks?
I don't know what that is, but you're probably looking for a line_profiler:
import random
from line_profiler import LineProfiler
profiler = LineProfiler()
def profile(function):
profiler.add_function(function)
return function
#profile
def foo(a, b, c):
if not isinstance(a, int):
raise TypeError("Is this what you mean by a 'run-time semantic check'?")
d = b * c
d /= a
return d**a
profiler.enable()
for _ in range(10000):
try:
foo(random.choice([2, 4, 2, 5, 2, 3, "dsd"]), 4, 2)
except TypeError:
pass
profiler.print_stats()
output:
Timer unit: 1e-06 s
File: rcv.py
Function: foo at line 11
Total time: 0.095197 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
11 #profile
12 def foo(a, b, c):
13 10000 29767 3.0 31.3 if not isinstance(a, int):
14 1361 4891 3.6 5.1 raise TypeError("Is this what you mean by a 'run-time semantic check'?")
15
16 8639 20192 2.3 21.2 d = b * c
17 8639 20351 2.4 21.4 d /= a
18
19 8639 19996 2.3 21.0 return d**a
So the "run-time semantic check", in this case would be taking 36.4% of the time of running foo.
If you want to time specific blocks manually that are larger than you'd use timeit on but smaller than you'd want for a profiler, instead of using two time.time() calls (which is quite an inaccurate method) I suggest Steven D'Aprano's Stopwatch context manager.
I would just use an exception, this example is using python 3. For Python 2, simple remove the annotations after the function parameters. So you function signature would look like this -> f(a,b):
def f(a: int, b: int):
"""
#param a:
#param b:
"""
try:
c = a / b
print(c)
except ZeroDivisionError:
print("You idiot, you can't do that ! :P")
if __name__ == '__main__':
f(1, 0)
>>> from cheese import f
>>> f(0, 0)
You idiot, you can't do that ! :P
>>> f(0, 1)
0.0
>>> f(1, 0)
You idiot, you can't do that ! :P
>>> f(1, 1)
1.0
This is an example of how you could catch Zero Division, by making an exception case using ZeroDivisionError.
I won't go into any specific tools for making loggers, but you can indeed understand the costs associated with this kind of checking. You can put a start = time.time() at the start of the function and end = time.time() at the end. If you take the difference, you will get the execution time in seconds.
I hope that helps.

Categories