有哪位大哥大姐能够给我说一下:经典龙格库塔的C++代码?

作者&投稿:江翟 (若有异议请与网页底部的电邮联系)
C++程序 龙格库塔数值解法~

http://ncforest.blog.163.com/blog/static/295626642007712104152987/
这个blog页面上有写源代码,比较详细.
懒得复制了.自己看去吧.
希望对你有用.

看得我都晕了

这个是从Matlab中查到的龙格库塔代码,你看对你有用没
function varargout = ode45(ode,tspan,y0,options,varargin)
solver_name = 'ode45';

% Check inputs
if nargin < 4
options = [];
if nargin < 3
y0 = [];
if nargin < 2
tspan = [];
if nargin < 1
error('MATLAB:ode45:NotEnoughInputs',...
'Not enough input arguments. See ODE45.');
end
end
end
end

% Stats
nsteps = 0;
nfailed = 0;
nfevals = 0;

% Output
FcnHandlesUsed = isa(ode,'function_handle');
output_sol = (FcnHandlesUsed && (nargout==1)); % sol = odeXX(...)
output_ty = (~output_sol && (nargout > 0)); % [t,y,...] = odeXX(...)
% There might be no output requested...

sol = []; f3d = [];
if output_sol
sol.solver = solver_name;
sol.extdata.odefun = ode;
sol.extdata.options = options;
sol.extdata.varargin = varargin;
end

% Handle solver arguments
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
options, threshold, rtol, normcontrol, normy, hmax, htry, htspan, dataType] = ...
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
nfevals = nfevals + 1;

% Handle the output
if nargout > 0
outputFcn = odeget(options,'OutputFcn',[],'fast');
else
outputFcn = odeget(options,'OutputFcn',@odeplot,'fast');
end
outputArgs = {};
if isempty(outputFcn)
haveOutputFcn = false;
else
haveOutputFcn = true;
outputs = odeget(options,'OutputSel',1:neq,'fast');
if isa(outputFcn,'function_handle')
% With MATLAB 6 syntax pass additional input arguments to outputFcn.
outputArgs = varargin;
end
end
refine = max(1,odeget(options,'Refine',4,'fast'));
if ntspan > 2
outputAt = 'RequestedPoints'; % output only at tspan points
elseif refine <= 1
outputAt = 'SolverSteps'; % computed points, no refinement
else
outputAt = 'RefinedSteps'; % computed points, with refinement
S = (1:refine-1) / refine;
end
printstats = strcmp(odeget(options,'Stats','off','fast'),'on');

% Handle the event function
[haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout] = ...
odeevents(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);

% Handle the mass matrix
[Mtype, Mfun, Margs, M] = odemass(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);
if Mtype>0 % non-trivial mass matrix
Msingular = odeget(options,'MassSingular','no','fast');
if strcmp(Msingular,'maybe')
warning('MATLAB:ode45:MassSingularAssumedNo',['ODE45 assumes ' ...
'MassSingular is ''no''. See ODE15S or ODE23T.']);
elseif strcmp(Msingular,'yes')
error('MATLAB:ode45:MassSingularYes',...
['MassSingular cannot be ''yes'' for this solver. See ODE15S '...
' or ODE23T.']);
end
if Mtype == 1
[L,U] = lu(M);
else
L = [];
U = [];
end

% Incorporate the mass matrix into odeFcn and odeArgs.
[odeFcn,odeArgs] = odemassexplicit(FcnHandlesUsed,Mtype,odeFcn,odeArgs,Mfun,Margs,L,U);
f0 = feval(odeFcn,t0,y0,odeArgs{:});
nfevals = nfevals + 1;
end

% Non-negative solution components
idxNonNegative = odeget(options,'NonNegative',[],'fast');
nonNegative = ~isempty(idxNonNegative);
if nonNegative % modify the derivative function
[odeFcn,thresholdNonNegative] = odenonnegative(odeFcn,y0,threshold,idxNonNegative);
f0 = feval(odeFcn,t0,y0,odeArgs{:});
nfevals = nfevals + 1;
end

t = t0;
y = y0;

