Do you have the symbolic toolbox?
If so, start with the int command. You can do both indefinite and definite integration.
syms x real
f = 5 + 1.0./sqrt(1-x^2)
Q = int(f,x)
Q = int(f,x,0,1/2)
Now that we have an anlytical answer, , we can validate numerical schemes. First we should make a vectorised anonymous function of the integrand,
f = @(x) 5 + 1.0./sqrt(1-x.^2)
Matlab has pre-canned routines for the final two integration schemes:
A3 =quadl(f,0,0.5)
A4=integral(f,0,0.5)
For the (composite) trapz and Simpson's you need to decide on a step size (as you did above).
h = 0.1;
x=0:h:1/2;
A1= trapz(x, f(x))
A quick hack of the Simpson's rule is
n = 10;
a = 0; b = 0.5;
x = linspace(a,b,n+1)';
c = [1,repmat([4 2],1,n/2-1),4,1];
A2 = (b-a)*c*f(x)/n/3;
It might be prudent to check all numerical values with the analytical one, especially the Simpson's implementation above.
Best Answer