I want Absolute numerical integral-sth like absolute trapz
9 views (last 30 days)
Show older comments
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
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.
Answers (2)
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.
0 Comments
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!