# Tema 9
09-4  Cálculo aproximado de integrales
          Integración numérica

kill(all)$

f(x):=1 + cos(x)/(100-x^6)$
'integrate(f(x), x, 0, 2)=integrate(f(x), x, 0, 2);

wxplot2d([f(x)], [x, 0, 2], [y, 0.96, 1.04] , [ylabel, ""] , [legend, "f(x)"] , [style, [lines, 2]] ,
 [plot_format, gnuplot],  [gnuplot_preamble, "set size ratio 1; set grid"]  )$

## Método de los trapecios

h=h:0.2$        n1=2/h;

IA_trapecios_1=T1:h*((f(0) + f(2))/2 + sum(f(h*n), n, 1, 9)), numer;

kill(h)$        h=h:0.1$        n2=2/h;

IA_trapecios_2=T2:h*((f(0) + f(2))/2 + sum(f(h*n), n, 1, 19)), numer;

kill(h)$        h=h:0.05$       n3=2/h;

IA_trapecios_3=T3:h*((f(0) + f(2))/2 + sum(f(h*n), n, 1, 39)), numer;

kill(h)$        h=h:0.01$       n4=2/h;

IA_trapecios_4=T4:h*((f(0) + f(2))/2 + sum(f(h*n), n, 1, 199)), numer;

kill(h)$        h=h:0.005$      n5=2/h;

IA_trapecios_5=T5:h*((f(0) + f(2))/2 + sum(f(h*n), n, 1, 399)), numer;

## Método de los rectángulos

kill(h)$        h=h:0.2$        n1=2/h;

IA_rectángulos_1=R1:h*(sum(f(h*(n + n+1)/2), n, 0, 9)), numer;

kill(h)$        h=h:0.1$        n2=2/h;

IA_rectángulos_2=R2:h*(sum(f(h*(n + n+1)/2), n, 0, 19)), numer;

kill(h)$        h=h:0.05$       n3=2/h;

IA_rectángulos_3=R3:h*(sum(f(h*(n + n+1)/2), n, 0, 39)), numer;

kill(h)$        h=h:0.01$       n4=2/h;

IA_rectángulos_4=R4:h*(sum(f(h*(n + n+1)/2), n, 0, 199)), numer;

kill(h)$        h=h:0.005$      n5=2/h;

IA_rectángulos_5=R5:h*(sum(f(h*(n + n+1)/2), n, 0, 399)), numer;

## Método de Simpson

kill(h)$        h=h:0.2$        n1=n1:2/h;     p1=n1/2;

IA_Simpson_1=
S1:(h/3)*(sum(f(h*2*n) + 4*f(h*(2*n+1)) + f(h*(2*n+2)), n, 0, 4)), numer;

kill(h)$        h=h:0.1$        n2=n2:2/h;     p2=n2/2;

IA_Simpson_2=
S2:(h/3)*(sum(f(h*2*n) + 4*f(h*(2*n+1)) + f(h*(2*n+2)), n, 0, 9)), numer;

kill(h)$        h=h:0.02$        n3=n3:2/h;     p3=n3/2;

IA_Simpson_3=
S3:(h/3)*(sum(f(h*2*n) + 4*f(h*(2*n+1)) + f(h*(2*n+2)), n, 0, 49)), numer;

kill(h)$        h=h:0.01$        n4=n4:2/h;     p4=n4/2;

IA_Simpson_4=
S4:(h/3)*(sum(f(h*2*n) + 4*f(h*(2*n+1)) + f(h*(2*n+2)), n, 0, 99)), numer;

## Métodos implementados en wxMaxima

'integrate(f(x), x, 0, 2)=romberg(f(x), x, 0, 2);

quad_qags(f(x), x, 0, 2);