Given a postive real number n, we are asked to evaluate the following integral:
We may rewrite the above function in Matlab as:
>> S = @(n) round(integral(@(x) sqrt(floor(x))-floor(sqrt(x)),0,n));
Therefore, for
, we have:
>> s = S(10*pi)
s =
12
Be careful though, in using the Matlab integral function, as it is only an approximation. For example if
:
>> s = S(100000)
Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is
8.0e+01. The integral may not exist, or it may be difficult to approximate numerically to the
requested accuracy.
> In integralCalc/iterateScalarValued (line 372)
In integralCalc/vadapt (line 132)
In integralCalc (line 75)
In integral (line 87)
In @(n)round(integral(@(x)sqrt(floor(x))-floor(sqrt(x)),0,n))
s =
49841
The correct answer is
. The integral function is off by only 2 units here, but the discrepancy could be even greater for other values of n. integral function can also be quite slow. The challenge is to find an efficient and more accurate algorithm to evaluate the integral.
Solution Stats
Problem Comments
1 Comment
Solution Comments
Show comments
Loading...
Problem Recent Solvers1
Suggested Problems
-
Make the vector [1 2 3 4 5 6 7 8 9 10]
52825 Solvers
-
Remove all the words that end with "ain"
2643 Solvers
-
19228 Solvers
-
Project Euler: Problem 7, Nth prime
1751 Solvers
-
Return fibonacci sequence do not use loop and condition
854 Solvers
More from this Author116
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Hi David,
You right I should have use floor (34) instead of round (35). Test suites were adjusted. Thanks.