MATLAB:Simpson';s 1/3规则
我已经为辛普森法则创建了一个代码,但我想我把函数搞错了。我没有其他来源可供参考(或者它们太难理解)。这是我的密码:MATLAB:Simpson';s 1/3规则,matlab,simpsons-rule,Matlab,Simpsons Rule,我已经为辛普森法则创建了一个代码,但我想我把函数搞错了。我没有其他来源可供参考(或者它们太难理解)。这是我的密码: function s = simpson(f_str, a, b, h) f = inline(f_str); n = (b-a)/h; x = a + [1:n-1]*h; xi = a + [1:n]*h; s = h/3 * (f(a) + f(b) + 2*sum(f(x)) + 4*sum(f(xi))); end 有人能帮忙看看哪里是错误的部分吗?假设
function s = simpson(f_str, a, b, h)
f = inline(f_str);
n = (b-a)/h;
x = a + [1:n-1]*h;
xi = a + [1:n]*h;
s = h/3 * (f(a) + f(b) + 2*sum(f(x)) + 4*sum(f(xi)));
end
有人能帮忙看看哪里是错误的部分吗?假设函数中的
h
是步长:
function s = simpson(f_str, a, b, h)
% The sample vector will be
xi = a:h:b;
f = inline(f_str);
% the function at the endpoints
fa = f(xi(1));
fb = f(xi(end));
% the even terms.. i.e. f(x2), f(x4), ...
feven = f(xi(3:2:end-2));
% similarly the odd terms.. i.e. f(x1), f(x3), ...
fodd = f(xi(2:2:end));
% Bringing everything together
s = h / 3 * (fa + 2 * sum(feven) + 4 * sum(fodd) + fb);
end
来源:大家好,欢迎来到StackOverflow。您是否有任何不起作用的特定测试用例?您能否编辑您的问题并为
f_str
、a
、b
、h
添加特定值,以及您预期的结果?