From 0c4597af1ff1c30f4f7df8735fb8e38e1e6e6b86 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Sun, 5 Mar 2017 19:13:21 +0000 Subject: fix exact solution mistake and plot h vs error --- coursework17/error_script.m | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/coursework17/error_script.m b/coursework17/error_script.m index 5a37fe8..f17f234 100644 --- a/coursework17/error_script.m +++ b/coursework17/error_script.m @@ -2,7 +2,8 @@ R = 0.5; % 0.5Ohm L = 0.0015; % 1.5mH -data_points = 10000; +data_points = 100000; +h_divisions = 1000; % Go on for a time constant time_constant = L/R; @@ -17,24 +18,33 @@ Vin = @(t) A*cos(w_c*t); current_initial=0; % e^m is the integratingn factor -m = 0.5/0.0015; +% m = 0.5/0.0015; % Solution is made by multiplying by integrating factor and % then integrating both sides -current_exact = @(t) 3/(m^2+w_c^2)*(2*m*cos(w_c*t) + 2*w_c*sin(w_c*t)); + +A = 6*R/(R^2 + (w_c*L)^2); +B = 6*w_c*L/(R^2 + (w_c*L)^2); + +current_exact = @(t) A*cos(w_c*t) + B*sin(w_c*t) - A*exp((-R/L)*t); +%current_exact = @(t) 3/(m^2+w_c^2)*(2*m*cos(w_c*t) + 2*w_c*sin(w_c*t)); Vout_exact = @(t) Vin(t) - current_exact(t)*R; -[time_array, Vout_array] = ralston(R, L, Vin, current_initial, step, data_points*step); +for k=1:h_divisions -for j=1:data_points - error_array(j) = Vout_exact(time_array(j)) - Vout_array(j); - % Vout_plot(j) = Vout_exact(time_array(j)); -end + [time_array, Vout_array] = ralston(R, L, Vin, current_initial, step*k, data_points*step); -% plot(time_array, Vout_array); -% figure; -% plot(time_array, Vout_plot); -% -% max(Vout_plot-Vout_array) + for j=1:data_points/k + error_array(j) = Vout_exact(time_array(j)) - Vout_array(j); + % Vout_plot(j) = Vout_exact(time_array(j)); + end + + % plot(time_array, Vout_array); + % figure; + % plot(time_array, Vout_plot); + % + max_array(k) = max(error_array); + max_t(k) = k*step; +end -loglog(time_array, error_array); +loglog(max_t, max_array); -- cgit v1.2.3