source("http://macosa.dima.unige.it/r.R")    # If I have not already loaded the library
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
S 09 Differential equations (1st and 2nd order). Direction fields
 
# Graphic solution of 1st order differential equations by the Euler method:
 
# soledif(x0,y0,xf,N,col) traces with N steps the polygonal approximation of the
# solution of y' = f(x,y) (that passes through (x0,y0) and goes from x0 to xf);
# f is in Dy.
# soledif1(x0,y0,xf,N,col) traces it slim.
 
# diredif(a,b,c,d, m,n) draws (for a<=x<=b, c<=y<=d) the direction field with m rows
# and n columns
#           [you have to use y and Dy to describe the differential equation;
#            instead of x you can use another variable]
 
# Example: y' = x-y such that (x0,y0) = (-1,3), for -1 <= x <= 5
 
Dy <- function(x,y) x-y
Plane(-1,5, -2,5)
soledif(-1,3, 5, 10, "brown")
soledif(-1,3, 5, 1e3, "blue")
 
# I understand that with N = 1000 the graph has stabilized. Typically, N = 1e4
 
soledif(4,2.5, -1, 1e4, "red")
 
# It's enough; if the graph has a lot of oscillation it is necessary to increase N
# If I want to see the directional field for 20 rows and 25 columns I do
 
diredif(-1,5, -2,5, 25,20)
 
# If I just want to see this I do not put the "soledif" lines.
 
# Other examples for the same equation (right figure):
 
Plane(-1,5, -2,5)
soledif(1,0, 5, 1e4, "brown"); soledif(1,0, -1, 1e4, "blue")
soledif(2,1.5, 5, 1e4, "brown"); soledif(2,1.5, -1, 1e4, "blue")
soledif(3,1, 5, 1e4, "brown"); soledif(3,1, -1, 1e4, "blue")
 
     
 
# soledifv(x0,y0, p,N) gives the value of  y(p)  approximated with N steps
P=soledifv(3,1, 4,1e5); P; P=soledifv(3,1, 4,1e6); P
#    2.632122                 2.632121
POINT(p,P, "red")         # The red point in the picture

 
# soledifp(x0,y0,xf,N,col) tracks only N + 1 points without linking them
 
Plane(-1,5, -2,5)
soledifp(-1,3, 5, 10, "black")
Plane(-1,5, -2,5)
soledif(-1,3, 5, 10, "red")
soledifp(-1,3, 5, 10, "black")
 
   
 
# In this case (y'=x-y) I know how to solve the equation; the solution is:
f = function(x) x-1+(C+1)*exp(-x)
# 
# The solution such that f(3)=1:
# f(3) = 1  implies   2+(C+1)*exp(-3) = 1, C = -1/exp(-3)-1
C = -1/exp(-3)-1
# I can find the value found before:
f(4)
# 2.632121    OK
# If I want I can compare the graphs of the two solutions
Plane(-1,5, -2,5)
POINT(3,1,"black"); soledif(3,1, 5, 1e4, "brown")
graph1(f, -1,5, "blue")
                        
 
# I can find the solution with WolframAlpha:
# y'(x) = x-y(x), y(3) = 1, y(4)    gives 3-1/exp(1) (= 2.632121)

# Another example: y'(x) = 3·y2/3(x)
Dy <- function(x,y) 3*(y^2)^(1/3)
Plane(-2,2, -3,3)
diredif(-2,2, -3,3, 20,20)
soledif(1,1, 2, 1e4, "seagreen")
soledif(1,1, -2, 1e4, "seagreen")
soledif(0,0, 2, 1e4, "blue")
soledif(0,0, -2, 1e4, "brown")
soledif(-2,-1, -1, 1e4, "brown")
                        
 
# It is easy verify that y(x) = x3 is a solution such that y(1)=1, but this is not the
# only solution! (WolframAlpha gives only this solution!).
# This example shows how useful it is to trace the directional field.

