0% found this document useful (0 votes)
6 views8 pages

Simpson's Rule Integral Calculator

The document describes a Simpson's rule integration program in MATLAB. The program takes a function, lower and upper bounds of integration, and number of partitions as input. It calculates the integral approximation using Simpson's rule and estimates the truncation error. For example, when given the function f(x)=x^5+x+2 on [-1,0] partitioned into 2 intervals, it returns the integral as 1.33203125 and the truncation error as 0.001953125.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Simpson's Rule Integral Calculator

The document describes a Simpson's rule integration program in MATLAB. The program takes a function, lower and upper bounds of integration, and number of partitions as input. It calculates the integral approximation using Simpson's rule and estimates the truncation error. For example, when given the function f(x)=x^5+x+2 on [-1,0] partitioned into 2 intervals, it returns the integral as 1.33203125 and the truncation error as 0.001953125.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//PROGRAMA SIMPSON

function Simpson
syms x
f1 = input('Ingrese la funcion f(x)= ','s')
a = input('Ingrese el limite inferior a = ')
b = input('Ingrese el limite inferior b = ')
n = input('Ingrese el numero de particiones n = ')
H = (b-a)/n
h = (b-a)/(2*n)
X = zeros(1,n+1)
X(1)= a

for i=2:1:n+1
X(i) = X(1) + (i-1)*H
end
disp(X)
I = 0;
ET = 0;
f = inline(f1)
df4 = diff(f1,x,4)
for i=1:1:n
I = I + (h/3)*(f(X(i))+4*f((X(i)+X(i+1))/2) + f(X(i+1)));
z = X(i): (X(i)+X(i+1))/10 : X(i+1);
y = subs(df4,x,z);
K = max(abs(y))
ET = ET + abs(-(1/90)*K*h.^5);
end
disp('La aproximacion de la integral es: ')
disp(vpa(I,10))
%disp('Los valores de K son: ')
%disp(K)
disp('El error de truncamiento es: ')
disp(vpa(ET))

//COMPILACION

>> Simpson

Ingrese la funcion f(x)= log(x)

f1 =

log(x)

Ingrese el limite inferior a = 2

a=
2

Ingrese el limite inferior b = 3

b=

Ingrese el numero de particiones n = 2

n=

H=

0.5000

h=

0.2500

X=

0 0 0
X=

2 0 0

X=

2.0000 2.5000 0

X=

2.0000 2.5000 3.0000

2.0000 2.5000 3.0000

f=

Inline function:

f(x) = log(x)

df4 =

-6/x^4

K=

0.3750
K=

0.1536

La aproximacion de la integral es:

0.909538787

El error de truncamiento es:

0.0000057356770833333328485142618702319

>> f = dsolve('Dy = (exp(x)-y)/x','y(1)=2','x')

f=

exp(x)/x - (exp(1) - 2)/x

>> f = dsolve('Dy = (exp(x)-y)/x','y(1)=2')

f=

exp(x) - exp(-t/x)*exp(1/x)*(exp(x) - 2)

>> f = dsolve('Dy = (exp(x)-y)/x','y(1)=2','x')

f=

exp(x)/x - (exp(1) - 2)/x

>> f = dsolve('Dy = (exp(x)-y)/x','y(1)=2','x')


f=

exp(x)/x - (exp(1) - 2)/x

>> h = dsolve('Dy = (exp(x)-y)/x','x')

h=

exp(x)/x + C6/x

>> syms x

>> y3 = subs(f,x,3)

y3 =

6.4558

>>

function Simpson
syms x
f1 = input('Ingrese la funcion f(x)= ','s')
a = input('Ingrese el limite inferior a = ')
b = input('Ingrese el limite inferior b = ')
n = input('Ingrese el numero de particiones n = ')
H = (b-a)/n
h = (b-a)/(2*n)
X = zeros(1,n+1)
X(1)= a

for i=2:1:n+1
X(i) = X(1) + (i-1)*H
end
disp(X)
I = 0;
ET = 0;
f = inline(f1)
df4 = diff(f1,x,4)
for i=1:1:n
I = I + (h/3)*(f(X(i))+4*f((X(i)+X(i+1))/2) + f(X(i+1)));
z = X(i): (X(i+1)-X(i))/10 : X(i+1);
y = subs(df4,x,z);
K = max(abs(y))
ET = ET + abs(-(1/90)*K*h.^5);
end
disp('La aproximacion de la integral es: ')
disp(vpa(I,10))
%disp('Los valores de K son: ')
%disp(K)
disp('El error de truncamiento es: ')
disp(vpa(ET))

>> Simpson

Ingrese la funcion f(x)= x^5 + x+ 2

f1 =

x^5 + x+ 2

Ingrese el limite inferior a = -1

a=

-1

Ingrese el limite inferior b = 0

b=

Ingrese el numero de particiones n = 2

n=

2
H=

0.5000

h=

0.2500

X=

0 0 0

X=

-1 0 0

X=

-1.0000 -0.5000 0

X=

-1.0000 -0.5000 0
-1.0000 -0.5000 0

f=

Inline function:

f(x) = x^5 + x+ 2

df4 =

120*x

K=

120

K=

60

La aproximacion de la integral es:

1.33203125

El error de truncamiento es:

0.001953125

You might also like