Solutions#

Question 1#

1. Create the following differential equations:

1. \(\frac{dy}{dx} = \cos(x)\)

import sympy as sym

x = sym.Symbol("x")
y = sym.Function("y")

differential_equation = sym.Eq(sym.diff(y(x), x), sym.cos(x))
differential_equation
\[\displaystyle \frac{d}{d x} y{\left(x \right)} = \cos{\left(x \right)}\]

2. \(\frac{dy}{dx} = 1 - y\)

differential_equation = sym.Eq(sym.diff(y(x), x), 1 - y(x))
differential_equation
\[\displaystyle \frac{d}{d x} y{\left(x \right)} = 1 - y{\left(x \right)}\]

3. \(\frac{dy}{dx} = \frac{x - 50}{10}\)

differential_equation = sym.Eq(sym.diff(y(x), x), (x - 50) / 10)
differential_equation
\[\displaystyle \frac{d}{d x} y{\left(x \right)} = \frac{x}{10} - 5\]

4. \(\frac{dy}{dx} = y ^2 \ln (x)\)

differential_equation = sym.Eq(sym.diff(y(x), x), y(x) ** 2 * sym.log(x))
differential_equation
\[\displaystyle \frac{d}{d x} y{\left(x \right)} = y^{2}{\left(x \right)} \log{\left(x \right)}\]

5. \(\frac{dy}{dx} = (1 + y) ^ 2\)

differential_equation = sym.Eq(sym.diff(y(x), x), (1 + y(x)) ** 2)
differential_equation
\[\displaystyle \frac{d}{d x} y{\left(x \right)} = \left(y{\left(x \right)} + 1\right)^{2}\]

Question 2#

2. Obtain the general solution for the equations in question 1.

1. \(\frac{dy}{dx} = \cos(x)\)

differential_equation = sym.Eq(sym.diff(y(x), x), sym.cos(x))
sym.dsolve(differential_equation, y(x))
\[\displaystyle y{\left(x \right)} = C_{1} + \sin{\left(x \right)}\]

2. \(\frac{dy}{dx} = 1 - y\)

differential_equation = sym.Eq(sym.diff(y(x), x), 1 - y(x))
sym.dsolve(differential_equation, y(x))
\[\displaystyle y{\left(x \right)} = C_{1} e^{- x} + 1\]

3. \(\frac{dy}{dx} = \frac{x - 50}{10}\)

differential_equation = sym.Eq(sym.diff(y(x), x), (x - 50) / 10)
sym.dsolve(differential_equation, y(x))
\[\displaystyle y{\left(x \right)} = C_{1} + \frac{x^{2}}{20} - 5 x\]

4. \(\frac{dy}{dx} = y ^2 \ln (x)\)

differential_equation = sym.Eq(sym.diff(y(x), x), y(x) ** 2 * sym.log(x))
sym.dsolve(differential_equation, y(x))
\[\displaystyle y{\left(x \right)} = - \frac{1}{C_{1} + x \log{\left(x \right)} - x}\]

5. \(\frac{dy}{dx} = (1 + y) ^ 2\)

differential_equation = sym.Eq(sym.diff(y(x), x), (1 + y(x)) ** 2)
sym.dsolve(differential_equation, y(x))
\[\displaystyle y{\left(x \right)} = \frac{- C_{1} + x + 1}{C_{1} - x}\]

Question 3#

3. Obtain the particular solution for the equations in question 1 with the following particular conditions:

1. \(y(0) = \pi\)

conditions = {y(0): sym.pi}
differential_equation = sym.Eq(sym.diff(y(x), x), sym.cos(x))
sym.dsolve(differential_equation, y(x), ics=conditions)
\[\displaystyle y{\left(x \right)} = \sin{\left(x \right)} + \pi\]

2. \(y(2) = 3\)

conditions = {y(2): 3}
differential_equation = sym.Eq(sym.diff(y(x), x), 1 - y(x))
sym.dsolve(differential_equation, y(x), ics=conditions)
\[\displaystyle y{\left(x \right)} = 1 + 2 e^{2} e^{- x}\]

3. \(y(50) = 1\)

conditions = {y(50): 1}
differential_equation = sym.Eq(sym.diff(y(x), x), (x - 50) / 10)
sym.dsolve(differential_equation, y(x), ics=conditions)
\[\displaystyle y{\left(x \right)} = \frac{x^{2}}{20} - 5 x + 126\]