% Allocate memory if we're generating output.
nout = 0;
tout = []; yout = [];
if nargout > 0
if output_sol
chunk = min(max(100,50*refine), refine+floor((2^11)/neq));
tout = zeros(1,chunk,dataType);
yout = zeros(neq,chunk,dataType);
f3d = zeros(neq,7,chunk,dataType);
else
if ntspan > 2 % output only at tspan points
tout = zeros(1,ntspan,dataType);
yout = zeros(neq,ntspan,dataType);
else % alloc in chunks
chunk = min(max(100,50*refine), refine+floor((2^13)/neq));
tout = zeros(1,chunk,dataType);
yout = zeros(neq,chunk,dataType);
end
end
nout = 1;
tout(nout) = t;
yout(:,nout) = y;
end

% Initialize method parameters.
pow = 1/5;
A = [1/5, 3/10, 4/5, 8/9, 1, 1];
B = [
1/5 3/40 44/45 19372/6561 9017/3168 35/384
0 9/40 -56/15 -25360/2187 -355/33 0
0 0 32/9 64448/6561 46732/5247 500/1113
0 0 0 -212/729 49/176 125/192
0 0 0 0 -5103/18656 -2187/6784
0 0 0 0 0 11/84
0 0 0 0 0 0
];
E = [71/57600; 0; -71/16695; 71/1920; -17253/339200; 22/525; -1/40];
f = zeros(neq,7,dataType);
hmin = 16*eps(t);
if isempty(htry)
% Compute an initial step size h using y'(t).
absh = min(hmax, htspan);
if normcontrol
rh = (norm(f0) / max(normy,threshold)) / (0.8 * rtol^pow);
else
rh = norm(f0 ./ max(abs(y),threshold),inf) / (0.8 * rtol^pow);
end
if absh * rh > 1
absh = 1 / rh;
end
absh = max(absh, hmin);
else
absh = min(hmax, max(hmin, htry));
end
f(:,1) = f0;

% Initialize the output function.
if haveOutputFcn
feval(outputFcn,[t tfinal],y(outputs),'init',outputArgs{:});
end

% THE MAIN LOOP

done = false;
while ~done

% By default, hmin is a small number such that t+hmin is only slightly
% different than t. It might be 0 if t is 0.
hmin = 16*eps(t);
absh = min(hmax, max(hmin, absh)); % couldn't limit absh until new hmin
h = tdir * absh;

% Stretch the step if within 10% of tfinal-t.
if 1.1*absh >= abs(tfinal - t)
h = tfinal - t;
absh = abs(h);
done = true;
end

% LOOP FOR ADVANCING ONE STEP.
nofailed = true; % no failed attempts
while true
hA = h * A;
hB = h * B;
f(:,2) = feval(odeFcn,t+hA(1),y+f*hB(:,1),odeArgs{:});
f(:,3) = feval(odeFcn,t+hA(2),y+f*hB(:,2),odeArgs{:});
f(:,4) = feval(odeFcn,t+hA(3),y+f*hB(:,3),odeArgs{:});
f(:,5) = feval(odeFcn,t+hA(4),y+f*hB(:,4),odeArgs{:});
f(:,6) = feval(odeFcn,t+hA(5),y+f*hB(:,5),odeArgs{:});

tnew = t + hA(6);
if done
tnew = tfinal; % Hit end point exactly.
end
h = tnew - t; % Purify h.

ynew = y + f*hB(:,6);
f(:,7) = feval(odeFcn,tnew,ynew,odeArgs{:});
nfevals = nfevals + 6;

% Estimate the error.
NNrejectStep = false;
if normcontrol
normynew = norm(ynew);
errwt = max(max(normy,normynew),threshold);
err = absh * (norm(f * E) / errwt);
if nonNegative && (err <= rtol) && any(ynew(idxNonNegative)<0)
errNN = norm( max(0,-ynew(idxNonNegative)) ) / errwt ;
if errNN > rtol
err = errNN;
NNrejectStep = true;
end
end
else
err = absh * norm((f * E) ./ max(max(abs(y),abs(ynew)),threshold),inf);
if nonNegative && (err <= rtol) && any(ynew(idxNonNegative)<0)
errNN = norm( max(0,-ynew(idxNonNegative)) ./ thresholdNonNegative, inf);
if errNN > rtol
err = errNN;
NNrejectStep = true;
end
end
end

