










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











restart:
We use table 2. 1. 11 for the world population data
**with(linalg): with(plots): pops:=matrix( 9 , 2 ,[[ 1960 , 3. 049 ],[ 1965 , 3. 358 ],[ 1970 , 3. 721 ],[ 1975 ,
pops :=
Here we compute a table with the population and the central differences approximation to the derivative *for i from 1 to 7 do lspoints[i]:=[pops[i+ 1 , 2 ], (pops[i+ 2 , 2 ]-pops[i, 2 ])/( 10 pops[i+ 1 , 2 ])]: od:
A:=matrix( 7 , 2 ): B:=vector( 7 ): for i from 1 to 7 do A[i, 1 ]:=lspoints[i][ 1 ]: A[i, 2 ]:= 1 : B[i]:=lspoints[i][ 2 ]: od:
evalm(A);evalm(B); #check work
358 1
721 1
103 1
473 1
882 1
249 1
679 1
02001191185 , 0. 02002149960 , 0. 01832805264 , 0. 01741560474 , 0. 01589512495 ,
01518384454 , 0. 01546046839
c:=linsolve(transpose(A)&(A),transpose(A)&B); #least squares solution**
c := K^0.^002379351595 0.^02816897209 b:=c[ 1 ]; # slope a:=c[ 2 ]; # ordinate at the origin b := K 0. 002379351595 a := 0. 02816897209
pict 1 :=pointplot({seq( lspoints[i],i= 1 .. 7 )}): line:=plot(a+bP,P= 2 .. 6 , color=black): display({pict 1 ,line}, title="data fitting for logistic eq parameters");*
Figure 2. 1. 10 shows that the logistic model is a pretty good one for the U.S. population in the 1800 's.
Let's use the "a" and "b" which we found with the least squares fit, use the population in 1900 for our
initial condition, and see how the logistic model works when we try to extend it to the 1900 's:
with(DEtools): #load the DE package deqtn 1 :=diff(x(t),t)=ax(t) + bx(t)^ 2 ; #logistic eqtn with our parameters**
We can now estimate the population in the year 2025 by using our model. Notice how we don't evaluate
x( 2025 ), which would give us a wrong answer because the model so that t= 0 corresponds to 1995.
printf("Estimated population in 2025 is % 5. 3 f billion\n",f( 2025
- 1995 )); Estimated population in 2025 is 8. 076 billion
x 0 := 0 ; xn:= 1 .; # first and last points in the interval y 0 := 1 ; # initial condition n:= 100 ; # number of steps h:=(xn-x 0 )/n; # step size x 0 := 0
xn := 1.
y 0 := 1
n := 100
h := 0. 01000000000
f:=(x,y)->y; # slope function (rhs in DE dy/dx = f(x,y), here f (x,y)=y) x:=x 0 ; y:=y 0 ; printf("% 4 s, % 15 s, % 15 s, % 15 s\n","x_i","y_i","exp(x_i)","error") ; for i from 1 to n do k 1 := f(x,y): # left hand slope k 2 := f(x+h,y+hk 1 ): # approximation to right hand slope k:=(k 1 +k 2 )/ 2 : # averaged slope y:= y + hk: # improved Euler update x:= x+h: # increase x if frac(i/ 10 )= 0 then printf("% 4. 1 f, % 15. 10 f, % 15. 10 f, % 15. 10 f\n",x,y,exp(x),abs(y- exp(x))); end if: end do: # i do loop** f := x , y / y
x := 0
y := 1 x_i, y_i, exp(x_i), error
dy dx
7 x
y = 0 with initial condition y K 1 = 1.
We use separation of variables which gives:
y
d y = K
7 x
d x C C , or equivalently
ln y = K
ln x C C. Since near x =K 1 we have y> 0 and x< 0 , we get ln y = K
ln K x C C ,
whereby using the initial condition we get C = 0. Thus y x =
K x
1 7
. (the formula in the book is
incorrect, as a fractional power of a negative number gives an imaginary number).
the solution
restart: # Clear Maple's internal memory with(LinearAlgebra): with(plots): x 0 :=- 1 .; xn:= 0. 5 ; # first and last points in the interval y 0 := 1 ; # initial condition n:= 10 ; # number of steps h:=(xn-x 0 )/n; # step size x 0 := K 1.
xn := 0. 5
y 0 := 1
n := 10
h := 0. 1500000000
f:=(x,y)->-y/( 7 x); # slope function (rhs in DE dy/dx = f(x,y) ) x:=x 0 ; y:=y 0 ; # initialize x,y xs 1 :=Vector(n+ 1 ): ys 1 :=Vector(n+ 1 ): xs 1 [ 1 ]:=x 0 : ys 1 [ 1 ]:=y 0 : printf("% 15 s, % 15 s, % 15 s\n","x_i","y_i","y(x_i)"): for i from 1 to n do k:= f(x,y): # current slope y:= y + hk: # new y value (Euler's method) x:= x+h: # increase x printf("% 15. 2 f, % 15. 10 f, % 15. 10 f\n",x,y, 1 /abs(x)^( 1 / 7 )); xs 1 [i+ 1 ]:=x: ys 1 [i+ 1 ]:=y; od: # end for i loop
f := x , y /K
y x
x := K 1.
y := 1 x_i, y_i, y(x_i)
x 0 :=- 1 .; xn:= 0. 5 ; # first and last points in the interval y 0 := 1 ; # initial condition n:= 50 ; # number of steps h:=(xn-x 0 )/n; # step size x 0 := K 1.
xn := 0. 5
y 0 := 1
n := 50
h := 0. 03000000000
**f:=(x,y)->-y/( 7 x); # slope function (rhs in DE dy/dx = f(x,y) ) x:=x 0 ; y:=y 0 ; # initialize x,y xs 2 :=Vector(n+ 1 ): ys 2 :=Vector(n+ 1 ): xs 2 [ 1 ]:=x 0 : ys 2 [ 1 ]:=y 0 : printf("% 15 s, % 15 s, % 15 s\n","x_i","y_i","y(x_i)"): for i from 1 to n do k:= f(x,y): # current slope y:= y + hk: # new y value (Euler's method) x:= x+h: # increase x if frac(i/ 5 )= 0 then
printf("% 15. 2 f, % 15. 10 f, % 15. 10 f\n",x,y, 1 /abs(x)^( 1 / 7 )); end if; xs 2 [i+ 1 ]:=x: ys 2 [i+ 1 ]:=y; od: # end for i loop**
f := x , y /K
y x
x := K 1.
y := 1 x_i, y_i, y(x_i)
Euler's method with h= 0. 006 (n= 250 )
x 0 :=- 1 .; xn:= 0. 5 ; # first and last points in the interval y 0 := 1 ; # initial condition n:= 250 ; # number of steps h:=(xn-x 0 )/n; # step size x 0 := K 1.
xn := 0. 5
y 0 := 1
n := 250
The objective of this problem is to implement a different second order method, called Runge Kutta of
order 2.
restart: # Clear Maple's internal memory with(LinearAlgebra): with(plots): Digits:= 16 :
We will now execute improved Euler's method for several values of n and see the error for y( 1 ) with
respect to the true value which is pi.
f:=(x,y)-> 4 /( 1 +x^ 2 ); # slope function => solution to y'(x)=f (x,y) is y(x) 4 atan(x) x 0 := 0 ; xn:= 1 .; # first and last points in the interval y 0 := 0 ; # initial conditional x:=x 0 ; y:=y 0 ; # initialize x,y printf("% 15 s,% 15 s,% 15 s,% 15 s\n", "n","y","true","error"): # fixed column width printing mmax:= 7 : # number of experiments to do errors:=Vector(mmax): ns:=Vector(mmax): # vectors to store the errors and values of n for m from 1 to mmax do # loop over experiments n:= 2 ^m: # the number of points for this experiment h:= (xn-x 0 )/n: x:=x 0 : y:=y 0 : for i from 1 to n do k 1 := f(x,y): # left hand slope k 2 := f(x+h,y+hk 1 ): # approximation to right hand slope k:= (k 1 +k 2 )/ 2 : # averaged slope y:= y+k*h: # improved Euler update x:= x+h: # increase x od: # end for i loop printf("% 15 f,% 15. 10 f,% 15. 10 f,% 15. 10 f\n", n,y,evalf(Pi),abs(y-evalf(Pi))): # fixed column width printing errors[m]:=abs(y-evalf(Pi)): ns[m]:=n: # store errors and ns for later use od: # end for m loop
f := x , y /
1 C x
2
x 0 := 0
xn := 1.
y 0 := 0
x := 0
y := 0 n, y, true, error
[slope,intercept] errfitplot 2 := plot(alpha 2 [ 1 ]*t+alpha 2 [ 2 ],t=log( 2 )..log( 2 ^mmax),linestyle= dot): # linear plot for future use
display({errfitplot 1 ,errplot 1 ,errfitplot 2 ,errplot 2 },caption= "error plot",labels=["log(n)","log(error)"]);
alpha 2 [ 1 ], alpha 1 [ 1 ]; K 1. 999447519927240 , K 1. 999720249100162
since both are very close to - 2 , we can say that RK 2 is a second order accurate method, as improved
Euler's method.
We first solve the crossbow example 2. 3. 3 using improved Euler's method with n= 100
with(LinearAlgebra): with(plots): Digits:= 16 : t 0 := 0 .; tn:= 10 .; # first and last points in the interval v 0 := 49 .; # initial velocity n:= 100 ; # number of steps h:=(tn-t 0 )/n; # step size f:=(t,v)-> - ( 0. 0011 )vabs(v) - 9. 8 ; # slope function (rhs in DE dy/dx = f(x,y)) v:=v 0 : t:=t 0 : ts 1 :=Vector(n+ 1 ): vs 1 :=Vector(n+ 1 ): ts 1 [ 1 ]:=t 0 : vs 1 [ 1 ]:=v 0 : printf("% 15 s, % 15 s\n","t","v"): for i from 1 to n do k 1 := f(t,v): # left hand slope k 2 := f(t+h,v+hk 1 ): # approximation to right hand slope k:=(k 1 +k 2 )/ 2 : # averaged slope v:= v + hk: # improved Euler update t:= t+h: # increase x ts 1 [i+ 1 ]:=t: vs 1 [i+ 1 ]:=v: if frac(i/ 10 )= 0 then printf("% 15. 1 f, % 15. 5 f\n",t,v): end if: od: # end for i loop** t 0 := 0.
tn := 10.
v 0 := 49.
n := 100
h := 0. 1000000000000000
f := t , v /K 0. 0011 v v K 9. 8 t, v
And now with 200 points
t 0 := 0 .; tn:= 10 .; # first and last points in the interval v 0 := 49 .; # initial velocity n:= 200 ; # number of steps h:=(tn-t 0 )/n; # step size f:=(t,v)-> - ( 0. 0011 )vabs(v) - 9. 8 ; # slope function (rhs in DE dy/dx = f(x,y)) v:=v 0 : t:=t 0 : ts 2 :=Vector(n+ 1 ): vs 2 :=Vector(n+ 1 ): ts 2 [ 1 ]:=t 0 : vs 2 [ 1 ]:=v 0 : printf("% 15 s, % 15 s\n","t","v"): for i from 1 to n do k 1 := f(t,v): # left hand slope k 2 := f(t+h,v+hk 1 ): # approximation to right hand slope k:=(k 1 +k 2 )/ 2 : # averaged slope v:= v + hk: # improved Euler update**
closest to 0. Since all computations are already stored in a vector here, we can compute the minimum in
Maple (as done below). Normally (in higher dimensional problems) you do not want to store the value
of your solution at all points, so the numerical method's loop could be modified to find the time for
which v is minimum (with no additional memory cost).
# find the index of the minimum value in the vector vs 2 res:=rtable_scanblock( abs(vs 2 ), [rtable_dims(vs 2 )],(val,ind, res) - > if(val - ( 0. 0011 )vabs(v) - 9. 8 ; # slope function (rhs in DE dy/dx = f(x,y)) v:=v 0 : t:=t 0 :**
for i from 1 to n do k 1 := f(t,v): # left hand slope k 2 := f(t+h,v+hk 1 ): # approximation to right hand slope k:=(k 1 +k 2 )/ 2 : # averaged slope v:= v + hk: # improved Euler update t:= t+h: # increase x od: # end for i loop printf("v( % 4. 2 f ) approx. equals to % 4. 2 f m/s\n",t,v);** t 0 := 0.
tn := 9. 41
v 0 := 49.
n := 200
h := 0. 04705000000000000
f := t , v /K 0. 0011 v v K 9. 8
v( 9. 41 ) approx. equals to - 43. 48 m/s