曲线下的Matlab局部区域

Matlab partial area under the curve

我想绘制 x 轴上特定值上方和下方的区域。

我面临的问题是离散值。例如下面的代码有一个明确的 X=10 所以我写它的方式是我可以找到索引并计算高于和低于该特定值的值但是如果我想找到曲线上方和下方的区域4 该程序现在可以运行了。

尽管在绘图中 matlab 进行了样条拟合(或某种用于连接离散值的拟合),但 y 的值对应于 x=4,matlab 计算我似乎无法存储或访问它。

%Example for Area under the curve and partial area under the curve using Trapezoidal rule of integration
clc;
close all;
clear all;
x=[0,5,10,15,20];% domain
y=[0,25,50,25,0];% Values

LP=log2(y);
plot(x,y);

full = trapz(x,y);% plot of the total area

I=find(x==10);% in our case will be the distance value up to which we want 
half = trapz(x(1:I),y(1:I));%Plot of the partial area

我们如何找到值的曲线下面积,即 x = 2 或 3 或 4 或 6 或 7 或 ...

我认为您要么需要更多样本,要么需要对数据进行插值。另一种选择是使用函数句柄。然后你需要知道这个功能。下面是使用线性插值的示例。

x0 = [0;5;10;15;20];
y0 = [0,25,50,25,0];
x1 = 0:20;
y1 = interp1(x0,y0,x1,'linear');
xMax = 4;
partInt = trapz(x1(x1<=xMax),y1(x1<=xMax));

其他类型的插值可能合适,但如果没有更多信息就很难说。此外,这从开始到 x 进行插值。但是,我想从这里弄清楚如何更改限制应该很容易。此解决方案与前者不同,因为它较少依赖于数据的金字塔形状。这么说,还是比较笼统的。

这是对 patrik 评论的阐述,"first interpolate and then integrate"。

为了这个答案的目的,我假设所讨论的区域是图中可以看到的区域,并且由于 plot 通过直线连接点,我假设线性插值就足够了.而且,由于梯形法则本身是基于线性插值的,所以我们只需要区间开始和结束的插值即可。

从给定的分数开始

x = [0, 5, 10, 15, 20];
y = [0, 25, 50, 25, 0];

和积分区间限制,比如说

xa = 4;
xb = 20;

我们首先select限制范围内的数据点

ind = (x > xa) & (x < xb);
xw = x(ind);
yw = y(ind);

然后通过边缘的插值补全:

ya = interp1(x, y, xa);
yb = interp1(x, y, xb);
xw = [xa, xw, xb];
yw = [ya, yw, yb];

现在我们可以简单地应用梯形积分:

area = trapz(xw, yw);