Let's start by solving a single, first-order, ordinary differential equation. We'll compare the exact solution to the numerical solution:
var('x, y') y = function('y', x) ode = diff(y, x) + 1 / x * y == x * y^2 sol = desolve(ode, y, ics=[10, 1]) sol.show() exact_plot = plot(sol, (x, 0.1, 10), color='red', marker='.') rk4_plot = desolve_rk4(ode, y, ics=[10, 1], output='slope_field', end_points=[0, 10]) show(exact_plot + rk4_plot, figsize=(4, 3))
The symbolic solution is shown in the following screenshot, and its graph is plotted on the same axes as the numerical solution and the slope field:
As we explained in the previous chapter, we defined a symbolic ordinary differential equation and found the solution using desolve
. The warning messages occur during the calculation of the slope field, and can be safely ignored. We then used the desolve_rk4
function to compute the solution numerically. As its name implies, the functions...