% Accept the solution only if the weighted error is no more than the
% tolerance rtol. Estimate an h that will yield an error of rtol on
% the next step or the next try at taking this step, as the case may be,
% and use 0.8 of this value to avoid failures.
if err > rtol % Failed step
nfailed = nfailed + 1;
if absh <= hmin
warning('MATLAB:ode45:IntegrationTolNotMet',['Failure at t=%e. ' ...
'Unable to meet integration tolerances without reducing ' ...
'the step size below the smallest value allowed (%e) ' ...
'at time t.'],t,hmin);

solver_output = odefinalize(solver_name, sol,...
outputFcn, outputArgs,...
printstats, [nsteps, nfailed, nfevals],...
nout, tout, yout,...
haveEventFcn, teout, yeout, ieout,...
{f3d,idxNonNegative});
if nargout > 0
varargout = solver_output;
end
return;
end

if nofailed
nofailed = false;
if NNrejectStep
absh = max(hmin, 0.5*absh);
else
absh = max(hmin, absh * max(0.1, 0.8*(rtol/err)^pow));
end
else
absh = max(hmin, 0.5 * absh);
end
h = tdir * absh;
done = false;

else % Successful step

NNreset_f7 = false;
if nonNegative && any(ynew(idxNonNegative)<0)
ynew(idxNonNegative) = max(ynew(idxNonNegative),0);
if normcontrol
normynew = norm(ynew);
end
NNreset_f7 = true;
end

break;

end
end
nsteps = nsteps + 1;

if haveEventFcn
[te,ye,ie,valt,stop] = ...
odezero(@ntrp45,eventFcn,eventArgs,valt,t,y,tnew,ynew,t0,h,f,idxNonNegative);
if ~isempty(te)
if output_sol || (nargout > 2)
teout = [teout, te];
yeout = [yeout, ye];
ieout = [ieout, ie];
end
if stop % Stop on a terminal event.
% Adjust the interpolation data to [t te(end)].

% Update the derivatives using the interpolating polynomial.
taux = t + (te(end) - t)*A;
[ignore,f(:,2:7)] = ntrp45(taux,t,y,[],[],h,f,idxNonNegative);

tnew = te(end);
ynew = ye(:,end);
h = tnew - t;
done = true;
end
end
end

if output_sol
nout = nout + 1;
if nout > length(tout)
tout = [tout, zeros(1,chunk,dataType)]; % requires chunk >= refine
yout = [yout, zeros(neq,chunk,dataType)];
f3d = cat(3,f3d,zeros(neq,7,chunk,dataType));
end
tout(nout) = tnew;
yout(:,nout) = ynew;
f3d(:,:,nout) = f;
end

if output_ty || haveOutputFcn
switch outputAt
case 'SolverSteps' % computed points, no refinement
nout_new = 1;
tout_new = tnew;
yout_new = ynew;
case 'RefinedSteps' % computed points, with refinement
tref = t + (tnew-t)*S;
nout_new = refine;
tout_new = [tref, tnew];
yout_new = [ntrp45(tref,t,y,[],[],h,f,idxNonNegative), ynew];
case 'RequestedPoints' % output only at tspan points
nout_new = 0;
tout_new = [];
yout_new = [];
while next <= ntspan
if tdir * (tnew - tspan(next)) < 0
if haveEventFcn && stop % output tstop,ystop
nout_new = nout_new + 1;
tout_new = [tout_new, tnew];
yout_new = [yout_new, ynew];
end
break;
end
nout_new = nout_new + 1;
tout_new = [tout_new, tspan(next)];
if tspan(next) == tnew
yout_new = [yout_new, ynew];
else
yout_new = [yout_new, ntrp45(tspan(next),t,y,[],[],h,f,idxNonNegative)];
end
next = next + 1;
end
end

if nout_new > 0
if output_ty
oldnout = nout;
nout = nout + nout_new;
if nout > length(tout)
tout = [tout, zeros(1,chunk,dataType)]; % requires chunk >= refine
yout = [yout, zeros(neq,chunk,dataType)];
end
idx = oldnout+1:nout;
tout(idx) = tout_new;
yout(:,idx) = yout_new;
end
if haveOutputFcn
stop = feval(outputFcn,tout_new,yout_new(outputs,:),'',outputArgs{:});
if stop
done = true;
end
end
end
end

if done
break
end

% If there were no failures compute a new h.
if nofailed
% Note that absh may shrink by 0.8, and that err may be 0.
temp = 1.25*(err/rtol)^pow;
if temp > 0.2
absh = absh / temp;
else
absh = 5.0*absh;
end
end

