Is using the Trapz function equivalent to using the Mean function in MATLAB ?
5 次查看(过去 30 天)
显示 更早的评论
For discrete data, mathematically speaking, calculating the integral is the same as taking the Average (mean) of the data points.
So I am wondering, Can the Mean function be used rather than the Trapz function in MATLAB to obtain the value of the integration provided that discrete data is used ? And would it be correct to do so ?
2 个评论
Jiri Hajek
2023-1-10
编辑:Jiri Hajek
2023-1-11
Hi, you started with an answer, so you seem to understand the principles... But perhaps I'd suggest you to look deeper at numerical integration schemes - the difference between them is accuracy. Note that some data may be integrated perfectly even using a simple scheme...
For example, in the context of comptational fluid dynamics, a piecewise-constant approximation is in fact a well-founded and much used scheme for convection term, known as first order upwind. In contrast to that, trapezoidal scheme uses the assumption of a piecewise-linear approximation of the numerically integrated function.
回答(1 个)
John D'Errico
2023-1-10
编辑:John D'Errico
2023-1-10
NO. It is not the same thing. Can it, under some circumstances be approximately the same? Well, with multiple caveats, yes. Sometimes you would get lucky.
But, NO. A mean is not the same thing as a trapezoidal integration. Effectively, I would say that until you understand integration and numerical integration well enough to know how the two are different, to never use mean instead of trapz.
You wold be slightly better off had you asked if a SUM could be used in lieu of a trapezoidal integration. This is because a sum can be interpreted as a variation of a rectangle rule, but even then only under the correct circumstances. SIgh, this will quickly devolve into a calc 101 course, and I will not go that far.
But consider the formula for a mean, versus a sum. Given a vector Y of length N, what is the sum?
sum(Y) = Y(1) + Y(2) + ... + Y(N)
Now, what is the mean?
mean(Y) = (Y(1) + Y(2) + ... + Y(N))/N
So the mean is just the sum(X), but then divided by the number of elements in the vector. Correct?
Next, compare what sum does, to a trapezoidal rule. For example...
X = 0:3:30;
Y = X.^2;
plot(X,Y,'b-o')
hold on
bar(X,Y)
hold off
I've drawn a bar chart. Now (if you ignore the space between the bars, sorry, I was being lazy) the area of the curve under the function y(x) would be
syms x
y = x^2;
int(y,[0,30])
So 9000 is the indicated area under that curve. How well does trapz do?
trapz(X,Y)
So not too bad. It is only an approximation of course. How about the mean?
mean(Y)
The problem is the area under the curve is approximated by the area of those bars. And each bar has width of 3 units.
sum(Y)*3
So the sum gives us an approximation of the area under the curve, thus a simple variation of the rectangle rule. Remember, you NEED to scale the result by the width of the boxes. And of course, sometimes the boxes have width 1, so sometimes you get lucky. If the box width was just 1, then voila, sum and trapz are at least approximately, sort, of vaguely close. (Did I use enough caveats there?)
Now, can you use a trapezoidal integration to compute a mean? Well, yes. For example, given the function sin(x), over the interval [0,pi]. What is the AVERAGE (MEAN) value of that function over that interval? We can compute the analytical result as simply
syms x
int(sin(x),[0,pi])/pi
And we might then do this:
fplot(@sin,[0,pi],'b-')
hold on
plot([0,pi],[2/pi,2/pi],'r-')
hold off
So the red line represents the average value of the function sin(x) on that interval. And, we might even have computed the average using mean, like this:
u = linspace(0,pi,100);
format long
mean(sin(u))
The exact value is:
2/pi
As you can see, mean here is indeed a good approximation to the desired average value of the function. But is it the same thing as trapz? For that, we would need to do this:
trapz(u,sin(u))/pi
You need to divide by pi there, which is the width of the entire interval.
As I said, don't use mean when you need to use trapz instead. Mean is generally not the correct operation. And really, you need to understand enough about integral calculus to know when you COULD use one instead of the other. Sorry. As I said, I won't teach a calc course here. I think I way overdid it already.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

