Sei sulla pagina 1di 31

1.

programas de trapecio simple y Simpson simple


a.-trapacio simple

function trapsim
global fun
fun=input('ingrese la funcion\n','s');
b=input('ingrese el limite superior de la integral\n');
a=input('ingrese el limite inferior de la integral\n');
h=b-a
x=a;
fa=eval(fun);
x=b;
fb=eval(fun);
I=(h/2)*(fa+fb);
fprintf('El valor aproximado es =%12.10f',I)

b. Simpson simple

function simpson
global fun
fun=input('ingrese la funcion\n','s');
b=input('ingrese el limite superior de la funcion\n');
a=input('ingrese el limite inferior de la integral\n');
h=(b-a)/2
x=a;
fa=eval(fun);
x=b;
fb=eval(fun);
x=a+h;
fc=eval(fun);
I=(3*h/8)*(fa+4*fc+fb)
fprintf('El valor aproximado de la integral es =%12.10f',I)

c. regla de los tres octavos


function regla3/8
global fun
fun=input('ingrese la funcion\n','s');
b=input('ingrese el limite superior de la funcion\n');
a=input('ingrese el limite inferior de la integral\n');
h=(b-a)/3
x=a;
fa=eval(fun);
x=a+h;
fc=eval(fun);
x=a+2*h;
fd=eval(fun);
x=b;
fb=eval(fun);
I=(3*h/8)*(fa+3*fc+3*fd+fb)
fprintf('El valor aproximado de la integral es =%12.10f',I)
2. programa de la regla del trapecio compuesto y Simpson compuesto
2.1 trapecio compuesto

function trapcomp
global fun
fun=input('ingrese la funcion\n','s');
b=input('ingrese el limite superior de la integral\n');
a=input('ingrese el limite inferior de la integral\n');
M=input('ingrese el numero de intervalos\n');
h=(b-a)/M;
I=0;
for k=1:M-1
x=a+h*k
f1=eval(fun)
I=I+f1
end
x=a
fa=eval(fun)
x=b
fb=eval(fun)
I=((h/2)*(fa+fb))+(h)*(I)
fprintf('El valor aproximado es =%12.10f',I)

2.2 Simpson compuesto

function simpscomp
global fun
fun=input('ingrese la funcion\n','s');
b=input('ingrese el limite superior de la integral\n');
a=input('ingrese el limite inferior de la integral\n');
n=input('ingrese el numero de intervalos');
h=(b-a)/(2*n)
i1=0
i2=0
for k=1:n-1
x=a+h*(2*k)
f1=eval(fun)
i1=i1+f1
end
for k=1:n
x=a+h*(2*k-1)
f2=eval(fun)
i2=i2+f2
end
x=a
fa=eval(fun)
x=b
fb=eval(fun)
i=(h/3)*((fa+fb)+(2*i1)+(4*i2))
fprintf('el valor aproximado de la integral es =%12.10f',i)
3. calculo de la integral por la regla del trapecio, Simpson y 3/8

I=

a. regla de trapecio

utilizar el comando trapsim en el comando window

ingrese la funcion

tan(x)

ingrese el limite superior de la integral

pi/4

ingrese el limite inferior de la integral

h=

0.7854

El valor aproximado es =0.3926990817>>

b. regla de simpson

utilizar el comando simpson en el comando window

ingrese la funcion

tan(x)

ingrese el limite superior de la funcion

pi/4

ingrese el limite inferior de la integral

h=

0.3927
I=

0.3913

El valor aproximado de la integral es =0.3912540840>>

c. regla de trapecio

utilizar el comando regla3/8 en el comando window

ingrese la funcion

tan(x)

ingrese el limite superior de la funcion

pi/4

ingrese el limite inferior de la integral

h=

0.2618

I=

0.3471

El valor aproximado de la integral es =0.3471360122>>

De los tres resultados anteriores el mas aproximado es por la regla de tres


octavos

4. calcular: I=
a.usar regla de Simpson compuesto :
a.1 con n=4 utilizar el comando simpscomp en el comando window
ingrese la funcion
tan(x)
ingrese el limite superior de la integral
pi/4
ingrese el limite inferior de la integral
0
ingrese le numero de intervalos 4

h=

0.0982

i1 =

i2 =

x=

0.1963

f1 =

0.1989

i1 =

0.1989

