Trapz给出奇怪的结果 [英] Trapz giving weird results

查看:267
本文介绍了Trapz给出奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Matlab的trapz函数有一个奇怪的结果.我有两个变量zptest和omega,它们都是3000x2x1正数组.

I'm having a weird result with Matlab's trapz function. I have two variables, zptest and omega, both of which are positive, 3000x2x1 arrays.

当我绘制zptest与omega(plot(zptest(:,1,1),omega(:,1,1))的曲线时,曲线显然是正的,积分时应该给出正的结果.事实并非如此,但是,如下所示:

When I plot zptest vs omega (plot(zptest(:,1,1),omega(:,1,1)) the curve is clearly positive and should give a positive result when integrating. This is not the case, however, as shown below:

trapz(zptest(:,1,1),omega(:,1,1))

trapz(zptest(:,1,1),omega(:,1,1))

ans =

-0.049999940237341

-0.049999940237341

只是为了证明omega和zptest均为阳性:

just to prove that both omega and zptest are positive:

发现(omega(:,1,1)< 0)

find(omega(:,1,1) < 0)

ans =

空矩阵:0乘1

find(zptest(:,1,1)< 0)

find(zptest(:,1,1) < 0)

ans =

空矩阵:0乘1

我知道我没有为自己的实际工作提供任何上下文,但这似乎是一个与上下文无关的问题.有人知道发生了什么吗?

I know I'm not giving any context to what I'm actually doing but this seems like a context independent problem. Does anyone have any idea what's going on?

推荐答案

尝试按升序(和相应的y值)重新排序x:

Try re-ordering x in ascending order (and y-values accordingly):

x_order = x(end:-1:1); %fliplr
y_order = y(end:-1:1); %fliplr
trapz(x_order, y_order)

trapz(x,y)中,通过diff(x,1,1)来应用x的微分,即[x(2:n,:) - x(1:n-1,:)].如果您的x下降,则将得到负dx.是正数还是负数都没有关系.但是,在plot中,曲线将显示为正定的(实际上,您看不到点的顺序,只是平面上两个矢量对的点).

In trapz(x,y) differentiation of x is applied through diff(x,1,1), i.e. [x(2:n,:) - x(1:n-1,:)]. If your x is descending this will give negative dx. It doesn't matter if it is positive or negative. However, in plot the curve will appear positive-definite (you don't actually see the order of points, just pairs from two vectors on a plane).

示例(比较以下内容):

x = [-1 -0.5 0]; y = 0.5-x; 
figure; plot(x,y); hold on; plot(-x, y,'r')
trapz(x, y)
trapz(-x, y)
figure; plot(x, y); hold on; plot(fliplr(-x), fliplr(y),'r')
trapz(fliplr(-x), fliplr(y))

这篇关于Trapz给出奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