# A third example: y'(x) = x/y(x) such that y(-3)=2; we want find the value of y(-1.5).
Dy <- function(x,y) x/y
Plane(-3,1, -2,2)
diredif(-3,1, -2,2, 22,22)
POINT(-3,2, "black")
                        
# From the directional field I can deduce that there is no solution.
# Let's face the problem.
soledif(-3,2, -1.5, 1e5, "seagreen")
# I obtain the green graph; and if I look for the solution I find:
soledifv(-3,2, -1.5, 1e5)
# -2.525611
# I understand (from the graph and from this value) that near -2.5 ends the domain of
# the solution. The algorithm stops when the slope of the approximate curve-solution
# changes the sign. In soledifvc there is the couple of points where this happens: 
soledifvc
# -2.23600500 -2.23599000 -0.07502041 -0.07457333
# I can improve the approximation of the value:
soledifv(-3,2, -2.23, 1e5)
# -0.06826086
soledifvc
# -2.236036800 -2.236029100 -0.007246153 -0.004870067
POINT(-2.236,0, "brown")

# Another question. If there are some points where the equation is non defined, it may
# happen that they are not identified by the program.
# An example with the same equation:
PLANE(-3,3, -3,3)
diredif(-3,3, -3,3, 20,20)
soledif(-3,3, 3, 1e4, "brown")
soledif(-3,-3, 3, 1001, "blue")
                            
# It may happen that the program overrides x=0 where the equation is not defined.
#
# Of course, the simplest differential equations are indefinite integrals, which can be
# studied with simpler methods (see). To trace the directional field it is useful to
# solve other simple differential equations, ot to better understand the solution. An
# example. The differential model f'(x) = f(x) [that is the differential equation y' =
# y] is easy to solve: I know that y=exp(x) is a solution, and that [because Dx(k*f(x))
# = k*Dxf(x)] y(x) = k*exp(x) is also a solution (for every number k). Let's check.
Dy <- function(x,y) y
Plane(-2,2, -5,5); diredif(-2,2, -5,5, 15,15)
soledif(0,1, 2, 1e5, "brown"); soledif(0,1, -2, 1e5, "brown")
soledif(-0.5,-2, 2, 1e5, "seagreen"); soledif(-0.5,-2, -2, 1e5, "seagreen")
# Solving the differential equation:
# k*exp(1)=-2 => k=-2/exp(1)
g=function(x) -2/exp(1)*exp(x); POINT(1,-2,"brown")
graph1(g, -2,2, "black")
                       
#  A problem:   
# A run-down condenser has capitance C = 80 μF and is connected to a battery of 45 V
# with a resistor of resistance R = 500 Ω. What is the final charge on the condenser?
 
V=45; C=80*10^-6; R=500       # y is Q (= C*V);   Q(0)=0 is the initial condition
Dy <- function(t,y) (V-y/C)/R
 
# Try looking for the solution with a particular graphic scale.
 
Plane(0,10, 0,1); soledif1(0,0, 10, 1e5, "blue")
   
# Then, based on the results, I modify it.
Plane(0,10, 0,0.05); soledif1(0,0, 10, 1e5, "blue")
 
# I modify it.
 
Plane(0,0.35, 0,0.004); soledif1(0,0, 0.35, 1e5, "blue")
 
# I look for the limit (from 0 to 0.35 or - better - from 0 to 1)
soledifv(0,0, 1, 1e5)
 
# 0.0036
 
coldash="brown";  segm(-1,0.0036, 0.4, 0.0036, 0)
abovex("t"); abovey("Q")
                      
 
# It is not difficult to solve this equation "by hand".
# Let's see how to solve it with Wolfram Alpha
 
# I put    Q(t)/C + R*dQ(t)/dt = V, Q(0)=0
# [or      dy(t)/dt = (V-y(t)/C)/R, y(0)=0   ]
 
# I obtain:    Q(t) = C*V*(1-exp(-t/(C*R)))
 
# Verification:
 
F = function(t) C*V*( 1-exp(-t/(C*R)) ); graph1(F, 0,0.35, "brown")
 
# I get a graph that overlaps with the previous one!

# For second-order differential equations see here.

Other examples of use