In the following code i am trying to find value of Teq inside the function B(x,Teq).
I just have to note that the code is correct, the whole for loops and etc. But I just added the part of solve equation and it does not works just shows me [] [] [] [] [] [] [] ....
I do not understand whay I ma getting this.
how to do that.
any help please?
dfimppara = pd.read_csv('C:/Users....csv', sep=",")
dfimporto = pd.read_csv('C:/Users....csv', sep=",")
dfpara=dfimppara.values
dforto=dfimporto.values
Tpara=dfpara[0,4:len(dfpara[0,:])]
Torto=dforto[0,4:len(dforto[0,:])]
Jup = dfpara[:,1]
Jlw = dfpara[:,2]
def B(x,Teq):
ee = exp(h * (dfimppara.iloc[x, 1] * 115e9)/ (KB * Teq)) - 1
return ((2 * h * ((dfimppara.iloc[x, 1] * 115e9 ) ** 3)/c**2) * (ee**(-1)))
def Unu(x,z,test):
TCMB = 2.7 * (1 + z)
ee = exp(h * (dfimppara.iloc[x, 1] * 115e9)/ (KB * TCMB)) - 1
return ((8 * pi * h * ((dfimppara.iloc[x, 1] * 115e9 ) ** 3)/c**3) * pow(ee, -1)) * test
from sympy import Symbol, Eq, solve
UU=[1e-25,1e-24,1e-23,1e-22,1e-21,1e-20,1e-19]
for ng in np.arange(100,200,500):
for z in np.arange(1, 2):
for xx in range(1, 819):
for uff in range (len(UU)):
Ju = dfimppara.iloc[xx, 1]
Jl = dfimppara.iloc[xx, 2]
lim = Ju - Jl
if lim > 1:
pass
else:
if Ju<2:
Teq = Symbol('Teq')
an = solve(UU[uff] + Unu(xx,z,1) - B(xx,Teq), 0)
print(an)
else:
pass
Related
Code:
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
# parameters
S = 0.0001
M = 30.03
K = 113.6561
Vr = 58
R = 8.3145
T = 298.15
Q = 0.000133
Vp = 0.000022
Mr = 36
Pvap = 1400
wf = 0.001
tr = 1200
mass = 40000
# define t
time = 14400
t = np.arange(0, time + 1, 1)
# define initial state
Cv0 = (mass / Vp) * wf # Cv(0)
Cr0 = (mass / Vp) * (1 - wf)
Cair0 = 0 # Cair(0)
# define function and solve ode
def model(x, t):
C = x[0] # C is Cair(t)
c = x[1] # c is Cv(t)
a = Q + (K * S / Vr)
b = (K * S * M) / (Vr * R * T)
s = (K * S * M) / (Vp * R * T)
w = (1 - wf) * 1000
Peq = (c * Pvap) / (c + w * c * M / Mr)
Pair = (C * R * T) / M
dcdt = -s * (Peq - Pair)
if t <= tr:
dCdt = -a * C + b * Peq
else:
dCdt = -a * C
return [dCdt, dcdt]
x = odeint(model, [Cair0, Cv0], t)
C = x[:, 0]
c = x[:, 1]
Now, I want to figure out wf value when I know C(0)(when t is 0) and C(tr)(when t is tr)(Therefore I know two kind of t and C(t)).
I found some links(Curve Fit Parameters in Multiple ODE Function, Solving ODE with Python reversely, https://medium.com/analytics-vidhya/coronavirus-in-italy-ode-model-an-parameter-optimization-forecast-with-python-c1769cf7a511, https://kitchingroup.cheme.cmu.edu/blog/2013/02/18/Fitting-a-numerical-ODE-solution-to-data/) related to this, although I cannot get the hang of subject.
Can I fine parameter wf with two data((0, C(0)), (tr, C(tr)) and ode?
First, ODE solvers assume smooth right-hand-side functions. So the if t <= tr:... statement in your code isn't going to work. Two separate integrations must be done to deal with the discontinuity. Integrate to tf, then use the solution at tf as initial conditions to integrate beyond tf for the new ODE function.
But it seems like your main problem (solving for wf) only involves integrating to tf (not beyond), so we can ignore that issue when solving for wf
Now, I want to figure out wf value when I know C(0)(when t is 0) and C(tr)(when t is tr)(Therefore I know two kind of t and C(t)).
You can do a non-linear solve for wf:
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
# parameters
S = 0.0001
M = 30.03
K = 113.6561
Vr = 58
R = 8.3145
T = 298.15
Q = 0.000133
Vp = 0.000022
Mr = 36
Pvap = 1400
mass = 40000
# initial condition for wf
wf_initial = 0.02
# define t
tr = 1200
t_eval = np.array([0, tr], np.float)
# define initial state. This is C(t = 0)
Cv0 = (mass / Vp) * wf_initial # Cv(0)
Cair0 = 0 # Cair(0)
init_cond = np.array([Cair0, Cv0],np.float)
# Definte the final state. This is C(t = tr)
final_state = 3.94926615e-03
# define function and solve ode
def model(x, t, wf):
C = x[0] # C is Cair(t)
c = x[1] # c is Cv(t)
a = Q + (K * S / Vr)
b = (K * S * M) / (Vr * R * T)
s = (K * S * M) / (Vp * R * T)
w = (1 - wf) * 1000
Peq = (c * Pvap) / (c + w * c * M / Mr)
Pair = (C * R * T) / M
dcdt = -s * (Peq - Pair)
dCdt = -a * C + b * Peq
return [dCdt, dcdt]
# define non-linear system to solve
def function(x):
wf = x[0]
x = odeint(model, init_cond, t_eval, args = (wf,), rtol = 1e-10, atol = 1e-10)
return x[-1,0] - final_state
from scipy.optimize import root
sol = root(function, np.array([wf_initial]), method='lm')
print(sol.success)
wf_solution = sol.x[0]
x = odeint(model, init_cond, t_eval, args = (wf_solution,), rtol = 1e-10, atol = 1e-10)
print(wf_solution)
print(x[-1])
print(final_state)
So I am looking to solve a system of equations in python 3.7 with numpy. However, I need to solve the system of equations at the end of each iteration. During the iterations, it will solve some equations that will make up the contents of A and B to find x in the form of Ax=B. Upon solving for x I need to save these values to then solve the underlying equations for the following iteration to be reimplemented in A and B.
I have tried a more linear approach to solving the problem but it is not good for my end goal of solving the equation attached in the image. What I have done so far has also been attached below:
i = 0
while (y[i] >= 0 ): #Object is above water
t[i+1] = t[i] + dt
vx[i+1] = vx[i] + dt * ax[i] #Update the velocities
vy[i+1] = vy[i] + dt * ay[i]
v_ax[i+1] = (vx[i]*np.sin(phi/180*np.pi)) - (vy[i]*np.cos(phi/180*np.pi))
v_nor[i+1] = (vx[i]*np.cos(phi/180*np.pi)) + (vy[i]*np.sin(phi/180*np.pi))
F_wnor[i+1] = (Cd_a * A_da * rho_air * (v_nor[i] - v_wind*np.sin(phi/180*np.pi)) * abs(v_nor[i] - v_wind*np.sin(phi/180*np.pi)))/2
F_wax[i+1] = (Cd_a * A_da * rho_air * (v_ax[i] - v_wind*np.sin(phi/180*np.pi)) * abs(v_ax[i] - v_wind*np.sin(phi/180*np.pi)))/2
F_wx[i+1] = (-F_wax[i] * np.sin(phi/180*np.pi)) - (F_wnor[i] * np.cos(phi/180*np.pi))
F_wy[i+1] = (F_wax[i] * np.cos(phi/180*np.pi)) - (F_wnor[i] * np.sin(phi/180*np.pi))
ax[i+1] = F_wx[i]/M
ay[i+1] = (F_wx[i]/M) - g
y[i+1] = (y[i]+dt*vy[i])
x[i+1] = (x[i]+dt*vx[i])
i = i + 1
j = i
#under water velocities
# if y(t)>0: M*z'' = M.g - Fb + Fd + Fm
while (y[j] <= 0 and y[j] > -10):
if (abs(y[j]/r)< 2):
theta_degree = 2 * np.arccos(1 - (abs(y[j])/r))
theta = theta_degree/180*np.pi
m = ((rho_water * r**2)/2) * (((2*(np.pi)**3*(1-np.cos(theta))) / ( 3 * (2*np.pi-theta)**2)) \
+ (np.pi * (1-np.cos(theta)*1/3)) + (np.sin(theta)) - (theta))
dm_dz = ((rho_water * r)/np.sin(theta/2)) * (((2 * (np.pi)**3 / 3) * ((np.sin(theta) / (2*np.pi - theta)**2) \
+ (2 * (1-np.cos(theta)) / (2*np.pi - theta )**3))) + (np.pi * np.sin(theta) / 3) + np.cos(theta) - 1)
A_i = (r**2)/2 * (theta - np.sin(theta))
F_m[j] = - m * ay[j] - dm_dz * np.max(vy)*vy[j]
F_uwater[j] = (M * g) - (rho_water * A_i * g) - (Cd_y * rho_water * r * vy[j] * abs(vy[j]))
else:
m = np.pi * rho_water * r**2
dm_dz = 0
A_i = np.pi * r**2
F_m[j] = - m * ay[j] - dm_dz * vy[j]**2
F_uwater[j] = (M * g) - (rho_water * A_i * g) - (Cd_y * rho_water * r * vy[j] * abs(vy[j]))
print("Fully submerged")
t[j+1] = t[j] + dt
vx[j+1] = vx[j] + dt * ax[j] #Update the velocities
vy[j+1] = vy[j] + dt * ay[j]
ax[j+1] = F_wx[j]/M
ay[j+1] = (F_uwater[j] + F_m[j]/M)
y[j+1] = (y[j]+dt*vy[j])
x[j+1] = (x[j]+dt*vx[j])
print(y[j])
j = j + 1
I do not know how to go about this and help for getting started would be greatly appreciated!.
The problem I am trying to solve can be seen more clearly in the picture attached. System of equations I am trying to solve
I am trying to solve this problem, searched stack overflow and couldn't find an answer.
I want to integrate a set of state equations, where my inputs that I want to pass as arguments, are arrays as the same length of t.
Example of time-invariant arguments:
# state function
def state(x, t, u_in):
u = x[0]
v = x[1]
w = x[2]
phi = x[3]
theta = x[4]
psi = x[5]
h = x[6]
ax = u_in[0]
ay = u_in[1]
az = u_in[2]
p = u_in[3]
q = u_in[4]
r = u_in[5]
pdot = u_in[6]
qdot = u_in[7]
rdot = u_in[8]
xdot = np.zeros(len(x))
xdot[0] = ax - g * np.sin(theta) + r * v - q * w
xdot[1] = ay + g * np.sin(phi) * np.cos(theta) + p * w - r * u
xdot[2] = az + g * np.cos(phi) * np.cos(theta) + q * u - p * v
xdot[3] = p + (q * np.sin(phi) + r * np.cos(phi)) * np.tan(theta)
xdot[4] = q * np.cos(phi) - r * np.sin(phi)
xdot[5] = (q * np.sin(phi) + r * np.cos(phi)) / np.cos(theta)
xdot[6] = u * np.sin(theta) - v * np.sin(phi) * np.cos(theta) - w * np.cos(phi) * np.cos(theta)
return xdot
# initial condition
x0 = np.zeros(7)
# set problem
n = 101
t = np.linspace(0, 10, num=n)
uinp = np.zeros(9)
uinp[0] = 0
uinp[1] = 0
uinp[2] = -g
uinp[3] = 0
uinp[4] = 0
uinp[5] = 0
uinp[6] = 0
uinp[7] = 0
uinp[8] = 0
# solve ODE
x = odeint(state, x0, t, args=(uinp,))
This works fine because my inputs are time invariant.
What I want to do and it doesn't work is to set my uinp as np.zeros_like(t) and solve the same ODE.
i.e.
uinp = np.zeros((n, 9))
uinp[:, 0] = 0
uinp[:, 1] = 0
uinp[:, 2] = -g
uinp[:, 3] = 0
uinp[:, 4] = 0
uinp[:, 5] = 0
uinp[:, 6] = 0
uinp[:, 7] = 0
uinp[:, 8] = 0
and I get this error:
ValueError: setting an array element with a sequence.
enter code here
Using a loop for each timepiece input is not an option, as it creates too much overhead and it takes forever to run this simulation.
Thanks for the help and insights
Couldn't you make your state function time dependent? If I understood correctly, then this part:
ax = u_in[0]
ay = u_in[1]
az = u_in[2]
p = u_in[3]
q = u_in[4]
r = u_in[5]
pdot = u_in[6]
qdot = u_in[7]
rdot = u_in[8]
should depend on time t:
ax = u_in[t, 0]
ay = u_in[t, 1]
az = u_in[t, 2]
p = u_in[t, 3]
q = u_in[t, 4]
r = u_in[t, 5]
pdot = u_in[t, 6]
qdot = u_in[t, 7]
rdot = u_in[t, 8]
You might need to find a way of mapping the input t, which may be a float to the corresponding column in u_in
I am trying to calculate g(x_(i+2)) from the value g(x_(i+1)) and g(x_i), i is an integer, assuming I(x) and s(x) are Gaussian function. If we know x_i = 100, then the summation from 0 to 100, I don't know how to handle g(x_i) with the subscript in python, knowing the first and second value, we can find the third value, after n cycle, we can find the nth value.
Equation:
code:
import numpy as np
from matplotlib import pyplot as p
from math import pi
def f_s(x, mu_s, sig_s):
ss = -np.power(x - mu_s, 2) / (2 * np.power(sig_s, 2))
return np.exp(ss) / (np.power(2 * pi, 2) * sig_s)
def f_i(x, mu_i, sig_i):
ii = -np.power(x - mu_i, 2) / (2 * np.power(sig_i, 2))
return np.exp(ii) / (np.power(2 * pi, 2) * sig_i)
# problems occur in this part
def g(x, m, mu_s, sig_s, mu_i, sig_i):
for i in range(1, m): # specify the number x, x_1, x_2, x_3 ......X_m
h = (x[i + 1] - x[i]) / e
for n in range(0, x[i]): # calculate summation
sum_f = (f_i(x[i], mu_i, sig_i) - f_s(x[i] - n, mu_s, sig_s) * g_x[n]) * np.conj(f_s(n +
x[i], mu_s, sig_s))
g_x[1] = 1 # initial value
g_x[2] = 5
g_x[i + 2] = h * sum_f + 2 * g_x[i + 1] - g_x[i]
return g_x[i + 2]
x = np.linspace(-10, 10, 10000)
e = 1
d = 0.01
m = 1000
mu_s = 2
sig_s = 1
mu_i = 1
sig_i = 1
p.plot(x, g(x, m, mu_s, sig_s, mu_i, sig_i))
p.legend()
p.show()
result:
I(x) and s(x)
import math
import numpy as np
S0 = 100.; K = 100.; T = 1.0; r = 0.05; sigma = 0.2
M = 100; dt = T / M; I = 500000
S = np.zeros((M + 1, I))
S[0] = S0
for t in range(1, M + 1):
z = np.random.standard_normal(I)
S[t] = S[t - 1] * np.exp((r - 0.5 * sigma ** 2) * dt + sigma *
math.sqrt(dt) * z)
C0 = math.exp(-r * T) * np.sum(np.maximum(S[-1] - K, 0)) / I
print ("European Option Value is ", C0)
It gives a value of around 10.45 as you increase the number of simulations, but using the B-S formula the value should be around 10.09. Anybody know why the code isn't giving a number closer to the formula?