4. \(y(e) = 1\)

conditions = {y(sym.exp(1)): 1}
differential_equation = sym.Eq(sym.diff(y(x), x), y(x) ** 2 * sym.log(x))
sym.dsolve(differential_equation, y(x), ics=conditions)
\[\displaystyle y{\left(x \right)} = - \frac{1}{x \log{\left(x \right)} - x - 1}\]

5. \(y(-1) = 3\)

conditions = {y(-1): 3}
differential_equation = sym.Eq(sym.diff(y(x), x), (1 + y(x)) ** 2)
sym.dsolve(differential_equation, y(x), ics=conditions)
\[\displaystyle y{\left(x \right)} = \frac{x + \frac{7}{4}}{- x - \frac{3}{4}}\]

Question 4#

4. The rate of increase of a population (\(p\)) is equal to 1% of the size of the population.

1. Define the differential equation that models this situation.

p = sym.Function("p")
t = sym.Symbol("t")

differential_equation = sym.Eq(sym.diff(p(t), t), p(t) / 100)
differential_equation
\[\displaystyle \frac{d}{d t} p{\left(t \right)} = \frac{p{\left(t \right)}}{100}\]

2. Given that \(p(0)=5000\) find the population after 5 time units.

condition = {p(0): 5000}
sym.dsolve(differential_equation, p(t), ics=condition)
\[\displaystyle p{\left(t \right)} = 5000 e^{\frac{t}{100}}\]

Thus the population after 5 time units is:

5000 * sym.exp(sym.S(5) / 100)
\[\displaystyle 5000 e^{\frac{1}{20}}\]

Question 5#

5. The rate of change of the temperature of a hot drink is proportional to the difference between the temperature of the drink (\(T\)) and the room temperature (\(T_R\)).

1. Define the differential equation that models this situation.

T = sym.Function("T")
t = sym.Symbol("t")
T_r = sym.Symbol("T_r")
k = sym.Symbol("k")

differential_equation = sym.Eq(sym.diff(T(t), t), k * (T(t) - T_r))
differential_equation
\[\displaystyle \frac{d}{d t} T{\left(t \right)} = k \left(- T_{r} + T{\left(t \right)}\right)\]

2. Solve the differential equation.

sym.dsolve(differential_equation, T(t))
\[\displaystyle T{\left(t \right)} = C_{1} e^{k t} + T_{r}\]

3. Given that \(T(0) = 100\) and the room temperature is \(T_R=20\) obtain the particular solution.

condition = {T(0): 100}
particular_differential_equation = differential_equation.subs({T_r: 20})
sym.dsolve(particular_differential_equation, T(t), ics=condition)
\[\displaystyle T{\left(t \right)} = 80 e^{k t} + 20\]

4. Use the particular solution to identify how on it will take for the drink to be ready for consumption (a temperature of 80) given that after 3 time units \(T(3)=90\).

First let us find the missing variable \(k\) by using the fact that \(T(3) = 90\):

equation = sym.Eq(80 * sym.exp(k * 3) + 20, 90)
sym.solveset(equation, k)
\[\displaystyle \left\{\frac{2 n i \pi}{3} + \frac{\log{\left(\frac{7}{8} \right)}}{3}\; \middle|\; n \in \mathbb{Z}\right\}\]

The set of solutions is infinite but contains a single real value element (when \(n=0\)):

particular_k = sym.log(sym.S(7) / 8) / 3

The final equation to be solved is given by:

equation = sym.Eq(80 * sym.exp(particular_k * t) + 20, 80)
sym.solveset(equation, t)
\[\displaystyle \left\{\frac{3 \cdot \left(2 n i \pi + \log{\left(\frac{3}{4} \right)}\right)}{\log{\left(\frac{7}{8} \right)}}\; \middle|\; n \in \mathbb{Z}\right\}\]

which has a single real valued element (when \(n=0\)):

t_ready = 3 * sym.log(sym.S(3) / 4)/ sym.log(sym.S(7) / 8)
t_ready
\[\displaystyle \frac{3 \log{\left(\frac{3}{4} \right)}}{\log{\left(\frac{7}{8} \right)}}\]

Which means the hot drink will be ready in about 7 minutes:

float(t_ready)
6.463245835997123