blob: e3d91b0d55ac2a6b1e19df8bdb93a75a1ca7b6fe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
|