MATLAB: Hi I have a problem with Matlab i want Computing error in Simpson program the program is:

MATLAB

please help me
thank you

Best 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
Related Question