hi I have a problem with Matlab i want Computing error in Simpson program my program is:

please help me
thank you

5 Comments

You do not call integrate() or integrate2() or int() or quad() or quadgk(), so that error message cannot occur in that code.
in numerical analysis , simpson rule error ,, meaning of computational error as input
Please, don't close questions that have answers. If you don't want someone to see what you asked because you were not allowed to ask for help online, then you should not be posting the question in the first place.

Sign in to comment.

 Accepted Answer

To be picky, your very first error was a simple one.
f=input('please insert integral : ','s');
You don't want the integral to be provided, but the integrand, thus the target of your integration. ;-)
So, lets see. does it work? If I start with n=10, a range of [0,pi], and f(x) provided as 'sin(x)', we get an integral as:
s
s =
2.000109517315
Which, since we know the true value to be...
syms u
int(sin(u),[0,pi])
ans =
2
That looks pretty good. Increase n, and it should be pretty accurate. So what might you have done wrong? Hard to know, since you don't tell us what you perceive to be the error.
Edit: So, going back, it looks like you want to know the error of the computation? That means you need to know the exact integral value, thus ground truth. You could use the symbolic toolbox to compute that value, as I did above. Or, since you know the function provided, you could compute it in advance. After all, you are the one who is testing the code, so if you don't know the answer in advance, then you cannot compute the error. Finally, you could use other, more accurate means to compute the integral. So, you might use the function integral as a reference, with a fairly tight tolerance.
In the example I gave above, I might have done:
syms u
err = s - double(int(sin(u),[0,pi]))
err =
0.000109517315004304

2 Comments

hi john thanks for help
if i want to consider the error as input
what should i do?
Are you saying that you need to have a tolerance on the allowed error? Since you don't know the true integral in general, you cannot really do that easily.
That does not say you can do nothing, just that it may not be easy. And easy is often a function of your programming skill, as well as knowledge of mathematics.
A simple trick is to to TWO integrations. Thus use 5 points, and then use 9. The nice thing is you do not need to re-evaluate the function at those 5 points, since they are also needed for the 9 point template. But you should see that you COULD keep on refining the problem, each time cutting the interval in half. (What matters is if you have n points in iteration 1, then 2*n-1 points for the next step.) At each refinement, the error should decrease by a roughly predictable factor.
So if you do so, then you can compare the result from the coarser iteration to the finer one. If that difference is larger than your "tolerance" then you keep refining the interval. In fact, this can be the basis for a method called Richardson extrapolation, where you effectively use the two estimates to implicitly derive a higher order approximation to the integral. And you can even do a multi-level Richardson extrapolant.
Finally, you can be more tricky yet, and only refine the integration in the sub-intervals where the integration is not yet "converged". That in turn requires you to be tricky in that you need to apportion the integration tolerance among the sub-intervals.
If you are asking about the casewhere you are assumed to know the true value of the integral, then the answer is simple. Just increase n until the deviation from ground truth is less than your tolerance. Again, if you are smart, you can re-use the evals from the previous iteration for efficiency.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!