【问题标题】:MATLAB : Simpson's 1/3 RuleMATLAB:辛普森的 1/3 法则
【发布时间】:2018-11-01 22:38:59
【问题描述】:

我已经为辛普森规则创建了一个代码,但我认为我的函数有误。我没有其他来源可以参考(或者它们太难理解了)。这是我的代码:

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

谁能帮忙看看哪里错了?

【问题讨论】:

  • 您好,欢迎来到 StackOverflow。您是否有任何特定的测试用例不起作用?您能否编辑您的问题并为f_strabh 添加特定值以及您期望的结果?

标签: matlab simpsons-rule


【解决方案1】:

假设你函数中的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

来源:
https://en.wikipedia.org/wiki/Simpson%27s_rule

【讨论】:

    猜你喜欢
    • 2018-08-18
    • 2012-10-19
    • 2016-01-12
    • 2014-01-06
    • 2021-04-19
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2016-05-05
    相关资源
    最近更新 更多