summaryrefslogtreecommitdiff
path: root/coursework17/midpoint.m
diff options
context:
space:
mode:
authorVasil Zlatanov <v@skozl.com>2017-02-17 14:43:50 +0000
committerVasil Zlatanov <v@skozl.com>2017-02-17 14:43:50 +0000
commitabcd65d98b8a4830673d53eaa9665ad195b70b03 (patch)
tree8da09311592df2779f7da88d551e384fbe08ce7f /coursework17/midpoint.m
parentd4660bb996f1177593d90ac4c518c436fe621156 (diff)
downloade2-matlab-abcd65d98b8a4830673d53eaa9665ad195b70b03.tar.gz
e2-matlab-abcd65d98b8a4830673d53eaa9665ad195b70b03.tar.bz2
e2-matlab-abcd65d98b8a4830673d53eaa9665ad195b70b03.zip
work in progress solution for exercise 1 of coursework
Diffstat (limited to 'coursework17/midpoint.m')
-rw-r--r--coursework17/midpoint.m25
1 files changed, 25 insertions, 0 deletions
diff --git a/coursework17/midpoint.m b/coursework17/midpoint.m
new file mode 100644
index 0000000..44ccdae
--- /dev/null
+++ b/coursework17/midpoint.m
@@ -0,0 +1,25 @@
+function [time_array, Vout_array] = midpoint(R, L, Vin, current_initial, step, t_final)
+ % Determine how many times to step forward
+ N = round(t_final/step);
+
+ % Initialise some arrays for the output
+ current_array=zeros(1,N);
+ time_array=zeros(1,N);
+ current_array(1) = current_initial;
+ Vout_array(1) = Vin(0) - R*current_initial;
+
+ % Given the input voltage as a function of time we can express
+ % dI/dt (dcurrent_dt) as a function of time as well (given R and L)
+ dcurrent_dt = @(t, I) (Vin(t) - I*R)/L;
+
+ % Loop as many times as step allows and:
+ % calculate the gradient at point
+ % Use it to emtimate next two points
+
+ for j=1:N-1 % loop N-1 times
+ grad = feval(dcurrent_dt, time_array(j), current_array(j));
+ current_array(j+1)=current_array(j) + step*feval(dcurrent_dt, time_array(j)+step/2, current_array(j)+step/2*grad);
+ time_array(j+1) = time_array(j) + step;
+ Vout_array(j+1) = Vin(time_array(j+1)) - R*current_array(j+1);
+ end
+