How to write hypergeometric function 1F1 in sympy?
I have the following code and I would like to correctly write the hypergeometric function 1F1 into the expression y. Could you explain how to do it?
import sympy as sp
import scipy as sc
import numpy as np
def Psii():
r=sp.symbols('r')
n = 2
y = sp.functions.special.hyper.hyper([1,1],[1],d) / n ** 2
yprime = sp.diff(y,r)
f = sp.utilities.lambdify(r, yprime, "numpy")
return y, yprime, f(3)
print(Psii())
Related
How to Interpolate for every 100lbs of Pressure_alt based on GW(grass weight) column for all value of grouped OAT(temperature).I am new to python, help me to find solution for this.
import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np
x = np.arange(17402,17330)
y = x**100
temp = interpolate.interp1d(x, y)
xnew = np.arange(17300, 17400, 100)
ynew = temp(xnew)
import numpy as np from math import * from matplotlib.pyplot import* def f(x,y) : return np.exp(-2*x)((sqrt(3)/6)*sin(2*sqrt(3)*x)+(1/2)*cos(2*sqrt(3)*x)) dx=0.1 a=0 b=6 N= int((b-a)/dx) x=np.linspace(a,b,N+1) y=np.zeros(N+1) y[0] = 6 y[1] = y[0] for i in range (N-1): y[i+2]=(2-4*dx)*y[i+1]+(-1+4*dx-16*dx**2)*y[i] Y2 = f(x,y) Err = abs (Y2-y) plot(x,y,'r',x,Y2,'b') show () plot(x,Err) show()
how to fix that? i kept get TypeError: only size-1 arrays can be converted to Python scalars
First of all don't give code examples like that please.
I cleaned up your code and I think you can use this:
import numpy as np
from math import *
from matplotlib.pyplot import*
def f(x,y) :
return np.exp(-2*x)*((np.sqrt(3)/6)*np.sin(2*np.sqrt(3)*x)+(1/2)*np.cos(2*np.sqrt(3)*x))
dx=0.1
a=0
b=6
N= int((b-a)/dx)
x=np.linspace(a,b,N+1)
y=np.zeros(N+1)
y[0] = 6
y[1] = y[0]
for i in range (N-1):
y[i+2]=(2-4*dx)*y[i+1]+(-1+4*dx-16*dx**2)*y[i]
Y2 = f(x,y)
Err = abs (Y2-y)
plot(x,y,'r',x,Y2,'b')
plot(x,Err)
I have a function f(x,y) where t is a parameter. I'm trying to plot the function where t = 1 for x and y values ranging from -5 to 5. The plot doesn't render.
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
C = sv.CoordSys3D("")
x, y, z = C.base_scalars()
t = sp.symbols("t")
f = sp.sin(2*sp.pi*t)*sp.exp(-(x-3*sp.sin(sp.pi*t))**2 -(y-3*sp.cos(sp.pi*t))**2)
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(projection='3d')
X = np.linspace(-5,5,100)
Y = np.linspace(-5,5,100)
xvals, yvals = np.meshgrid(X,Y)
zvals = sp.lambdify((x,y),f.subs(t,1),"numpy")(xvals,yvals)
ax.plot_surface(xvals,yvals,zvals)
plt.show()
I get an error 'int' object has no attribute 'ndim' which I don't know how to solve.
The problem is that when you execute f.subs(t,1) it returns a number (zero in this case). So, f=0 is the expression that you are going to lambdify. Let's see the function generated by lambdify:
import inspect
print(inspect.getsource(sp.lambdify((x,y),f.subs(t,1),"numpy")))
# def _lambdifygenerated(Dummy_25, Dummy_24):
# return 0
So, no matter the values and shape of xvals and yvals, that numerical function will always return 0, which is an integer number.
However, ax.plot_surface requires zvals to have the same shape as xvals or yval. Luckily, we can easily fix that with a simple if statement:
import sympy as sp
import sympy.vector as sv
import numpy as np
import matplotlib.pyplot as plt
C = sv.CoordSys3D("")
x, y, z = C.base_scalars()
t = sp.symbols("t")
f = sp.sin(2*sp.pi*t)*sp.exp(-(x-3*sp.sin(sp.pi*t))**2 -(y-3*sp.cos(sp.pi*t))**2)
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(projection='3d')
X = np.linspace(-5,5,100)
Y = np.linspace(-5,5,100)
xvals, yvals = np.meshgrid(X,Y)
zvals = sp.lambdify((x,y),f.subs(t,1),"numpy")(xvals,yvals)
# if zvals is just a number, create a proper matrix
if not isinstance(zvals, np.ndarray):
zvals = zvals * np.ones_like(xvals)
ax.plot_surface(xvals,yvals,zvals)
plt.show()
The fact that this doesn't render is bug in lambdify that it doesn't work well for constant expressions.
Your real problem though is that the expression you are trying to plot is just zero:
In [5]: f
Out[5]:
2 2
- (x_ - 3⋅sin(π⋅t)) - (y_ - 3⋅cos(π⋅t))
ℯ ⋅sin(2⋅π⋅t)
In [6]: f.subs(t, 1)
Out[6]: 0
The following code fails when find_root is decorated with nb.jit. This is a toy example, but the idea is to have the ability to find the root of a scalar function (or potentially a multivariate function using root) for an array of values and store them in a numpy array.
Error message: TypingError: cannot determine Numba type of <class 'function'>
import numba as nb
import numpy as np
from scipy.optimize import root_scalar
a = 3.0
b = 1.0
c = -10.5
#nb.jit(nopython=True)
def f(x):
return a*x**2 + b*x + c
#nb.jit(nopython=True)
def fprime(x):
return 2*a*x + b
#nb.jit(nopython=True)
def fprime2(x):
return 2*a
#nb.jit(nopython=True) # <-- Commenting this line makes the code work but it is slow
def findroot(arr):
for i in range(len(arr)):
arr[i] = root_scalar(f, fprime=fprime, fprime2=fprime2, x0=0).root
if __name__ == '__main__':
arr = np.zeros(20, np.float)
import timeit
start = timeit.time.process_time()
findroot(arr)
end = timeit.time.process_time()
print(end - start)
this is a 2nd ODE with boundaries that I'm trying to solve, but I can't figure out. It is a heat transfer problem. If you have insights, it would be very appreciable.
Basically, boundary problem, but with different location. One at 0 and the other at the end.
T(0) unknown, y'(0) is a func. of T(0.06), but T(0.06) is given.
The key is how to connect the known value, T(0.06)=300, to solve the problem.
y''=0, y(0)=t0, y'(0)=(4.82e-08*T0**4-208.0)/1.2, y(0.06)=300
I tried this code, but no luck.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.integrate import odeint
def dU_dx(U, x):
return [U[1], 0]
#set initial values
y0 = T0
z0 = (4.82e-08*T0**4-208.0)/1.2
U0 = [y0, z0]
yL = 300 # how do I use this boundary condition?
L=0.006
#solve 2nd ode
xs = np.linspace(0, L, 100)
Us = odeint(dU_dx, U0, xs)
ys = Us[:,0]
plt.xlabel("x")
plt.ylabel("T")
plt.title("2nd ODE")
plt.plot(xs,ys);
from scipy.interpolate import interp1d
g = interp1d(xs,ys)
T=g(0)
print("Temp(at 0)=",T)
This would definitely not an elegant code, but this is the way I get the answer I want. But please, feel free to pose if you know better code.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.integrate import odeint
def dU_dx(U, x):
return [U[1], 0]
#Assume initial value
T0=292.75
y0 = T0
z0 = (4.82e-08*T0**4-208.0)/1.2
U0 = [y0, z0]
L=0.06
xs = np.linspace(0, L, 100)
Us = odeint(dU_dx, U0, xs)
ys = Us[:,0]
plt.xlabel("x")
plt.ylabel("T")
plt.title("2nd ODE")
plt.plot(xs,ys);
from scipy.interpolate import interp1d
g = interp1d(xs,ys)
#Repeat until see T(0.06)=300
T0=g(L)
print("Temp(0)=",T0)