Docsity

Project 1 - Ordinary Differential Equations - Solved Problems | MATH 2280, Study Guides, Projects, Research of Mathematics

Toggle sidebar
bg1
O
O
O
O
(2)
O
O
O
(1)
O
Math 2280-2, Project #1, Problem #1
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,
4.103],[1980,4.473],[1985,4.882],[1990,5.249],[1995,5.679],
[2000,6.127]]);
#matrix of populations (billions)
pops :=
1960 3.049
1965 3.358
1970 3.721
1975 4.103
1980 4.473
1985 4.882
1990 5.249
1995 5.679
2000 6.127
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
3.358 1
3.721 1
4.103 1
4.473 1
4.882 1
5.249 1
5.679 1
0.02001191185, 0.02002149960, 0.01832805264, 0.01741560474, 0.01589512495,
0.01518384454, 0.01546046839
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Project 1 - Ordinary Differential Equations - Solved Problems | MATH 2280 and more Study Guides, Projects, Research Mathematics in PDF only on Docsity!

O

O

O

O

O

O

O

O

Math 2280 - 2 , Project # 1 , Problem # 1

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 ,

  1. 103 ],[ 1980 , 4. 473 ],[ 1985 , 4. 882 ],[ 1990 , 5. 249 ],[ 1995 , 5. 679 ], [ 2000 , 6. 127 ]]); #matrix of populations (billions)**

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

  1. 358 1

  2. 721 1

  3. 103 1

  4. 473 1

  5. 882 1

  6. 249 1

  7. 679 1

  8. 02001191185 , 0. 02002149960 , 0. 01832805264 , 0. 01741560474 , 0. 01589512495 ,

  9. 01518384454 , 0. 01546046839

O

O

O

O

O

O

O

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");*

P

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**

O

O

s

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

O

O

Math 2280 - 2 , Project # 1 , Problem 2

(a) Improved Euler's method

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

  1. 1 , 1. 1051690900 , 1. 1051709180 , 0. 0000018280
  2. 2 , 1. 2213987170 , 1. 2214027580 , 0. 0000040410
  3. 3 , 1. 3498521080 , 1. 3498588080 , 0. 0000067000
  4. 4 , 1. 4918148260 , 1. 4918246980 , 0. 0000098720
  5. 5 , 1. 6487076330 , 1. 6487212710 , 0. 0000136380
  6. 6 , 1. 8221007140 , 1. 8221188000 , 0. 0000180860
  7. 7 , 2. 0137293870 , 2. 0137527070 , 0. 0000233200
  8. 8 , 2. 2255114730 , 2. 2255409280 , 0. 0000294550
  9. 9 , 2. 4595664900 , 2. 4596031110 , 0. 0000366210
  10. 0 , 2. 7182368590 , 2. 7182818280 , 0. 0000449690

O

O

O

O

Math 2280 - 2 , Project # 1 , Problem 3 ( 2. 4. 29 in textbook)

(a) We need to solve

dy dx

C

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).

(b) We use Eulers method with a step size that is too large (h= 0. 5 ) and that misses the singularity of

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)

    1. 85 , 1. 0214285710 , 1. 0234886020
    1. 70 , 1. 0471788710 , 1. 0522740280
    1. 55 , 1. 0792353670 , 1. 0891583980
    1. 40 , 1. 1212834980 , 1. 1398522810
    1. 25 , 1. 1813522570 , 1. 2190136540
    1. 10 , 1. 2826110220 , 1. 3894954940
    2. 05 , 1. 5574562410 , 1. 5341274050

O

O

O

(c) Using Eulers method with 50 points (h= 0. 03 )

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

display current values and compare to true sol.

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)

    1. 85 , 1. 0230525210 , 1. 0234886020
    1. 70 , 1. 0511872810 , 1. 0522740280
    1. 55 , 1. 0870173480 , 1. 0891583980
    1. 40 , 1. 1357655930 , 1. 1398522810
    1. 25 , 1. 2103714660 , 1. 2190136540
    1. 10 , 1. 3612350040 , 1. 3894954940
    2. 05 , 1. 8721276780 , 1. 5341274050
    3. 20 , 1. 4711426700 , 1. 2584989510
    4. 35 , 1. 3506362150 , 1. 1618047200
    5. 50 , 1. 2807638240 , 1. 1040895140

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

O

x

K 1. 0 K 0. 5 0 0. 5 1. 0

ytrue

O

O

O

Math 2280 - 2 , Project # 1 , Problem 4

The objective of this problem is to implement a different second order method, called Runge Kutta of

order 2.

Improved Euler's method

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

  1. 000000 , 3. 1000000000 , 3. 1415926536 , 0. 0415926536
  2. 000000 , 3. 1311764706 , 3. 1415926536 , 0. 0104161830
  3. 000000 , 3. 1389884945 , 3. 1415926536 , 0. 0026041591
  4. 000000 , 3. 1409416120 , 3. 1415926536 , 0. 0006510415
  5. 000000 , 3. 1414298932 , 3. 1415926536 , 0. 0001627604

O

O

O

O

[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

(b) Plot of both methods compared (Euler: full line, RK 2 : dotted

line)

display({errfitplot 1 ,errplot 1 ,errfitplot 2 ,errplot 2 },caption= "error plot",labels=["log(n)","log(error)"]);

log(n)

log(error)

K 12

K 11

K 10

K 9

K 8

K 7

K 6

K 5

K 4

error plot

(c) We look at the slopes of the lines we fitted through the error plots for both methods:

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.

O

O

O

Math 2280 - 2 , Project # 1 , Problem 5

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

  1. 0 , 37. 15469
  2. 0 , 26. 24277
  3. 0 , 15. 94529
  4. 0 , 6. 00406
  5. 0 , - 3. 80200
  6. 0 , - 13. 51045
  7. 0 , - 22. 93557
  8. 0 , - 31. 89841
  9. 0 , - 40. 25568
  10. 0 , - 47. 90661

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**

O

O

O

K 40

K 30

K 20

K 10

(a) to obtain the value where v= 0 , we run improved Euler's method and select the time for which v is

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 :**

O

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