x=

0.3927

f1 =

0.4142

i1 =
0.6131

x=

0.5890

f1 =

0.6682

i1 =

1.2813

x=

0.0982

f2 =

0.0985

i2 =

0.0985

x=

0.2945

f2 =

0.3033

i2 =

0.4018

x=
0.4909

f2 =

0.5345

i2 =

0.9363

x=

0.6872

f2 =

0.8207

i2 =

1.7570

x=

fa =

x=

0.7854

fb =

1.0000

i=
0.3466

el valor aproximado de la integral es =0.3465805405>>

a.2 con n=8 utilizar el comando simpscomp en el comando window

ingrese la funcion
tan(x)
ingrese el limite superior de la integral
pi/4
ingrese el limite inferior de la integral
0
ingrese le numero de intervalos>> 8

h=

0.0491

i1 =

i2 =

x=

0.0982

f1 =

0.0985

i1 =

0.0985

x=

0.1963
f1 =

0.1989

i1 =

0.2974

x=

0.2945

f1 =

0.3033

i1 =

0.6008

x=

0.3927

f1 =

0.4142

i1 =

1.0150

x=

0.4909

f1 =

0.5345
i1 =

1.5495

x=

0.5890

f1 =

0.6682

i1 =

2.2177

x=

0.6872

f1 =

0.8207

i1 =

3.0383

x=

0.0491

f2 =

0.0491

i2 =

0.0491
x=

0.1473

f2 =

0.1483

i2 =

0.1975

x=

0.2454

f2 =

0.2505

i2 =

0.4479

x=

0.3436

f2 =

0.3578

i2 =

0.8058

x=

0.4418
f2 =

0.4730

i2 =

1.2787

x=

0.5400

f2 =

0.5994

i2 =

1.8781

x=

0.6381

f2 =

0.7417

i2 =

2.6197

x=

0.7363

f2 =

0.9063
i2 =

3.5261

x=

fa =

x=

0.7854

fb =

1.0000

i=

0.3466

el valor aproximado de la integral es =0.3465740373>>


5. programa. la integración de romberg

function romberg
global fun
fun=input('ingrese la funcion\n','s');
b=input('ingrese el limite superior de la integral\n');
a=input('ingrese el limite inferior de la integral\n');
n=input('ingrese el numero de intervalos\n');
h=(b-a);
M=1;
J=0;
R=zeros(n,n);
x=a;
f1=eval(fun);
x=b;
f2=eval(fun);
R(1,1)=h*(f1+f2)/2;
while(J<(n-1))
J=J+1;
h=h/2;
s=0
for p=1:M
x=a+h*(2*p-1);
f3=eval(fun)
s=s+f3;
end
R(J+1,1)=(1/2)*(R(J,1))+h*s;
M=2*M
for k=1:J
R(J+1,k+1)=R(J+1,k)+(R(J+1,k)-R(J,k))/(4^k-1)
end
end
i=R(J+1,J+1)
ezplot(fun),grid on

6. clacular la integral utilizando romberg.

I= , con n=6 , ejecutamos el comando romberg en el comando


window

ingrese la funcion

(x.^2+x+1)*cos(x)

ingrese el limite superior de la integral

pi/2

ingrese el limite inferior de la integral

ingrese el numero de intervalos

s=

f3 =

1.6986
M=

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

s=

f3 =

1.4292

f3 =

1.3647
M=

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

s=
0

f3 =

1.2112

f3 =

1.6097

f3 =

1.6365

f3 =

0.8318

M=

8
R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

0 0 0 0 0 0
0 0 0 0 0 0

s=

f3 =

1.1025

f3 =

1.3218

f3 =

1.5273

f3 =

1.6693
f3 =

1.6902

f3 =

1.5302

f3 =

1.1336

f3 =

0.4549

M=

16

R=

0.7854 0 0 0 0 0
1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

2.0333 2.0382 0 0 0 0

0 0 0 0 0 0

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

2.0333 2.0382 2.0382 0 0 0

0 0 0 0 0 0

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

2.0333 2.0382 2.0382 2.0382 0 0

0 0 0 0 0 0

R=
0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

2.0333 2.0382 2.0382 2.0382 2.0382 0

0 0 0 0 0 0

s=

f3 =

1.0502

f3 =

1.1563

f3 =

1.2665
f3 =

1.3762