% Advance the integration one step.
t = tnew;
y = ynew;
if normcontrol
normy = normynew;
end
if NNreset_f7
% Used f7 for unperturbed solution to interpolate.
% Now reset f7 to move along constraint.
f(:,7) = feval(odeFcn,tnew,ynew,odeArgs{:});
nfevals = nfevals + 1;
end
f(:,1) = f(:,7); % Already have f(tnew,ynew)

end

solver_output = odefinalize(solver_name, sol,...
outputFcn, outputArgs,...
printstats, [nsteps, nfailed, nfevals],...
nout, tout, yout,...
haveEventFcn, teout, yeout, ieout,...
{f3d,idxNonNegative});
if nargout > 0
varargout = solver_output;
end

对楼上无语~c++代码……
我记得是照书上流程编的 不过忘了储存~~


本人姓彭,老婆姓吴,我和宝宝是一个生肖的(‘马)!哪位大哥大姐能给...
彭睿君、彭尚言

十万火急~哪位大哥大姐能提供几个关于企业凝聚力的参考文献!!1_百度...
「参考文献」1.徐二明、王智慧:《企业战略管理理论的发展与创新》,《首都经济贸易大学学报》199 9年第1期。2.武亚军:《90年代企业战略管理理论的发展与研究趋势》,《南开管理评论》1999年第2期。3.王毅、陈劲、许庆瑞:《企业核心能力:理论溯源与逻辑结构剖析》,《管理科学学报》2000年第9期。4...

...她听说会很疼,哪位大哥大姐能帮我替她消除这个恐惧啊?
女的第一次性爱,伴随着处女膜的破裂和阴道的阵痛,无论在生理上和心理上都会有些影响。所以对男性来说,这是对您是否是一个好丈夫、一个好男友的考验……如何面对她的第一次(图)VS当他不是你的“第一次”>>解决方案 [实际对策]●确保你的伴侣了解你面对第一次性生活的心情;如果可能事先与他...

哪位好心的大哥大姐能帮我制定一个减肥计划?
一共12天: 头3天:每天以蔬菜和水果作为食物,早上吃水果,中午可以蔬菜水果一起吃,晚上吃蔬菜(不要油和盐哦),份量不限 第4-6天:每天吃牛奶和酸奶(请不要买里面有防腐剂的,不但不能帮助减肥还会增加小肚肚),可以多吃,份量不限 最后的6天,蔬菜水果牛奶和酸奶混和吃,份量不限 台湾减肥妙方-3星期瘦7公斤 方...

哪位大哥大姐能告诉一下大学计算机专业最先学什么编程语言,谢谢了...
哪位大哥大姐能告诉一下大学计算机专业最先学什么编程语言,谢谢了计算机语言的种类非常的多,总的来说可以分成机器语言,汇编语言,高级语言三大类。 电脑每做的一次动作,一个步骤,都是按照以经用计算机语言编好

哪位大叔大妈大姐大哥阿姨伯伯有没用的等级高点的QQ号 给小弟个...
476486347,就这个。(真的,2个太阳呢,还是会员!)密码是912584632

哪位大哥大姐给小弟个入团申请书,小弟感激不尽呢~
敬爱的学校团支部:共青团是先进青年人才辈出的组织,是促使人上进、严格要求自己的组织。是以毛泽东思想、马列主义和邓小平理论为战斗的主导思想,是党的得力助手。我认为自己已达到了入团的标准,恳请团组织批准我成为团员。我加入团的原因是因为:在各位老团员的表现中,我知道了入团可以使我得到更多的...

有哪些手机操作系统啊?哪位大哥大姐可以详细给我讲讲?
智能手机市场主要应用的系统为:Symbian S60(诺基亚)、Linux(摩托罗拉)、Mac OS X(苹果)、Windows mobile(众多品牌)、Symbian UIQ(索爱 )、BlackBerry OS(黑莓)、Palm OS(Palm)和最新的Android(谷歌)。.非智能机基本上是没有操作系统的,不能够升级和刷机。

哪位大哥大姐能告诉我小龙女比黄蓉更适合做女朋友的原因?急用,兄弟我...
我觉得这是毋庸置疑的问题:选黄蓉。理由:黄蓉聪明机智,处事圆滑,活泼可爱,有这样的妻子,人际关系一定不错,而且对自己所喜欢的人可谓生死相依。反之小龙女,冷!好像是个不食人间烟火的小姑娘,不会与人交往。总之我就是喜欢黄蓉这样的女孩子~~~...

