integration in matlab plot
Show older comments
I have a graph of 2 arrays, and I need to do an integration between 0 and 4.7. Can you please tell me how to do it?
x=[0 1.4660 2.9319 4.3979 5.8639 7.3298 8.7958 10.2618 11.7277 13.1937 14.6597]
y=[78.8093 78.8093 77.7206 76.6319 75.3877 73.2103 69.4776 64.2933 57.0353 44.3338 26.4480]
Accepted Answer
More Answers (1)
John D'Errico
on 2 Mar 2024
Edited: John D'Errico
on 2 Mar 2024
You CANNOT use trapz. Despite the fact that another poster has done so, that does not yield a result up to 4.7. If you have no data point that lies EXACTLY at x==4.7, trapz CANNOT be used.
x=[0 1.4660 2.9319 4.3979 5.8639 7.3298 8.7958 10.2618 11.7277 13.1937 14.6597];
y=[78.8093 78.8093 77.7206 76.6319 75.3877 73.2103 69.4776 64.2933 57.0353 44.3338 26.4480];
plot(x,y,'o-')
The solution is not that difficult though. I think fnint is part of the curve fitting toolbox, so you can do this:
fn = pchip(x,y);
Note that pchip is a better choice than spline here, since your curve seems to have a sharp transition in it in some cases.
fnI = fnint(fn)
fnval(fnI,4.7) - fnval(fnI,0)
And that will be the integral from 0 to 4.7.
Another approach, if you lack the fnint function (as I said, part of the CFTB) is to just use integral.
integral(@(x) ppval(fn,x),0,4.7)
As you can see, either yields the same result. However, trapz cannot do that.
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
