I want Absolute numerical integral-sth like absolute trapz

조회 수: 8 (최근 30일)
Hajik zagori
Hajik zagori 2011년 8월 10일
Hi,
I am trying to calculate integral of this function with trapz .
x=-1.5*pi:pi/100:pi;
y=sin(x);
z=trapz(x,y);
It gives me this : z=0.999;
Now I want the whole surface confined by the function to be calculated. so the number must be z=3.
whole surface=[positive surfaces+abs(negative surfaces)].
Is there any function for numerical Integration for this process.
I have to add that I can not use explicit integral function because the only thing I have is just some X & Y .In other words assume that we don't know y's function.
Thanks in advance.
  댓글 수: 2
Daniel Shub
Daniel Shub 2011년 8월 10일
I don't understand the question. How is what you want not trapz(x, abs(y))? I don't understand how what you want can lead to z=3.
Hajik zagori
Hajik zagori 2011년 8월 10일
Look , I have some X values and for each X value I have a Y value.I don't know what 's the function between X & Y.
The X value is expanded from negative to positive and the Y value is only positive.
Now I graph the (X,Y) points and connect the points together . I want to know the surface below this line.How can I achieve this?
because I only have points not a function I have to use trapz command.but the trapz , add postitive suface with negative surface so it doesn't give you the whole underneath surface.

댓글을 달려면 로그인하십시오.

답변 (2개)

Friedrich
Friedrich 2011년 8월 10일
Hi,
as long as Y is positive trapez(X,Y) will work. You don't have to worry about negative X values, e.g.
>> X = -0.5:0.1:0;
>> Y = ones(size(X));
>> trapz(X,Y)
(This would be a simple recatangle). You only have to take care of negative Y values, which you don't have.

Fangjun Jiang
Fangjun Jiang 2011년 8월 10일
Your y HAS negative values. plot(x,y) to take a look.
This is a simple task. There is a theoretical solution of the integration of sin(x). Do that calculation and compare that with your numerical result.
Depending on your need, you may need to do z=trapz(x,y) or z=trapz(x,abs(y))
Although your y is generated using sin(x), when you use trapz(x,y), MATLAB doesn't know the functional relationship between x and y. So don't worry about that. Calm down, do your paper work and you will confirm that trapz() is doing what it suppose to do. Then you can move on with your real task.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by