【问题标题】:solving integral in rectangular method in matlab在matlab中用矩形法求解积分
【发布时间】:2018-05-11 19:10:57
【问题描述】:

这是我在matlab中对矩形方法进行积分的代码

f=@(x) (x^(1/2))
a = 1  
b = 10 

% step size
h = 0.25 

n = 0 % the counter

xn= a + (n * h)
%%
%Rectangle Method:
s=0
for i =0:n-1
s = s + f(xn)
end
Rectangle = h * s

答案应该是 20 左右,但我得到 29.5 有什么问题?

【问题讨论】:

  • 你的变量 xn 没有更新,因为它在循环之外
  • x^(1/2)sqrt(x) 相同(在这种情况下)。使用后者,它更不容易出错。

标签: matlab


【解决方案1】:

两个错误:

1) xn 未更新。

2) 点数n 设置为零。

还有其他一些我没有解决的小问题,例如左右边界点都应该以 1/2 的权重对总和做出贡献。

快速修复:

f=@(x) (x^(1/2));
a = 1;  
b = 10 ;

% step size
h = 0.25; 

n = (b-a)/h; % the counter


%%
%Rectangle Method:
s=0;
for i =0:n-1
    xn= a + (i * h);
    s = s + f(xn);
end
Rectangle = h * s;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多