Clear Filters
Clear Filters

I want Absolute numerical integral-sth like absolute trapz

11 views (last 30 days)
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 Comments
Daniel Shub
Daniel Shub on 10 Aug 2011
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 on 10 Aug 2011
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.

Sign in to comment.

Answers (2)

Friedrich
Friedrich on 10 Aug 2011
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 on 10 Aug 2011
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.

Community Treasure Hunt

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

Start Hunting!