summaryrefslogtreecommitdiffstats
path: root/assignments/hw02/euler.jl
blob: a9513bf53a509b455c173e3414fc2c9c0b2bfb64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# Use Euler's method to integrate simple one variable ODE
# Use program as template for more interesting models

# Note:  produces slightly different output compared with 
# example shown in class.  Different values of b, dt, tlast

using Winston

a = 20
b = 0.5
c = 5
 
dt    = 0.2
tlast = 20
 
iterations = integer(round(tlast/dt))
xall = zeros(Float64, (iterations, 1))

x = c
for i = 1:iterations
  xall[i] = x
  dxdt = a - b*x
  x = x + dxdt*dt
end

time = dt*(0:iterations-1)'
figure
plot(time,xall)