哪位大哥大姐能告诉我<<陶侃为官>>的文言文解释啊?小弟急于要知道啊...
为百姓勤于农耕养殖,达到家给人足。当时造船,木屑和竹头他全让人收起管好,人们全不 理解这原因。后来元旦皇帝朝会群臣,长久下雪才晴,大厅前雪化了还很湿,于是用木 屑布在地上。等到桓温攻伐蜀地时,又用陶侃贮存的竹头做竹钉组装船。他总揽事务细微缜 密,都是这样。陶侃性情细致好问,曾...

彭水苗族土家族自治县13012717162: 哪位大哥大姐推荐一些电影来看看,要经典些的,新老都行.. -
惠柯万君: 死亡飞车 , 通缉令. 我是传奇

彭水苗族土家族自治县13012717162: 哪位大哥介绍几本经典,经典,再经典的小说 -
惠柯万君: [奇幻修真] 星辰变 [游戏生涯] 从零开始 [异世大陆] 神墓 [异世大陆] 兽血沸腾 [异世大陆] 佣兵天下 [古典仙侠] 诛仙 [都市生活] 极品公子 [星际战争] 小兵传奇 [架空历史] 极品家丁 [机器时代] 师士传说 [西方奇幻] 亵渎 [架空历史] 回到明朝当王爷 [异世大...

彭水苗族土家族自治县13012717162: 哪位大哥给说几部好看经典的动漫 -
惠柯万君: 我推荐你一些我看过的很赞的动画片!都是我分类整理好的!希望对你有帮助!!!纯爱+搞笑的1 好想告诉你——这个超经典超纯的!!很感人!一定要看!这个强烈推荐2 伯爵与妖精——也...

彭水苗族土家族自治县13012717162: 有什么经典的电影哪位大哥可以告诉小弟 -
惠柯万君: 我来推荐你一些经典影片: 《魔戒1-3》《黑客帝国1-3》《生化危机1-3》《美国派1-5》《谍中谍1-2》《一球成名1-2》《南极大冒险》《勇闯夺命岛》《空中监狱》《星河战队》《太阳之泪》《第五元素》《王者之心》《后天》《天地大冲撞...

彭水苗族土家族自治县13012717162: 哪位大哥大姐给小弟推荐几部经典的电视剧啊
惠柯万君: 十全九美 很好玩 挺逗的

彭水苗族土家族自治县13012717162: 哪位大哥大姐能给我说说经典的科幻电影?越全越好!
惠柯万君: 侏罗纪系列 远古入侵(美剧)系列 X-战警系列 星河战队系列 异形系列 铁血战士系列 变形金刚系列 哈利波特系列 加勒比海盗系列 大白鲨系列 黑客帝国系列 暮光之城系列 还有一些优秀的 比如洛杉矶之战,关键第四号等等,太多了 留个地址 经典科幻片 www.goyyy.com 这里你都可以看到

彭水苗族土家族自治县13012717162: 各位大哥大姐推荐几部经典 全本 网络小说吧
惠柯万君:荒莽岁月推荐您必看的10位大神的作品 第一名 众神之王——李思远 吼吼~~~~~~~~~~~` 主要作品:《追忆荒莽岁月》《血裔传说》《幻界神曲》《阴影之书》《魔爪王、白骨与将星》《天威》 重点推荐:《追忆荒莽岁月》《血裔传说》《天威...

彭水苗族土家族自治县13012717162: 我想问哪个大哥大姐知到经典的语言.要好经典好经典的! -
惠柯万君: 华语电影经典对白 朕给你的,才是你的,朕不给你,你不能抢.——《黄金甲》 黎叔很生气,后果很严重.——《天下无贼》 做人要厚道.——《手机》 有一日,我的心上人会脚踏七色祥云,身披金甲战衣来迎娶我,可惜我猜中了开头,却猜...

彭水苗族土家族自治县13012717162: 那位大哥大姐给小弟介绍点经典小说 都市 网游 修真 都行 谢谢乐
惠柯万君: 王牌进化 我很喜欢 卡徒 也不错

彭水苗族土家族自治县13012717162: 哪位经常读名著的大哥大姐 给我推荐几本名著 先给我简介内容 我给高分
惠柯万君: 简爱

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网