diff options
author | Vasil Zlatanov <v@skozl.com> | 2017-02-17 14:43:22 +0000 |
---|---|---|
committer | Vasil Zlatanov <v@skozl.com> | 2017-02-17 14:43:22 +0000 |
commit | d4660bb996f1177593d90ac4c518c436fe621156 (patch) | |
tree | 322d237a81de23a9979aa010a9da7c026cc06c0f /general/euler.m | |
download | e2-matlab-d4660bb996f1177593d90ac4c518c436fe621156.tar.gz e2-matlab-d4660bb996f1177593d90ac4c518c436fe621156.tar.bz2 e2-matlab-d4660bb996f1177593d90ac4c518c436fe621156.zip |
general approximation techniques developed from exercise sheets
Diffstat (limited to 'general/euler.m')
-rw-r--r-- | general/euler.m | 25 |
1 files changed, 25 insertions, 0 deletions
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 |