Construction of a symbolic result for a particular solution of a differential equation

So far, I have managed to find a specific solution to this equation for any given mass coefficient and resistance. However, I did not find a way to build a solution or even evaluate the solution for a specific point. I really want to find a way to build a solution.

from sympy import *

m = float(raw_input('Mass:\n> '))
g = 9.8
k = float(raw_input('Drag Coefficient:\n> '))
f = Function('f')
f1 = g * m
t = Symbol('t')
v = Function('v')
equation = dsolve(f1 - k * v(t) - m * Derivative(v(t)), 0)
C1 = Symbol('C1')
C1_ic = solve(equation.rhs.subs({t:0}),C1)[0]
equation = equation.subs({C1:C1_ic})
+4
source share
3 answers

Import these libraries (the sea coast just makes the graphics pretty).

from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np

Then apply this to the end. This will determine the time, t, against speed, v (t).

# make a numpy-ready function from the sympy results
func = lambdify(t, equation.rhs,'numpy')
xvals = np.arange(0,10,.1)
yvals = func(xvals)

# make figure
fig, ax = plt.subplots(1,1,subplot_kw=dict(aspect='equal'))     
ax.plot(xvals, yvals)
ax.set_xlabel('t')
ax.set_ylabel('v(t)')
plt.show()

I get such a graph for mass 2 and resistance coefficient 2. enter image description here

+3
source

Sympy plot, , , , " " .

plot(equation.rhs,(t,0,10))

enter image description here

+4

If I understand correctly, you want to present the right side of your solution, here is one of several ways to do this:

from sympy import *
import numpy as np
import matplotlib.pyplot as plt

m = float(raw_input('Mass:\n> '))
g = 9.8
k = float(raw_input('Drag Coefficient:\n> '))
f = Function('f')
f1 = g * m
t = Symbol('t')
v = Function('v')
equation = dsolve(f1 - k * v(t) - m * Derivative(v(t)), 0)
C1 = Symbol('C1')
C1_ic = solve(equation.rhs.subs({t: 0}), C1)[0]
equation = equation.subs({C1: C1_ic})

t1 = np.arange(0.0, 50.0, 0.1)
y1 = [equation.subs({t: tt}).rhs for tt in t1]

plt.figure(1)
plt.plot(t1, y1)
plt.show()
0
source

Source: https://habr.com/ru/post/1651343/


All Articles