Creating a Function in matlab, that finds the mathmatical average of any function.

3 views (last 30 days)
hello i'm new to matlab, and need to write a program that finds the average value of a function. I need to use linspace(a,b,n) to accomplish this. Any help would be much appreciated.

Answers (1)

Walter Roberson
Walter Roberson on 3 Feb 2016
Finding the mathematical average of any arbitrary function cannot be done by sampling the function over a finite number of points (unless the function is only defined at those points perhaps.)
For example, let the function be 1/x and sample it at the integers 1 to n. Then the mean would be sum(1./(1:n))/n and if you plot that for increasing n you see that it continually decreases. So to get its "true" value you would need an infinite number of points, because for any finite stopping the approximation would not be good enough. Therefore there is at least one function whose arithmetic mean cannot be determined by sampling at a finite number of points. By the way, sum(1./(1:n)) is infinity when n goes to infinity, but the sum is finite for any finite n.
  2 Comments
devon anable
devon anable on 3 Feb 2016
Edited: Walter Roberson on 3 Feb 2016
thanks for the quick response, this was my initial reaction as well, however i may have misinterpreted it. here is the actual question given to us by my professor.
we need to creat this function
"function favg = myFuncAvg(f, a, b, n),
This function determines the average value of a function
F over a range from a to b. Use Linspace to generate n equally spaced x values across the range from a to b."

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!