How to integrate a function w.r.t time; i.e 'y' is an array and values of time(t) is varying from 1 to 3000

Viewed 126

How do I integrate a function f(y) w.r.t time; i.e 'y' is an array of 3000 values and values of time(t) is varying from 1 to 3000. So, I need 3000 values after integrating f(y). Integration would be indefinite and the integral values must be without 'x' and 'C'(Constant). This is a part of my code:

k12 = np.array([random.random() for _ in range(3000)])
I1 = np.array([random.random() for _ in range(3000)])
m12 = np.array([random.random() for _ in range(3000)])
_k12 = [-x for x in k12]
_m12 = [-x for x in m12]

k21 = np.array([random.random() for _ in range(3000)])
I2 = np.array([random.random() for _ in range(3000)])
m21 = np.array([random.random() for _ in range(3000)])
_k21 = [-x for x in k21]
_m21 = [-x for x in m21]

k12_I1 = [i / j for i, j in zip(k12, I1)]
m12_I1 = [i / j for i, j in zip(m12, I1)]
_k12_I1 = [i / j for i, j in zip(_k12, I1)]
_m12_I1 = [i / j for i, j in zip(_m12, I1)]

k21_I2 = [i / j for i, j in zip(k21, I2)]
m21_I2 = [i / j for i, j in zip(m21, I2)]
_k21_I2 = [i / j for i, j in zip(_k21, I2)]
_m21_I2 = [i / j for i, j in zip(_m21, I2)]

X1_x = np.array(pd.read_csv(r"C:/Users/Lenovo/Desktop/Temp/X1_x.csv"))
X2 = np.array(pd.read_csv(r"C:/Users/Lenovo/Desktop/Temp/X2.csv"))
X2_diff = np.array(pd.read_csv(r"C:/Users/Lenovo/Desktop/Temp/X2_diff.csv"))

X3_ = ((k12*X1_x)-(I1*X2_diff)+(m12*X2))/k12

I need X3_ but since the integral values are of the form constant*x, so it gives an error:

'TypeError: can't multiply sequence by non-int of type 'float''
1 Answers

There are symbolical libraries that totally suit your task, like SymPy - very advanced symbolical library. You can use it.

Numpy library is doing only numerical computations, no symbolics, you have to do all symbolics in your mind.

As I understood in your final X3 you have sum of two terms, one of kind Val1 * x another is of kind Val2, i.e. X3 = Val1 * x + Val2. Then you can split your X3 into two parts (here Val1 is X3_x and Val2 is X3_c):

X3_x = k12*X1/k12
X3_c = (-(I1*X2_diff)+(m12*X2))/k12

Later to compute whole X3 for some given fixed value of x you just do in Python X3 = X3_x * x + X3_c.

Also you have to process your CSVs in such a way that your final arrays contain only floats, no symbolics, *x removed everywhere, i.e. do:

X1_rd = np.array([e.strip().replace('[', '').replace(']', '').replace('*x', '') for e in pd.read_csv(r"X1_x.csv")]).astype(np.float64)
X2_rd = np.array(pd.read_csv(r"X2.csv")).astype(np.float64)
X2_diff_rd = np.array(pd.read_csv(r"X2_diff.csv")).astype(np.float64)

Full final corrected code

Related