trapz函数给出了奇怪的结果
问题描述:
我在两组数据上使用了trapz函数,并且出现了错误。trapz函数给出了奇怪的结果
下面是数据的视图: pressure drag coefficient and lift coefficient http://s17.postimage.org/inxk1xolr/cptheta.jpg
所以你能理解我的问题; y轴为压力系数Cp,x轴为角度(共49个值,0:7.5:360),绿色方阵的数据为实验得到的数据,红色曲线基本上为Cp sin ),而粉红色的是Cp cos(角度)
对红色的trapz使用完美,它给出7.5,如果我将trapz的输入参数反转,我会得到这个数字的负值!问题在于粉红色图表,使用trapz给出了一个巨大的数字(这是错误的,我不应该得到这个),当切换输入参数时,我得到另一个巨大的数字,而不是第一个数字的负数,这很奇怪,我不知道什么是错的,所以我用四(需要一个函数)来测试trapz
for i = 1:48
y = @(x) (Cp(i+1) - Cp(i))/(theta_rad(i+1) - theta_rad(i)) * x .* sin(x);
clll(i) = -0.5* quad(y,theta_rad(i), theta_rad(i+1));
end
clll = sum(clll);
for j = 1:48
f = @(t) (Cp(j+1) - Cp(j))/(theta_rad(j+1) - theta_rad(j)) * t .* cos(t);
cdddp(i) = 0.5* quad(f,theta_rad(j), theta_rad(j+1));
end
cdddp = sum(cdddp);
的结果对于第一循环(红色曲线的四)我有非常密切的回答,错误可能是由于我在点之间使用了线性插值,而对于第二个循环,我得到了一个合理的答案,这是一个非常小的数字,它在我正在寻找的答案的范围内。我也在Excel中尝试了梯形法则,并且我再次得到了这个庞大的数字,所以这是我做错了事情。
编辑:
我刚刚发现了一个错误,我用的是使用角度以度而非弧度的梯形!现在我得到的数字要小得多,但我认为还有一个错误,因为使用quad的整数给出了一个更明智的答案。
下面是我使用trapz
Cdp = 0.5*trapz(theta*pi/180, Cp.*cosd(theta)); %pressure drag coefficient
Cl = -0.5*trapz(theta*pi/180, Cp.*sind(theta)); %lift coefficient
答
我现在使用的代码,在trapz功能我得到了正确的答案使用弧度后。我试图做的插值是废话!我设法使用曲线拟合工具来创建一个合适的,这是非常有效的
这
Cdp = 0.5*trapz(theta*pi/180, Cp.*cosd(theta)); %pressure drag coefficient
可以通过使用工具来实现:
fit1 = createFit(theta*pi/180, Cp.*cosd(theta));
##createFit is generated by MatLab and I only need parts of it, all the plots commands were removed.
Cdp = 0.5*integrate(fit1,0,2*pi) ##"integrate" < this is why the tool is great, No need to export the coefficients to the workspace, create a function handle then use quad