📄 rk4ada.mht
字号:
From: <Saved by Windows Internet Explorer 7>
Subject:
Date: Tue, 12 May 2009 09:54:21 -0700
MIME-Version: 1.0
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: 7bit
Content-Location: http://www.mece.ualberta.ca/Courses/mec390/390code/rk4ada.m
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.6000.16825" name=GENERATOR></HEAD>
<BODY><PRE>function [t,y] = rk4ada(func, a, b, y0, h1,errmax);
% solution of 1st order ODE using RK 4th order with adaptive step size
%
% dy/dt = func(t,y)
%
% USAGE: [t,y] = rk4ada(func,a,b,y0,h1,eps)
%
% input func = name of external function to evaluate the RHS
% of the ODE
% a, b = limits of integration
% y0 = initial condition
% h1 = initial stepsize
% errmax = desired absolute accuracy
%
% output [t, y] = solution vectors
t = [a];
y = [y0];
i = 1;
while t(i) < b
% first solution using h = h1
h = h1;
k1 = feval(func, t(i) , y(i) );
k2 = feval(func, t(i)+h/2, y(i)+k1*h/2);
k3 = feval(func, t(i)+h/2, y(i)+k2*h/2);
k4 = feval(func, t(i)+h , y(i)+k3*h );
y1 = y(i) + (k1 + 2*k2 + 2*k3 + k4)*h/6;
% second solution using h = h1 / 2
h = h1 / 2;
k1 = feval(func, t(i) , y(i) );
k2 = feval(func, t(i)+h/2, y(i)+k1*h/2);
k3 = feval(func, t(i)+h/2, y(i)+k2*h/2);
k4 = feval(func, t(i)+h , y(i)+k3*h );
tm = t(i) + h;
ym = y(i) + (k1 + 2*k2 + 2*k3 + k4)*h/6;
k1 = feval(func, tm , ym );
k2 = feval(func, tm+h/2, ym+k1*h/2);
k3 = feval(func, tm+h/2, ym+k2*h/2);
k4 = feval(func, tm+h , ym+k3*h );
y2 = ym + (k1 + 2*k2 + 2*k3 + k4)*h/6;
% estimate error, modify solution, change next step size
delta = y2 - y1;
i = i+1;
t(i) = t(i-1) + h1;
y(i) = y2 + delta/15;
if abs(delta) < errmax % error below tolerance
alpha = .2;
else % error too big
alpha = .25;
end
h1 = h1*(errmax/abs(delta))^alpha
end
</PRE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -