From d4660bb996f1177593d90ac4c518c436fe621156 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Fri, 17 Feb 2017 14:43:22 +0000 Subject: general approximation techniques developed from exercise sheets --- general/euler.m | 25 +++++++++++++++++++++++++ general/heun.m | 27 +++++++++++++++++++++++++++ general/midpoint.m | 27 +++++++++++++++++++++++++++ general/ralston.m | 27 +++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 general/euler.m create mode 100644 general/heun.m create mode 100644 general/midpoint.m create mode 100644 general/ralston.m diff --git a/general/euler.m b/general/euler.m new file mode 100644 index 0000000..72360b8 --- /dev/null +++ b/general/euler.m @@ -0,0 +1,25 @@ +function [xa, ya] = euler(derivative, x_initial, y_initial, step, x_final) + % Define intial values for x and y + %x_initial=0; + %y_initial=2; + + %% Set the step size between calculations + %step=0.0003; + + %% Define an endpoint + %x_final=1; + + %% Define a dy/dx + %derivative=@(x,y) x+y; + + % Determine how many times to step forward + N=round((x_final-x_initial)/step); % use this determine size + + % Define and initialise arrays to contain coordinates + ya=zeros(1,N); xa=zeros(1,N); + xa(1)=x_initial; ya(1)=y_initial; + + for j=1:N-1 % loop for N-1 steps + ya(j+1)=ya(j) + step*feval(derivative, xa(j),ya(j)); % next value + xa(j+1)=xa(j)+step; % increase x by stepsize + end diff --git a/general/heun.m b/general/heun.m new file mode 100644 index 0000000..e3d91b0 --- /dev/null +++ b/general/heun.m @@ -0,0 +1,27 @@ +function [xa, ya] = heun(derivative, x_initial, y_initial, step, x_final) + % Define intial values for x and y + %x_initial=0; + %y_initial=2; + + %% Set the step size between calculations + %step=0.0003; + + %% Define an endpoint + %x_final=1; + + %% Define a dy/dx + %derivative=@(x,y) x+y; + + % Determine how many times to step forward + N=round((x_final-x_initial)/step); % use this determine size + + % Define and initialise arrays to contain coordinates + ya=zeros(1,N); xa=zeros(1,N); + xa(1)=x_initial; ya(1)=y_initial; + + % da is the value of the derivative at xa, ya + for j=1:N-1 % loop for N-1 steps + da = feval(derivative, xa(j), ya(j)); + ya(j+1)=ya(j) + step/2*(da + feval(derivative, xa(j)+step, ya(j)+step*da)) ; + xa(j+1)=xa(j)+step; % increase x by stepsize + end diff --git a/general/midpoint.m b/general/midpoint.m new file mode 100644 index 0000000..cecef44 --- /dev/null +++ b/general/midpoint.m @@ -0,0 +1,27 @@ +function [xa, ya] = midpoint(derivative, x_initial, y_initial, step, x_final) + % Define intial values for x and y + %x_initial=0; + %y_initial=2; + + %% Set the step size between calculations + %step=0.0003; + + %% Define an endpoint + %x_final=1; + + %% Define a dy/dx + %derivative=@(x,y) x+y; + + % Determine how many times to step forward + N=round((x_final-x_initial)/step); % use this determine size + + % Define and initialise arrays to contain coordinates + ya=zeros(1,N); xa=zeros(1,N); + xa(1)=x_initial; ya(1)=y_initial; + + % da is the value of the derivative at xa, ya + for j=1:N-1 % loop for N-1 steps + da = feval(derivative, xa(j), ya(j)); + ya(j+1)=ya(j) + step*feval(derivative, xa(j)+step/2, ya(j)+step/2*da); + xa(j+1)=xa(j)+step; % increase x by stepsize + end diff --git a/general/ralston.m b/general/ralston.m new file mode 100644 index 0000000..2d18571 --- /dev/null +++ b/general/ralston.m @@ -0,0 +1,27 @@ +function [xa, ya] = ralston(derivative, x_initial, y_initial, step, x_final) + % Define intial values for x and y + %x_initial=0; + %y_initial=2; + + %% Set the step size between calculations + %step=0.0003; + + %% Define an endpoint + %x_final=1; + + %% Define a dy/dx + %derivative=@(x,y) x+y; + + % Determine how many times to step forward + N=round((x_final-x_initial)/step); % use this determine size + + % Define and initialise arrays to contain coordinates + ya=zeros(1,N); xa=zeros(1,N); + xa(1)=x_initial; ya(1)=y_initial; + + % da is the value of the derivative at xa, ya + for j=1:N-1 % loop for N-1 steps + da = feval(derivative, xa(j), ya(j)); + ya(j+1)=ya(j) + step/4*(da + 3*feval(derivative, xa(j)+2/3*step, ya(j)+2/3*step*da)) ; + xa(j+1)=xa(j)+step; % increase x by stepsize + end -- cgit v1.2.3