summaryrefslogtreecommitdiffstats
path: root/assignments/hw02/euler.m
diff options
context:
space:
mode:
Diffstat (limited to 'assignments/hw02/euler.m')
-rw-r--r--assignments/hw02/euler.m26
1 files changed, 26 insertions, 0 deletions
diff --git a/assignments/hw02/euler.m b/assignments/hw02/euler.m
new file mode 100644
index 0000000..1773e6c
--- /dev/null
+++ b/assignments/hw02/euler.m
@@ -0,0 +1,26 @@
+% % 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
+
+a = 20 ;
+b = 0.5 ;
+c = 5 ;
+
+dt = 0.2 ;
+tlast = 20 ; % s
+
+iterations = round(tlast/dt) ;
+xall = zeros(iterations,1) ;
+
+x = c ;
+for i = 1:iterations
+ xall(i) = x ;
+ dxdt = a - b*x ;
+ x = x + dxdt*dt ;
+end % of this time step
+
+time = dt*(0:iterations-1)' ;
+figure
+plot(time,xall)