f3 =

1.4798

f3 =

1.5709

f3 =

1.6428

f3 =

1.6882

f3 =
1.6996

f3 =

1.6695

f3 =

1.5904

f3 =

1.4553

f3 =

1.2577

f3 =

0.9918
f3 =

0.6529

f3 =

0.2374

M=

32

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

2.0333 2.0382 2.0382 2.0382 2.0382 0

2.0370 2.0382 0 0 0 0

R=

0.7854 0 0 0 0 0
1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

2.0333 2.0382 2.0382 2.0382 2.0382 0

2.0370 2.0382 2.0382 0 0 0

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

2.0333 2.0382 2.0382 2.0382 2.0382 0

2.0370 2.0382 2.0382 2.0382 0 0

R=

0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

2.0333 2.0382 2.0382 2.0382 2.0382 0

2.0370 2.0382 2.0382 2.0382 2.0382 0

R=
0.7854 0 0 0 0 0

1.7268 2.0406 0 0 0 0

1.9605 2.0384 2.0383 0 0 0

2.0188 2.0382 2.0382 2.0382 0 0

2.0333 2.0382 2.0382 2.0382 2.0382 0

2.0370 2.0382 2.0382 2.0382 2.0382 2.0382

i=

2.0382
7. programa de romberg modificado para aproximar I=

function romberg
global f
f=input('Ingrese la funcion:\n','s');
b=input('Ingrese el limite superior:\n');
a=input('Ingrese el limite inferior:\n');
n=input('Ingrese el numero de particiones:\n');
tol=input('Ingrese la tolerancia:\n');
disp('********************************************************')
disp('********************************************************')
M=1;
h=b-a;
err=1;
J=0;
R=zeros(4,4);
x=a;
f_a=eval(f);
x=b;
f_b=eval(f);
R(1,1)=h*(f_a+f_b)/2;
disp(' quad err h')
while((err>tol)&(J<n))|(J<4)
J=J+1;
h=h/2;
s=0;
for p=1:M
x1=a+h*(2*p-1);
x=x1;
f_x1=eval(f);
s=s+f_x1;
end
R(J+1,1)=R(J,1)/2+h*s;
M=2*M;
for K=1:J
R(J+1,K+1)=R(J+1,K)+(R(J+1,K)-R(J,K))/(4^K-1);
end
err=abs(R(J,J)-R(J+1,K+1));
fprintf('%10.9f %10.9f %10.9f\n',R(J+1,J+1),err,h)
end
disp('********************************************************')
disp('LA MATRIZ TRIANGULAR INFERIOR ES:')
disp(R)
disp('********************************************************')
disp('El error es para el numero de particiones:')
disp(err)
disp('********************************************************')
disp('El tamaño de la ultima particion es:')
disp(h)
disp('********************************************************')
disp('La respuesta es:')
disp(R(J+1,J+1))

Ingrese la funcion:

(x.^2)*exp(-x.^2)

Ingrese el limite superior:

Ingrese el limite inferior:

Ingrese el numero de particiones:

30

Ingrese la tolerancia:

0.0000000001

********************************************************

********************************************************

quad err h

0.514926773 0.441664218 1.000000000

0.416589814 0.098336960 0.500000000

0.422812099 0.006222285 0.250000000

0.422725008 0.000087091 0.125000000

0.422725055 0.000000048 0.062500000


0.422725056 0.000000001 0.031250000

0.422725056 0.000000000 0.015625000

********************************************************

LA MATRIZ TRIANGULAR INFERIOR ES:

0.0733 0 0 0 0 0 0 0

0.4045 0.5149 0 0 0 0 0 0

0.4182 0.4227 0.4166 0 0 0 0 0

0.4216 0.4227 0.4227 0.4228 0 0 0 0

0.4224 0.4227 0.4227 0.4227 0.4227 0 0 0

0.4227 0.4227 0.4227 0.4227 0.4227 0.4227 0 0

0.4227 0.4227 0.4227 0.4227 0.4227 0.4227 0.4227 0

0.4227 0.4227 0.4227 0.4227 0.4227 0.4227 0.4227 0.4227

********************************************************

El error es para el numero de particiones:

1.5439e-012

********************************************************

El tamaño de la ultima particion es:

0.0156

********************************************************

La respuesta es:

0.4227

Potrebbero piacerti anche