I am trying to solve a differential equation contains complex numbers. These complex numbers are constants A and B.
import numpy as np
import matplotlib.pyplot as plt
# intinialize values
x0=0
y0=1
xf=2
h=0.1
n=200
y=np.zeros([n])
t=np.zeros([n])
y[0]=y0
t[0]=x0
t=np.linspace(x0,xf,n)
gamma=1.0
width=1.0
v_g=1.0
L=1.0
k=1
r=1.0
C=1j
A=((8*np.pi)**(1.0/4.0)/(np.sqrt(width*L)))*(-
C/2*np.pi)*np.sqrt(gamma*v_g*L/2)
B=(k**2)+C*k*r-((width**2)*(r**2)/4)+r*v_g*t*(width**2/2)-v_g**2*t**2*
(width**2/4)+k**2/width**2
for i in range(1,n):
t[i]=t[i-1]+h
slope=A*np.exp(B)-(1/2)*(y[i-1])
y[i]=y[i-1]+h*slope
plt.plot(t,y,'-')
Can anyone help me to explain the problem and give a suggestion to solve the problem?
Related
I am trying to solve a differential system equation using solve_ivp from scipy.integrate, but what I have readen in the web https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html
is when the boundaries conditions are given in the initial point t=t_0. I Would like to know how to modify it, to be more precise, I would like to solve the next problem
the coefficients are constants, and the U_i's only depent of x, L>0, and in this case I don't know how to specify these kind of boundary conditions, what I have done
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
# Coefficients of matrix
u1=2
u2=2
k12=3
k13=2
k14=10
k21=6
k23=27
k24=13
k31=2
k32=15
k34=31
k41=37
k42=10
k43=1j
zeta1=1
zeta2=1
nu=1 # Nu parameter
# Boundaries condition
L=10
interval=[-L/2,L/2]
boundaries_conditions=[0,0,0,0]
### Definition of matrix of coeficients
Matrix_A=np.array([[-u1, k12, k13,k14],
[k21, u1, k23, k24],
[k31, k32, -u2, k34],
[k41, k42, k43, u2]])*1j
# Definition of the homogeneous system
def hom_system(x , U, u_1, u_2, u_3, u_4):
U1, U2, U3, U4 = U,U,U,U
line1=np.dot(Matrix_A[0],np.array([U1,U2,U3,U4]))
line2=np.dot(Matrix_A[1],np.array([U1,U2,U3,U4]))
line3=np.dot(Matrix_A[2],np.array([U1,U2,U3,U4]))
line4=np.dot(Matrix_A[3],np.array([U1,U2,U3,U4]))
return [line1,line2,line3,line4]
If someone could give some hints I would appreciate very much, thanks.
I have wrote this code to solve an equation , I know the behavior of this function has very rapid oscillations, when I RUN it gives bogus values for some "m[x]" and some "t"'s, with this error:
C:\Users\dani\anaconda3\lib\site-packages\scipy\integrate\odepack.py:247: ODEintWarning: Excess work done on this call (perhaps wrong Dfun type). Run with full_output = 1 to get quantitative information.
warnings.warn(warning_msg, ODEintWarning)
I don't know what is the problem.
how can I get correct results? or at least as accurate as possible? or maybe I should rewrite the code in another form?
thank you.
import scipy as sio
import numpy as np
import mpmath as mp
import scipy.integrate as spi
import matplotlib.pyplot as plt
import time
from scipy.integrate import quad
initial_value=np.logspace(24,27,100)
t=np.logspace(-20,6,100)
m=np.logspace(0,6,100)
start_time=time.perf_counter()
phi_m={}
phi_m_prime={}
phi=[]
phi_prime=[]
j=0
i=np.pi*2.435*initial_value[0]
while i<(np.pi*(2.435*10**(27))):
i=np.pi*2.435*initial_value[j]
phi=[]
phi_prime=[]
for x in range (len(m)):
def dzdt(z,T):
return [z[1], -3*1.4441*(10**(-6))*m[x]*np.sqrt(0.69)*(mp.coth(1.5*np.sqrt(0.69)*(10**(-6))*1.4441*m[x]*T))*z[1] - z[0]]
z0 = [i,0]
ts = t/m[x]
zs = spi.odeint(dzdt, z0, ts)
phi.append(zs[99,0])
phi_prime.append(zs[99,1])
phi_m[j]=phi
phi_m_prime[j]=phi_prime
j+=1
end_time=time.perf_counter()
print(end_time-start_time,"seconds")
I am supposed to use python to solve a Matrix A^1000. I have tried numpy's built in matrix_power function to compute the output, but when I try to compute the result step by step according to the formula P*(D^1000) *P^-1 , I get an incomplete result. I am trying to figure out if my precision options are just too tight or if I am doing something else wrong completely
I have tried the matrix_power function to definitely get the result I want. But I need to be able to show how the calculation is done, and when I do so, I get an incomplete result
import numpy as np
np.set_printoptions(precision=6) # set the precision of the output
np.set_printoptions(suppress=True) # suppress the use of scientific notation
from numpy import diag, allclose, corrcoef
from numpy.random import randint, randn
from numpy.linalg import eig, matrix_rank, inv, cholesky, qr, norm, matrix_power
from sympy import Matrix, init_printing, matrix2numpy
A = np.array([[0.9,0.15,0.25],[0.075,0.8,0.25],[0.025,0.05,0.5]])
A
#python way
A_1000 = matrix_power(A,1000)
A_1000
D , P = eig(A)
P * np.diag(D**1000) * np.linalg.inv(p)
I have a function that takes two m-dimensional arrays does some calculation with them (here it is very simplified) and returns one dimensional array. Also I have m-dimensional measurement data and would like to optimise those two arrays to fit the measurements. This worked fine with one arrays. I can just simply not get it to work with two arrays (or more). it always throws:
TypeError: Improper input: N=40 must not exceed M=20
Here is my Code. Thank you very much if anyone can help!
import numpy as np
from scipy import optimize
data=[np.arange(0,20.0,1),np.array([-52.368, 32.221, 40.102, 48.088, 73.106, 50.807, 52.235, 76.933, 65.737, 34.772, 94.376, 123.366, 92.71, 72.25, 165.051, 91.501, 118.92, 100.936, 56.747, 159.034])]
def line(m,b):
return m*b
guessm = np.ones(20) #initial guessed values for m
guessb = np.ones(20) #initial guesses values for b
guess = np.append(guessm,guessb)
errfunc= lambda p,y: (y-line(p[:20],p[20:]))
parameter, sucess = optimize.leastsq(errfunc, guess, args=(data[1]))
print(parameter)
plt.plot(data[0],d[1],'o')
plt.plot(data[0],line(parameter[0],parameter[1]))
plt.show()
If you want to fit a line, you should give the slope and intercept - two parameters, not 40. I suspect this is what you try to do:
import matplotlib.pyplot as plt
import numpy as np
from scipy import optimize
data=[np.arange(0,20.0,1),np.array([-52.368, 32.221, 40.102, 48.088, 73.106, 50.807, 52.235, 76.933, 65.737, 34.772, 94.376, 123.366, 92.71, 72.25, 165.051, 91.501, 118.92, 100.936, 56.747, 159.034])]
def line(m,b):
return np.arange(0, 20, 1)*m + b
guess = np.ones(2)
errfunc= lambda p,y: (y-line(p[0],p[1]))
parameter, sucess = optimize.leastsq(errfunc, guess, args=(data[1]))
print(parameter)
plt.plot(data[0],data[1],'o')
plt.plot(data[0],line(parameter[0],parameter[1]))
plt.show()
Here is my first steps within the NumPy world.
As a matter of fact the target is plotting below 2-D function as a 3-D mesh:
N = \frac{n}{2\sigma\sqrt{\pi}}\exp^{-\frac{n^{2}x^{2}}{4\sigma^{2}}}
That could been done as a piece a cake in Matlab with below snippet:
[x,n] = meshgrid(0:0.1:20, 1:1:100);
mu = 0;
sigma = sqrt(2)./n;
f = normcdf(x,mu,sigma);
mesh(x,n,f);
But the bloody result is ugly enough to drive me trying Python capabilities to generate scientific plots.
I searched something and found that the primary steps to hit above mark in Pyhton might be acquired by below snippet:
from matplotlib.patches import Polygon
import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
sigma = 1
def integrand(x,n):
return (n/(2*sigma*np.sqrt(np.pi)))*np.exp(-(n**2*x**2)/(4*sigma**2))
t = np.linespace(0, 20, 0.01)
n = np.linespace(1, 100, 1)
lower_bound = -100000000000000000000 #-inf
upper_bound = t
tt, nn = np.meshgrid(t,n)
real_integral = quad(integrand(tt,nn), lower_bound, upper_bound)
Axes3D.plot_trisurf(real_integral, tt,nn)
Edit: With due attention to more investigations on Greg's advices, above code is the most updated snippet.
Here is the generated exception:
RuntimeError: infinity comparisons don't work for you
It is seemingly referring to the quad call...
Would you please helping me to handle this integrating-plotting problem?!...
Best
Just a few hints to get you in the right direction.
numpy.meshgrid can do the same as MatLABs function:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html
When you have x and n you can do math just like in matlab:
sigma = numpy.sqrt(2)/n
(in python multiplication/division is default index by index - no dot needed)
scipy has a lot more advanced functions, see for example How to calculate cumulative normal distribution in Python for a 1D case.
For plotting you can use matplotlibs pcolormesh:
import matplotlib.pyplot as plt
plt.pcolormesh(x,n,real_integral)
Hope this helps until someone can give you a more detailed answer.