diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-08 23:36:03 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-08 23:36:03 +0000 |
commit | 0d24b5ac48cef35513a12d68c07320cc42e0bdfa (patch) | |
tree | 631bf2401f4978ba2bfe59fc08f314c88d6ec48f | |
parent | dba3bb1c68b4f11435c979998569367c854cbeaf (diff) | |
download | NumericalAnalysis-0d24b5ac48cef35513a12d68c07320cc42e0bdfa.tar.gz NumericalAnalysis-0d24b5ac48cef35513a12d68c07320cc42e0bdfa.zip |
Finished most of bonus too
-rw-r--r-- | Part 3/figures/abs_sin_24_100_tvbc1.png | bin | 0 -> 120382 bytes | |||
-rw-r--r-- | Part 3/figures/abs_sin_24_100_tvbc2.png | bin | 0 -> 90962 bytes | |||
-rw-r--r-- | Part 3/scripts/finite_script.m | 34 | ||||
-rw-r--r-- | Part 3/scripts/finite_script.m~ | 42 | ||||
-rw-r--r-- | Part 3/scripts/get_function.m | 22 | ||||
-rw-r--r-- | Part 3/scripts/get_function.m~ | 7 | ||||
-rw-r--r-- | Part 3/scripts/solvetridiag.m | 34 |
7 files changed, 83 insertions, 56 deletions
diff --git a/Part 3/figures/abs_sin_24_100_tvbc1.png b/Part 3/figures/abs_sin_24_100_tvbc1.png Binary files differnew file mode 100644 index 0000000..39622e4 --- /dev/null +++ b/Part 3/figures/abs_sin_24_100_tvbc1.png diff --git a/Part 3/figures/abs_sin_24_100_tvbc2.png b/Part 3/figures/abs_sin_24_100_tvbc2.png Binary files differnew file mode 100644 index 0000000..92839f3 --- /dev/null +++ b/Part 3/figures/abs_sin_24_100_tvbc2.png diff --git a/Part 3/scripts/finite_script.m b/Part 3/scripts/finite_script.m index 6d8177c..76c682c 100644 --- a/Part 3/scripts/finite_script.m +++ b/Part 3/scripts/finite_script.m @@ -4,10 +4,10 @@ clear; % Set the number of samples to take -N = 24; +N = 60; % Set the total time to run -m = 100; +m = 300; % Declare final size of matrix for speed res = zeros(N+1, m+1); @@ -19,24 +19,8 @@ v = 0.25; h = 1/N; k = h^2 * v; -% Set initial condition to triangle function -count = 1; -for i = 0:h:1 - if i < 0.5 - res(count, 1) = 2*i; - else - res(count, 1) = 2-2*i; - end - count = count + 1; -end - -% Set inital condition to sin function -% res(:, 1) = sin(2*pi*(0:h:1)); - -% Set initial condition to abs of sin -% res(:, 1) = abs(sin(2*pi*(0:h:1))); - - +% Set Initial condition +res(:, 1) = get_function(N, h, 3); % Set boundary conditions res(1, :) = zeros(1, m+1); @@ -49,8 +33,14 @@ for c = 1:m end end -plot(0:h:1, res); +for i = 1:m+1 + if rem(i, 10) == 0 + plot(0:h:1, res(:, i)); + hold on; + end +end +hold off; xlabel('x'); ylabel('y'); -title('Plots of 1D Heat equation over time'); +title('Plots of 1D Heat equation over time, bc = abs(sin(2*pi*x))'); legend('m = 0', 'm = 1', '...', 'm = 99');
\ No newline at end of file diff --git a/Part 3/scripts/finite_script.m~ b/Part 3/scripts/finite_script.m~ new file mode 100644 index 0000000..b568d1c --- /dev/null +++ b/Part 3/scripts/finite_script.m~ @@ -0,0 +1,42 @@ +% This script implements the finite difference method to solve the heat +% equation + +clear; + +% Set the number of samples to take +N = 24; + +% Set the total time to run +m = 100; + +% Declare final size of matrix for speed +res = zeros(N+1, m+1); + +% Set v +v = 0.25; + +% Get h and k +h = 1/N; +k = h^2 * v; + +% Set Initial condition +res(:, 1) = get_function(h, 3); + +% Set boundary conditions +res(1, :) = abs(sin(2*pi*(0:1/m:1))); +res(N+1, :) = abs(sin(2*pi*(0:1/m:1))); + +% Calculate M+1 and plot it continuously +for c = 1:m + for i = 2:N + res(i, c+1) = v * res(i-1, c) + (1-2*v) * res(i, c) + v * res(i+1, c); + end +end + +for i = 1:(N/10-10) +plot(0:h:1, res()); +end +xlabel('x'); +ylabel('y'); +title('Plots of 1D Heat equation over time, constantly increasing boundary conditions'); +legend('m = 0', 'm = 1', '...', 'm = 99');
\ No newline at end of file diff --git a/Part 3/scripts/get_function.m b/Part 3/scripts/get_function.m new file mode 100644 index 0000000..1e1fb8f --- /dev/null +++ b/Part 3/scripts/get_function.m @@ -0,0 +1,22 @@ +function y = get_function(N, h, f) + +y = 0:N; + +if f == 1 + count = 1; + for i = 0:h:1 + if i < 0.5 + y(count, 1) = 2*i; + else + y(count, 1) = 2-2*i; + end + count = count + 1; + end +elseif f == 2 + y = sin(2*pi*(0:h:1)); +else + y = abs(sin(2*pi*(0:h:1))); +end + +end + diff --git a/Part 3/scripts/get_function.m~ b/Part 3/scripts/get_function.m~ new file mode 100644 index 0000000..769c3c9 --- /dev/null +++ b/Part 3/scripts/get_function.m~ @@ -0,0 +1,7 @@ +function y = get_function(bc0, bc1, h, f) + +if f == 1 +el + +end + diff --git a/Part 3/scripts/solvetridiag.m b/Part 3/scripts/solvetridiag.m deleted file mode 100644 index eca7d3f..0000000 --- a/Part 3/scripts/solvetridiag.m +++ /dev/null @@ -1,34 +0,0 @@ -function x = solvetridiag(N,a,b,c,vec) - -% -% -% Use this function to solve a tridiagonal matrix equation -% of the form: Tx=v; -% -% T is a tridiagonal matrix with entries [a, b, c] -% on the three non-zero diagonals -% -% v is the right hand side vector -% -% N is the number of equations -% -% -%************************************************************** - - diag=ones(N,1)*b; - x=zeros(N,1); - - for k = 1:N-1 - p=diag(k); - m=a/p; - diag(k+1)=diag(k+1)-m*c; - vec(k+1)=vec(k+1)-m*vec(k); - end - - x(N)=vec(N)/diag(N); - - for k = N-1:-1:1 - x(k)=(vec(k)-c*x(k+1))/diag(k); - end - -return
\ No newline at end of file |