SciPy’s solve_ivp returns a result containing y (numerical function result, ylabel ( 'concentration' ) > # Finally, display the annotated plot > plt. T ) > # Add title, legend, and axis labels to the plot > plt. y > # Plot the result graphically using matplotlib > plt. solve_ivp ( f, ( 0, 10 ), y0, t_eval = t_eval, args = k_vals ) > # Extract the y (concentration) values from SciPy solution result > y = solution. linspace ( 0, 10, 50 ) # evaluate integral from t = 0-10 for 50 points > # Call SciPy's ODE initial value problem solver solve_ivp by passing it > # the function f, > # the interval of integration, > # the initial state, and > # the arguments to pass to the function f > solution = scipy. array () # arbitrary in this case > y0 = # initial condition (initial values) > t_eval = np. > from sympy import symbols, lambdify > import numpy as np > import scipy.integrate > import matplotlib.pyplot as plt > # Create symbols y0, y1, and y2 > y = symbols ( 'y:3' ) > kf, kb = symbols ( 'kf kb' ) > rf = kf * y ** 2 * y > rb = kb * y ** 2 > # Derivative of the function y(t) values for the three chemical species > # for input values y, kf, and kb > ydot = > ydot > t = symbols ( 't' ) # not used in this case > # Convert the SymPy symbolic expression for ydot into a form that > # SciPy can evaluate numerically, f > f = lambdify (( t, y, kf, kb ), ydot ) > k_vals = np. Them as symbols and then substitute in their values using ForĮxample, if you want to assign values to arbitrary constants, you can create Generated automatically by dsolve(), by creating them as symbols. You can manipulate arbitrary constants such as C1, C2, and C3, which are It should be given in the form of ] > # Extract expression for y(x) > solutions_list C1 - C2*exp(x) Work With Arbitrary Constants # Initial and boundaryĬonditions are treated the same way (even though the argument is called ics). Them with the dsolve() optional argument ics. If your differential equation(s) have initial or boundary conditions, specify Specify Initial Conditions or Boundary Conditions # Note that you supply the functions to be solved for as a list as the secondĪrgument of dsolve(), here. from sympy import symbols, Eq, Function, dsolve > f, g = symbols ( "f g", cls = Function ) > x = symbols ( "x" ) > eqs = > dsolve ( eqs, )
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |