Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Summer A ODEs
Views: 101
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2004
Kernel: SageMath 9.2

Slope fields and integral curves

This notebook contains two interactive elements. (If the interactive elements don't show up immediately, click on each of the code cells and either hit "Run" or press "Shift + Enter")

1. Plotting slope fields

The first lets you input a function f(x,y)f(x,y) into a box and it plots the slope field of the differential equation dydx=f(x,y).\frac{dy}{dx} = f(x,y).

x,y = var('x,y') @interact def show_slope_field(xrange = slider(1, 10, .5, default = 3), yrange = slider(1,10, .5, default = 3), f = ('$f$', y^2 - x)): show(plot_slope_field(f, (x,-xrange,xrange), (y,-yrange,yrange)))

2. Plotting slope fields with solution

This function allows you to specify, together with a differential equation as in the last problem, a list of initial conditions y(0)=y1,,yky(0) = y_1, \ldots, y_k and plots the slope field together with the plots of the solutions

which pass through the points (0,yi)(0, y_i). Note that this method can only plot solutions which are single valued.

Remark: If you are getting junk answers because of an infinite slope, try changing infinite_slope_tolerance = 20 to a different value, maybe make 20 smaller, and then re-evaluate the cell.

x,y = var('x,y') infinite_slope_tolerance = 20 @interact def show_slope_field_with_integral_curves(xrange = slider(1, 10, .5, default = 3), yrange = slider(1,10, .5, default = 3), f = ('$f$', -y/(x+1)), y0s = input_box([1,2], label = '$y(0) = $')): lines = [] for y0 in y0s: sol = desolve_rk4(f, y, [0, y0], end_points = [-xrange, xrange], step = 0.005) start, end = 0, len(sol)-1 for i in range(len(sol)): (a,b) = sol[i] if (a < 0) and not((-yrange - 0.1 < b < yrange + 0.1) and (abs(f(x = a + 1e-9, y = b + 1e-9)) < infinite_slope_tolerance)) : start = i + 1 if (a >= 0) and not( (-yrange - 0.1 < b < yrange + 0.1) and (abs(f(x = a + 1e-9, y = b + 1e-9)) < infinite_slope_tolerance)): end = i break lines.append(line(sol[start:end])) show(plot_slope_field(f, (x,-xrange,xrange), (y,-yrange,yrange)) + sum(lines))