Graphics Programs Reference
In-Depth Information
% 1st-order differential equations
% F(x,y) = [dy1/dx dy2/dx dy3/dx ...].
% x,y = initial values; y must be row vector.
% xStop = terminal value of x.
% h = increment of x used in integration.
% OUTPUT:
% xSol = x-values at which solution is computed.
% ySol = values of y corresponding to the x-values.
ifsize(y,1)>1;y=y';end %ymustberowvector
xSol = zeros(2,1); ySol = zeros(2,length(y));
xSol(1) = x; ySol(1,:) = y;
i=1;
whilex<xStop
i=i+1;
h = min(h,xStop - x);
K1 = h*feval(dEqs,x,y);
K2 = h*feval(dEqs,x + h/2,y + K1/2);
K3 = h*feval(dEqs,x + h/2,y + K2/2);
K4 = h*feval(dEqs,x+h,y + K3);
y=y+(K1+2*K2+2*K3+K4)/6;
x=x+h;
xSol(i) = x; ySol(i,:) = y; % Store current soln.
end
EXAMPLE 7.3
Use the second-order Runge-Kuttamethod to integrate
y
=
=
sin
y
(0)
1
from
x
=
0to0
.
5instepsof
h
=
0
.
1. Keep four decimal places in the computations.
Solution
In this problemwehave
f
(
x
,
y
)
=
sin
y
so that the integration formulas in Eqs. (7.9) are
K
1
=
hf
(
x
,
y
)
=
0
.
1 sin
y
hf
x
2
K
1
1 sin
y
2
K
1
h
2
,
1
1
K
2
=
+
y
+
=
0
.
+
y
(
x
+
h
)
=
y
(
x
)
+
K
2
Search WWH ::
Custom Search