From 50e2cd5cb4d87ee54b1bd737ab30f98f831e6a7f Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Sat, 25 Feb 2017 19:58:29 +0000 Subject: working solution for ex4 --- coursework17/ex4.m | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 coursework17/ex4.m diff --git a/coursework17/ex4.m b/coursework17/ex4.m new file mode 100644 index 0000000..c3ec32d --- /dev/null +++ b/coursework17/ex4.m @@ -0,0 +1,50 @@ +% Coursework 17 Q4 +% Using dx and dt as h and k are confusing +% h = dx +% k = dt + +dx = input('Input position step (for example 0.01): '); % play about with this to get resolution + +% Calulate maximum dt to maintain stability, based on the tailor expansion. +dt = dx^2/2; + +tfin = input('Input the time you wawnt to end the simulation (for example 0.1): '); + +lines = input('How many lines across the time range would you like to plot (for example 10): '); + +% v = dx/(dt^2); %redundant + +% Create x and t for plotting in the array +x = 0:dx:1; +t = 1:dt:tfin+1; + +% Initialization of temperature matrix advancing in time (rows) and space (columns) +u = zeros(length(t),length(x)); +u(1,:) = 0; +u(1,length(x)) = 0; + +% Initial condition +for i = 1:length(x) + if x(i) <= 0.5 + u(1,i) = 2*x(i);; + else + u(1,i) = 2*(1-x(i)); + end +end + + +for m = 1:length(t) + % Set boundaries + u(m+1,1) = 0; + u(m+1,length(x)) = 0; + for j = 2:(length(x)-1) + % multiply out (1-2v) and factorise out v + u(m+1,j) = u(m,j) + ((dt/(dx^2))*(u(m,j+1) - 2*u(m,j) + u(m,j-1))); + end +end + +figure; +hold on; +for i = 1:round(length(t)/lines):length(t) + plot(x,u(i,:),'.'); +end -- cgit v1.2.3-54-g00ecf