a) og b) og c)
function result = simpsons(a,b,N,f) N = N+isEven(N)-1; h = calcDelta(a,b,N); result = h/3*f(h*0); mult = [4,2]; for i = 1:N-1 result = result + h/3*f(h*i)*mult(mod(i,2)+1); end result = result + h/3*f(h*N); end function even = isEven(n) even = mod(n,2)+1; end function h = calcDelta(a,b,n) h = (b-a)/n; end
c)
1.9832 = simpsons(0,pi,14,@(x)sin(x))
d)
function err = deviation(a,b,N,f,corr) err= corr-simpsons(a,b,N,f); end