### Euler's Method ###
library(phaseR)
## -------------------------------------------------------------------------------
## phaseR: Phase plane analysis of one- and two-dimensional autonomous ODE systems
## -------------------------------------------------------------------------------
##
## v.2.1: For an overview of the package's functionality enter: ?phaseR
##
## For news on the latest updates enter: news(package = "phaseR")
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.5
# Specify beginning and ending times and step size
# to create sequence of time values
# We'll create two different sequences for comparison of step sizes,
# but in general can just change 'by' and rerun script
ss <- 10
ss2 <- 1
t <- seq(0, 200, by = ss)
t2 <- seq(0, 200, by = ss2)
# Specify initial condition
x0 <- 3.93
r<- 0.03
K<- 251.86
# Function to be integrated (right-hand expression of the derivative above)
ex <- function(x){r*x*(1-(x/K))}
# Create empty vector to store results; put initial condition in first position
# Again, we'll create two vectors for comparison
x <- c()
x[1] <- x0
x2 <- c()
x2[1] <- x0
# This loop implements the Euler's Method algorithm
# Loop over time, approximating function at each time step
for (i in 1:(length(t)-1)){
x[i+1] = x[i] + ss * ex(x[i])
}
for (i in 1:(length(t2)-1)){
x2[i+1] = x2[i] + ss2 * ex(x2[i])
}
# Produce a table of output
data.frame(setNames(t, x))
## setNames.t..x.
## 3.93 0
## 5.09060299372667 10
## 6.58691645893782 20
## 8.51131093711345 30
## 10.978415311075 40
## 14.1283772869801 50
## 18.1291261856566 60
## 23.1763784347601 70
## 29.4894787546531 80
## 37.3004738653405 90
## 46.8333556216485 100
## 58.2707641995305 110
## 71.7075060929195 120
## 87.0949666517576 130
## 104.18804028573 140
## 122.514474123318 150
## 141.390078526144 160
## 159.994879850648 170
## 177.502164287352 180
## 193.22360886456 190
## 206.71912435648 200
data.frame(setNames(t2, x2))
## setNames.t2..x2.
## 3.93 0
## 4.04606029937267 1
## 4.16549214361794 2
## 4.28839012580961 3
## 4.41485129239604 4
## 4.54497519471108 5
## 4.67886394080249 6
## 4.81662224752296 7
## 4.95835749282422 8
## 5.10417976818943 9
## 5.25420193113434 10
## 5.40853965770176 11
## 5.56731149486842 12
## 5.73063891277718 13
## 5.8986463567012 14
## 6.07146129864005 15
## 6.24921428844089 16
## 6.43203900433033 17
## 6.62007230273549 18
## 6.81345426726422 19
## 7.01232825670689 20
## 7.21684095191308 21
## 7.42714240138783 22
## 7.64338606544282 23
## 7.86572885872842 24
## 8.09433119096224 25
## 8.3293570056601 26
## 8.57097381666436 27
## 8.81935274225387 28
## 9.07466853660849 29
## 9.33709961838974 30
## 9.6068280961874 31
## 9.88403979056952 32
## 10.1689242524616 33
## 10.4616747775675 34
## 10.7624884165328 35
## 11.0715659805373 36
## 11.3891120419916 37
## 11.7153349299998 38
## 12.0504467202342 39
## 12.3946632188605 40
## 12.7482039401334 41
## 13.1112920772726 42
## 13.4841544662164 43
## 13.8670215418369 44
## 14.2601272861892 45
## 14.6637091683556 46
## 15.0780080754351 47
## 15.5032682342165 48
## 15.9397371230668 49
## 16.3876653735547 50
## 16.8473066613243 51
## 17.3189175857244 52
## 17.8027575376976 53
## 18.2990885554256 54
## 18.808175167227 55
## 19.330284221205 56
## 19.8656847011399 57
## 20.4146475281292 58
## 20.9774453474816 59
## 21.5543523003813 60
## 22.1456437798482 61
## 22.7515961705377 62
## 23.3724865719359 63
## 24.0085925045316 64
## 24.6601915985691 65
## 25.3275612650113 66
## 26.0109783483798 67
## 26.7107187611706 68
## 27.4270570995868 69
## 28.1602662403743 70
## 28.9106169185969 71
## 29.6783772862421 72
## 30.463812451608 73
## 31.2671839994888 74
## 32.0887494922451 75
## 32.928761951922 76
## 33.7874693236592 77
## 34.6651139207242 78
## 35.561931851591 79
## 36.4781524295856 80
## 37.4139975657215 81
## 38.3696811454549 82
## 39.3454083902048 83
## 40.3413752045975 84
## 41.3577675105173 85
## 42.3947605691703 86
## 43.4525182924968 87
## 44.5311925453978 88
## 45.6309224403754 89
## 46.7518336263208 90
## 47.8940375733194 91
## 49.0576308554771 92
## 50.2426944339066 93
## 51.4492929421427 94
## 52.6774739763873 95
## 53.9272673931061 96
## 55.19868461662 97
## 56.4917179594458 98
## 57.8063399582455 99
## 59.1425027283379 100
## 60.500137339811 101
## 61.879153218347 102
## 63.2794375739278 103
## 64.7008548606383 104
## 66.143246270807 105
## 67.6064292667403 106
## 69.090197153295 107
## 70.5943186945101 108
## 72.1185377774678 109
## 73.6625731264878 110
## 75.2261180706658 111
## 76.8088403676517 112
## 78.4103820864288 113
## 80.0303595516891 114
## 81.668363352219 115
## 83.3239584154994 116
## 84.996684150491 117
## 86.6860546603256 118
## 88.3915590263436 119
## 90.1126616646269 120
## 91.8488027558564 121
## 93.5993987489962 122
## 95.3638429389527 123
## 97.1415061180029 124
## 98.9317373004066 125
## 100.733864519243 126
## 102.547195694122 127
## 104.371019568035 128
## 106.204606711218 129
## 108.047210589516 130
## 109.898068694364 131
## 111.756403731124 132
## 113.621424862166 133
## 115.492329000744 134
## 117.368302151396 135
## 119.248520792302 136
## 121.132153294755 137
## 123.01836137468 138
## 124.906301570882 139
## 126.795126744573 140
## 128.683987594535 141
## 130.572034182202 142
## 132.458417460847 143
## 134.342290803036 144
## 136.222811520512 145
## 138.099142370706 146
## 139.970453044171 147
## 141.83592162731 148
## 143.69473603498 149
## 145.54609540768 150
## 147.389211468303 151
## 149.223309833644 152
## 151.047631276151 153
## 152.861432931723 154
## 154.663989449659 155
## 156.454594081237 156
## 158.232559703746 157
## 159.99721977719 158
## 161.74792923125 159
## 163.484065280514 160
## 165.205028166352 161
## 166.910241824241 162
## 168.599154475718 163
## 170.271239144541 164
## 171.925994097029 165
## 173.562943206895 166
## 175.181636245264 167
## 176.781649096907 168
## 178.362583904037 169
## 179.924069139304 170
## 181.465759609936 171
## 182.9873363952 172
## 184.488506719604 173
## 185.969003764483 174
## 187.428586420756 175
## 188.867038985862 176
## 190.284170807948 177
## 191.679815880563 178
## 193.053832391125 179
## 194.406102226565 180
## 195.736530439523 181
## 197.045044678544 182
## 198.331594585679 183
## 199.596151164904 184
## 200.838706124709 185
## 202.059271198169 186
## 203.257877443723 187
## 204.434574529808 188
## 205.589430006409 189
## 206.722528566449 190
## 207.833971299853 191
## 208.923874942986 192
## 209.992371126018 193
## 211.039605620658 194
## 212.065737590534 195
## 213.070938846359 196
## 214.05539310789 197
## 215.01929527452 198
## 215.962850706207 199
## 216.886274516305 200
# Plot approximation, using bullet points at each approximated value
plot(x~t, type="l", col = "blue")
points(t, x, pch = 20, col = "blue")
# Add actual solution to graph
curve(989.5 / (247.85*exp(-.03*x)+3.93), add=T)
# Add other approximation to graph,
# using bullet points at each approximated value
# and connecting them with line segments
points(t2, x2, pch = 20, col = "red")
lines(t2, x2, pch = 20, col = "red")
