Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 8
Case study
python to solve differential equation
Submitted by Name: Ansari Farhan Mohd Siddique Div: B Roll no : 01
Department of mechanical engineering
Aim: solve differential equation in python
• Differential equations can be solved with different methods in Python.
Below are examples that show how to solve differential equations with (1) GEKKO Python, (2) Euler's method, (3) the ODEINT function from Scipy.Integrate. Additional information is provided on using APM Python for parameter estimation with dynamic models and scale-up to large-scale problems. This page, based very much on is aimed at introducing techniques for solving initial-value problems involving ordinary differential equations using Python. Specifically, it will look at systems of the form: dydt=f(t,y,c)(1) dydt=f(t,y, where yy represents an array ofc)dependent variables, tt represents the independent variable, and cc represents an array of constants. Note that although the equation above is a first-order differential equation, many higher-order equations can be re-written to satisfy the form above. In addition, the examples on this page will assume that the initial values of the variables in yy are known - this is what makes these kinds of problems initial value problems (as opposed to boundary value problems). Simple Example Assume you want to numerically solve: dy(t)dt=t−y(t)(2)(2)dy(t)dt=t−y(t)for some times between 0 and 15. Assuming the initial value for y(0)y(0) is 2, you can solve this with:
import numpy as np from
scipy.integrate import solve_ivp sol = solve_ivp(lambda t, y: t-y, [0, 15], [2], rtol = 1e-5) After this runs, sol will be an object containing 10 different items. Of these, sol.t will be the times at which the solver found values and sol.y will be a 2-D array. Each row of sol.y will be the solution to one of the dependent variables -- since this problem has a single differential equation with a single initial condition, there will only be one row. To plot the solution, use:
import matplotlib.pyplot as plt
plt.plot(sol.t, sol.y[0], 'k--s') The [0] at the end of sol.y is critical as it pulls out the first row of values. Note in this case that the solutions are not at evenly spaced times - specifically, they are at:
In [1]: sol.t.reshape(-1,1) Out[1]: array([[ 0. ], [ 0.0932467 ],