Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
32 views
ubuntu2404
Kernel: SageMath 10.2

Differential Equations with SageMath in CoCalc - Part 2

First-Order Examples

This notebook contains Part 2 from the main Differential Equations with SageMath in CoCalc notebook.

For the complete course, please refer to the main notebook: Differential Equations with SageMath in CoCalc.ipynb

# Essential SageMath setup for differential equations from sage.all import * # Configure plotting for better visualization import matplotlib.pyplot as plt %matplotlib inline print(" SageMath environment loaded successfully!") print(f" Plotting backend: {plt.get_backend()}") print(" Ready to explore differential equations!")

Example 1: Population Growth - The Malthusian Model

Real-world scenario: Thomas Malthus (1798) observed that populations grow proportionally to their current size when resources are abundant.

Mathematical statement: "The rate of population growth is proportional to the current population"

If P(t)P(t) represents population at time tt, then: dPdt=kP\frac{dP}{dt} = kP

where:

  • dPdt\frac{dP}{dt} is the rate of population change

  • kk is the growth rate constant (births minus deaths per individual)

  • PP is the current population size

# Define symbolic variables for population model t = var('t') # time variable P = function('P')(t) # population as a function of time k = var('k') # growth rate constant # The differential equation: dP/dt = k*P population_eq = diff(P, t) == k * P print("Population Growth Differential Equation:") print(f" {population_eq}") print() print("Interpretation:") print(" • Left side: Rate of population change") print(" • Right side: Growth constant × Current population") print(" • Meaning: Larger populations grow faster (exponential growth)")
population_solution = desolve(population_eq, P, ivar=t) print("Solving the differential equation:") print(f" General solution: P(t) = {population_solution}") print() print("Understanding the solution:") print(" • C₁ represents the initial population P(0)") print(" • e^(kt) creates exponential growth/decay") print(" • If k > 0: population grows exponentially") print(" • If k < 0: population declines exponentially") print(" • If k = 0: population remains constant")
# Visualize population growth for different scenarios t_vals = srange(0, 5, 0.1) # Create plots for different growth rates p = Graphics() colors = ['blue', 'red', 'green', 'purple'] scenarios = [0.5, 0.2, -0.1, -0.3] labels = ['Fast growth (k=0.5)', 'Moderate growth (k=0.2)', 'Slow decline (k=-0.1)', 'Fast decline (k=-0.3)'] for i, k_val in enumerate(scenarios): P_vals = [1 * exp(k_val * t_val) for t_val in t_vals] p += list_plot(list(zip(t_vals, P_vals)), plotjoined=True, color=colors[i], legend_label=labels[i], thickness=2) p.axes_labels(['Time t', 'Population P(t)']) p.show(title='Population Dynamics: $P(t)=P_0 e^{k t}$', legend_loc='upper left') print("Key Insight: The same mathematical form describes both growth and decay!")

Example 2: Newton's Law of Cooling

Historical Background: Isaac Newton (1701) discovered that hot objects cool at a rate proportional to the temperature difference with their surroundings.

Real-world scenario: Your morning coffee cools down, but not uniformly – it cools faster when very hot, slower as it approaches room temperature.

Mathematical statement: "The rate of temperature change is proportional to the difference between object temperature and ambient temperature."

If T(t)T(t) is temperature at time tt, and TroomT_{\text{room}} is room temperature: dTdt=k(TTroom)\frac{dT}{dt} = -k(T - T_{\text{room}})

where:

  • dTdt\frac{dT}{dt} is the rate of temperature change

  • k>0k > 0 is the cooling constant (depends on material properties)

  • (TTroom)(T - T_{\text{room}}) is the temperature difference

  • The negative sign indicates temperature decreases when T>TroomT > T_{\text{room}}

# Define variables for Newton's Law of Cooling T = function('T')(t) # temperature as function of time T_room = var('T_room') # room temperature (constant) k_cooling = var('k_cooling') # cooling constant # Newton's Law of Cooling differential equation cooling_eq = diff(T, t) == -k_cooling * (T - T_room) print("Newton's Law of Cooling:") print(f" {cooling_eq}") print() print("Physical Interpretation:") print(" • Negative sign: Temperature decreases when T > T_room") print(" • Proportional to difference: Larger difference → faster cooling") print(" • As T → T_room, cooling rate → 0 (approaches equilibrium)")
t = var('t') k, T_room, T0 = var('k T_room T0') # assume(T0, 'real') # optional T = function('T')(t) # Newton's cooling: dT/dt = -k*(T - T_room) cooling_eq = diff(T, t) == -k*(T - T_room) # General solution cooling_solution = desolve(cooling_eq, T, ivar=t) print("Solving the cooling equation:") print(f" General solution: T(t) = {cooling_solution}") # With initial condition T(0) = T0 cooling_solution_ic = desolve(cooling_eq, T, ics=[0, T0], ivar=t) print(f" With T(0) = T0: T(t) = {cooling_solution_ic}")
--- ## **From First-Order Examples to Classification and Solution Methods** You've completed **First-Order Examples**! The population growth and cooling models demonstrate how differential equations capture the mathematics of change in real-world systems. **What awaits in Part 3?** Part 3 explores **Classification and Solution Methods** - learning to categorize different types of differential equations and the systematic approaches to solve them. ### **Continue Your Journey** The concrete examples you've worked with will now be generalized into broader solution techniques and classification schemes. **[Continue to Part 3: Classification and Solution Methods ](https://cocalc.com/share/public_paths/c0b7bff7fcdd845564cc6c77dd23db6736b99c77)** **[Return to Main Course](https://cocalc.com/share/public_paths/43b79cbb7789fef95ac7cf057d8a27db19a84f34)**

From First-Order Examples to Classification and Solution Methods

You've completed First-Order Examples! The population growth and cooling models demonstrate how differential equations capture the mathematics of change in real-world systems.

What awaits in Part 3?

Part 3 explores Classification and Solution Methods - learning to categorize different types of differential equations and the systematic approaches to solve them.

Continue Your Journey

The concrete examples you've worked with will now be generalized into broader solution techniques and classification schemes.

Continue to Part 3: Classification and Solution Methods →

Return to Main Course