0

How do we solve a system of linear equations in Python and NumPy:

We have a system of equations and there is the right side of the values after the equal sign. We write all the coefficients into the matrix matrix = np.array(...),, and write the right side into the vector vector = np.array(...) and then use the command np.linalg.solve(matrix, vector) to find the variables.

But if I have derivatives after the equal sign and I want to do the same with the system of differential equations, how can I implement this?

(Where are the lambda known values, and I need to find A

P.S. I saw the use of this command y = odeint(f, y0, t) from the library scipy but I did not understand how to set my own function f if I have a matrix there, what are the initial values y0 and what t?

5
  • t is the time period you want to integrate over & y0 is the values of the p's at the first time point. Commented Oct 23, 2020 at 14:15
  • @DrBwts how can i find out the initial values of p if they are unknown values? and how to make a function f? Commented Oct 23, 2020 at 14:20
  • your function will contain the set of differential equations that you need to solve. If you dont have initial conditions you will not be able to solve these numerically. Commented Oct 23, 2020 at 14:26
  • @DrBwts do I have to write the function itself explicitly? I can not also do it using the coefficient matrix when solving linear equations? Commented Oct 23, 2020 at 14:28
  • 2
    Stackoverflow isn't here to teach you how to solve differential equations. I suggest you find some tutorials about solving differential equations using scipy. Here might be a good place to start. Good luck! Commented Oct 23, 2020 at 14:37

1 Answer 1

1

You can solve your system with the compact form

t = arange(t0,tf,h) solX = odeint(lambda X,t: M.dot(X), X0, t) 

after setting the parameters and initial condition.

For advanced use set also the absolute and relative error thresholds according to the scale of the state vector and the desired accuracy.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it seems like the truth. The question arose when we solve a system of linear equations linalg.solve, the function returns to us an array containing the desired answers, i.e. intersection of equations. But odeint returns an array of ordinates for all equations over all time, but what is the solution? In all the examples, I've seen how to do plotting, but where is the numerical solution itself?
The solution is the function table of the pairs (t[k],X[k]). Other solvers can also return an array of interpolating polynomials, the so-called "dense output".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.