matlab中quad.m函数,我把它简化了,function [Q] = quad2(funfcn,a,b)f = fcnchk(funfcn);h = 0.13579*(b-a);x = [a a+h a+2*h (a+b)/2 b-2*h b-h b];y = f(x);[Q(1)] = ...quadstep(f,x(1),x(3),y(1),y(2),y(3));[Q(2)] = ...quadstep(f,x(3),x(5),y(3),y(4),y
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 20:39:27
matlab中quad.m函数,我把它简化了,function [Q] = quad2(funfcn,a,b)f = fcnchk(funfcn);h = 0.13579*(b-a);x = [a a+h a+2*h (a+b)/2 b-2*h b-h b];y = f(x);[Q(1)] = ...quadstep(f,x(1),x(3),y(1),y(2),y(3));[Q(2)] = ...quadstep(f,x(3),x(5),y(3),y(4),y
matlab中quad.m函数,我把它简化了,
function [Q] = quad2(funfcn,a,b)
f = fcnchk(funfcn);
h = 0.13579*(b-a);
x = [a a+h a+2*h (a+b)/2 b-2*h b-h b];
y = f(x);
[Q(1)] = ...
quadstep(f,x(1),x(3),y(1),y(2),y(3));
[Q(2)] = ...
quadstep(f,x(3),x(5),y(3),y(4),y(5));
[Q(3)] = ...
quadstep(f,x(5),x(7),y(5),y(6),y(7));
Q = sum(Q);
function [Q] = quadstep(f,a,b,fa,fc,fb)
tol = 1.e-6;
h = b - a;
c = (a + b)/2;
x = [(a + c)/2,(c + b)/2];
y = f(x);
fd = y(1);
fe = y(2);
% Three point Simpson's rule.
Q1 = (h/6)*(fa + 4*fc + fb);
% Five point double Simpson's rule.
Q2 = (h/12)*(fa + 4*fd + 2*fc + 4*fe + fb);
% One step of Romberg extrapolation.
Q = Q2 + (Q2 - Q1)/15;
if abs(Q2 - Q)
matlab中quad.m函数,我把它简化了,function [Q] = quad2(funfcn,a,b)f = fcnchk(funfcn);h = 0.13579*(b-a);x = [a a+h a+2*h (a+b)/2 b-2*h b-h b];y = f(x);[Q(1)] = ...quadstep(f,x(1),x(3),y(1),y(2),y(3));[Q(2)] = ...quadstep(f,x(3),x(5),y(3),y(4),y
三个句点(...)表示续行符(Continuation),即从版面上换行,但语法上认为没换行,类似于C语言中行尾的反斜线(\);
那一段不可以删.quadstep是递归函数,那一段是递归的出口,本质上是用两种不同阶次的积分方法进行积分,如果二者误差比较大就对积分区间进一步细分.你如果删掉递归的出口,很自然就会不停的进行递归调用,直到超出允许的递归次数RecursionLimit.
楼上的回答基本不靠谱,完全是治标不治本的方法.