【发布时间】:2018-08-15 12:45:50
【问题描述】:
我有一个关于查找矩形面积S2(曲线上方)的问题。我想找到S1/S2 之类的(S - S2)/(S2),其中S = S1 + S2。
我有 2 个vectors double (x;y),我可以找到 S1 + S2:
S = (x.back() - x[0])*(y.back() - y[0]))
那我想用数值积分求曲线S2下的整个区域,然后从S2中减去z:
z = (x.back() - x[0])*(y[0] - 0),S2 = S2 - z
我的问题是:如果我没有函数但有 (x;y),如何使用数值积分。例如,在 matlab 中,feval 看起来像这样:
% Total area under the curve
ft = fittype('smoothingspline');
cf = fit(x,y,ft);
F = @(x) feval(cf,x);
S2 = quad(F,x(1),x(end));
在 C++ 中我有:
#include "Functions.h"
std::vector<double>AreaRatio(std::vector<double>&x, std::vector<double>&y) {
double S(0.0), z(0.0), S2(0.), R(0.0);
S = (x.back() - x[0])*(y.back() - y[0]);
z = (x.back()*x[0])*(y[0]-0);
S2 = /.../
// Numerical methods (any library) to find the area under the curve,
// but I don't know how to transfer function into function of Numerical integration,
// because I have only coordinates.
R = (S - S2) / S2;
return R;
}
【问题讨论】:
-
矩形的面积是它的高度乘以它的宽度。
-
您的代码在 MATLAB 中,但标题询问的是 C++。还请提供 C++ 代码并解释您目前使用两种语言的原因,因为这非常令人困惑
-
您的向量包含点的坐标(定义小矩形),因此将这些矩形的面积相加得到面积。
-
c++ 本身并不了解数值积分。您要么必须编写自己的集成器,要么选择一个库。无论如何,您的问题要么过于宽泛,要么应该改写并移至math.stackexchange.com
-
辛普森法则是一种精确的积分技术,用于样条曲线(包括三次)。
标签: c++ matlab curve numerical-integration