I have a second-order linear ODE y'' + (w/c)^2*(cos(cos(2pix/d) + q0)*y=0. I need to get exact and numerical solution. Firstly, i tried to solve it with sympy.dsolve with this code:
c = 1
w = 1.5
d = 1
q0 = 2
wc = (w / c)**2
x = symbols('x')
y = Function('y')
equation = Eq(y(x).diff(x, x) + wc * (cos(2 * pi * x / d) + q0) * y(x), 0)
y_x = dsolve(equation)
y_x
but when compiling jupyter said: "The kernel has died. It will restart automatically". I already did 2 ODEs with this method but there weren't cos(kx) and there were initial conditions in equations I solved. I guess I have a problem with that but I don't know how to fix this.
Related
I am trying to solve a function in an annular domain that has a change of phase with respect to the angular direction of the annulus.
My attempt to solve it is the following:
import numpy as np
from scipy import integrate
def f(x0, y0):
r = np.sqrt(x0**2 + y0**2)
if r >= rIn and r <= rOut:
theta = np.arctan(y0 / x0)
R = np.sqrt((x - x0)**2 + (y - y0)**2 + z**2)
integrand = (np.exp(-1j * (k*R + theta))) / R
return integrand
else:
return 0
# Test
rIn = 0.5
rOut = 1.5
x = 1
y = 1
z = 1
k = 3.66
I = integrate.dblquad(f, -rOut, rOut, lambda x0: -rOut, lambda x0: rOut)
My problem is that I don't know how to get rid of the division by zero occuring when I evaluate theta.
Any help will be more than appreciated!
Use numpy.arctan2 instead, it will have problems only if both x and y are zero, in which case the angle is undetermined.
Also I see you that your integrand is complex, in this case you will probably have to handle real and imaginary part separately, as done here.
I'm new to numpy, and trying to implement the following equation.
The equation has two parts, and should give a final value called Sigma.
the equation is taken from the paper as below image:
image of the equation to provide the result of Sigma
I tried to implement it as below, but when running the code, the value c is giving nan
c = np.sqrt(np.log(2 / np.sqrt( 16 * delta + 1 ) -1 ))
sigma = (c + np.sqrt(np.square(c) + epsilon) ) * s / (epsilon * np.sqrt(2))
appreciate if you can advise on how to implement it in numpy
You missed a bracket in your code
c = np.sqrt(np.log(2 / (np.sqrt( 16 * delta + 1 ) -1 )))
sigma = (c + np.sqrt(np.square(c) + epsilon) ) * s / (epsilon * np.sqrt(2))
To get a valid c value, you should input delta like 0 < delta < 0.5.
You are missing a parenthesis. This is the correct formula:
c = np.sqrt(np.log(2/(np.sqrt(16*delta + 1) -1)))
Also, keep in mind that (as the paper states) this is defined only for .
i want to calculate the steady states of a set of differential equations, as you can see in the following code.
from sympy import *
from sympy import solve
from sympy import init_printing
init_printing()
X = Symbol("\X")
Y = Symbol("\Y")
Z = Symbol("\Z")
tau = Symbol(r"\tau")
Ryx = Symbol(r"\Ryx")
Rxy = Symbol(r"\Rxy")
Ry = Symbol(r"\Ry")
Rz = Symbol(r"\Rz")
Rzy = Symbol(r"\Rzy")
Rzz = Symbol(r"\Rzz")
Rxz = Symbol(r"\Rxz")
Ryz = Symbol(r"\Ryz")
#equation system
dX = X - X**2 - Ryx * Y * X
dY = Ry * ( Y - Y**2 + Rxy * X * Y + Rzy * Z * Y )
dZ = Rz * ( -Rzz * Z + Rxz * X * Z + Ryz * Y * Z )
equilibria = solve( (dX,dY,dZ),X,Y,Z )
If I copy my code in the sympy live shell the code works fine and gives me my set of solutions.
As I want to save my results in a latex file, I want to run this code on my system. Which is the following Ubuntu 12.04, python2.7.3, sympy 0.7.1.
Here my code results in the following error:
NotImplementedError: only zero-dimensional systems supported (finite number of solutions)
If I cancel out the last equation and the Z variable I get a set of solutions
equilibria = solve( (dX,dY),X,Y )
-> [(0, 0), (1, 0)]
In my opinion, the system needs to much computation time to solve the complete set.
Do you have any suggestions?
I found the module which might solve this problem
from sympy.solvers.solveset import nonlinsolve
But my version of sympy doesn't know this module.
Thank you in advance
Now I face some problem when I use scipy.integrate.ode.
I want to use spectral method (fourier transform) solve a PDE including dispersive and convection term, such as
du/dt = A * d^3 u / dx^3 + C * du/dx
Then from fourier transform this PDE will convert to a set of ODEs in complex space (uk is complex vector)
duk/dt = (A * coeff^3 + C * coeff) * uk
coeff = (2 * pi * i * k) / L
k is wavenumber, (e.g.. k = 0, 1, 2, 3, -4, -3, -2, -1)
i^2 = -1,
L is length of domain.
When I use r = ode(uODE).set_integrator('zvode', method='adams'), python will warn like:
c ZVODE-- At current T (=R1), MXSTEP (=I1) steps
taken on this call before reaching TOUT
In above message, I1 = 500
In above message, R1 = 0.2191432098050D+00
I feel it is because the time step I chosen is too large, however I cannot decrease time step as every step is time consuming for my real problem. Do I have any other way to resolve this problem?
Did you consider solving the ODEs symbolically? With Sympy you can type
import sympy as sy
sy.init_printing() # use IPython for better results
from sympy.abc import A, C, c, x, t # variables
u = sy.Function(b'u')(x,t)
eq = sy.Eq(u.diff(t), c*u)
sl1 = sy.pde.pdsolve(eq, u)
print("The solution of:")
sy.pprint(eq)
print("was determined to be:")
sy.pprint(sl1)
print("")
print("Substituting the coefficient:")
k,L = sy.symbols("k L", real=True)
coeff = (2 * sy.pi * sy.I * k) / L
cc = (A * coeff**3 + C * coeff)
sl2 = sy.simplify(sl1.replace(c, cc))
sy.pprint(sl2)
gives the following output:
The solution of:
∂
──(u(x, t)) = c⋅u(x, t)
∂t
was determined to be:
c⋅t
u(x, t) = F(x)⋅ℯ
Substituting the coefficient:
⎛ 2 2 2⎞
-2⋅ⅈ⋅π⋅k⋅t⋅⎝4⋅π ⋅A⋅k - C⋅L ⎠
──────────────────────────────
3
L
u(x, t) = F(x)⋅ℯ
Note that F(x) depends on your initial values of u(x,t=0), which you need to provide.
Use sl2.rhs.evalf() to substitute in numbers.
I have the following set of equations, and I want to solve them simultaneously for X and Y. I've been advised that I could use numpy to solve these as a system of linear equations. Is that the best option, or is there a better way?
a = (((f * X) + (f2 * X3 )) / (1 + (f * X) + (f2 * X3 ))) * i
b = ((f2 * X3 ) / (1 + (f * X) + (f2 * X3))) * i
c = ((f * X) / (1 + (j * X) + (k * Y))) * i
d = ((k * Y) / (1 + (j * X) + (k * Y))) * i
f = 0.0001
i = 0.001
j = 0.0001
k = 0.001
e = 0 = X + a + b + c
g = 0.0001 = Y + d
h = i - a
As noted by Joe, this is actually a system of nonlinear equations. You are going to need more firepower than numpy alone provides.
Solution of nonlinear equations is tricky, and the typical approach is to define an objective function
F(z) = sum( e[n]^2, n=1...13 )
where z is a vector containing a value for each of your 13 variables a,b,c,d,e,f,g,h,i,X,Y and e[n] is the amount by which each of your 13 equations is violated. For example
e[3] = (d - ((k * Y) / (1 + (j * X) + (k * Y))) * i )
Once you have that objective function, then you can apply a nonlinear solver to try to find a z for which F(z)=0. That of course corresponds to a solution to your equations.
Commonly used solvers include:
The Solver in Microsoft Excel
The python library scipy.optimize
Fitting routines in the Gnu Scientific Library
Matlab's optimization toolbox
Note that all of them will work far better if you first alter your set of equations to eliminate as many variables as practical before trying to run the solver (e.g. by substituting for k wherever it is found). The reduced dimensionality makes a big difference.