You are on page 1of 8

Trapecio

Option Explicit

Function f(x)

f = 3 - (x) + 0.3 * (x ^ 2)

End Function

Function trapezoidal(a, b)

trapezoidal = (b - a) * (f(a) + f(b)) / 2

End Function

Sub funcionCuadratica()

Dim a, b As Double

Dim trapecio As Double

Sheets("trapezoidal").Activate

a = Cells(1, 2).Value

b = Cells(2, 2).Value

trapecio = trapezoidal(a, b)

Cells(4, 1).Value = "resultado"

Cells(4, 2).Value = trapecio

End Sub

Multiple trapezoidal

Option Explicit

Sub calcularintegrales()

Dim a As Double, b As Double, n As Integer, integral As Double

Dim x() As Double, h As Double, i As Integer

Dim fdx() As Double, suma As Double


Sheets("trapezoidalMultiple").Activate

Range("b4:k32767").ClearContents

a = Cells(1, 2).Value

b = Cells(2, 2).Value

n = Cells(3, 2).Value

If n = 1 Then

integral = trapezoidal(a, b)

Cells(5, 2).Value = integral

Else

ReDim Preserve x(n)

ReDim Preserve fdx(n)

'calculo de h

h = (b - a) / n

x(0) = a

For i = 1 To n

x(i) = x(i - 1) + h

Next i

For i = 0 To n

Cells(i + 6, 2).Value = x(i)

Next i

For i = 0 To n

fdx(i) = fx(x(i))

Cells(i + 6, 3).Value = fdx(i)

Next i

suma = 0

For i = 1 To n - 1

'suma = suma + f(X(i))

suma = suma + fdx(i)

Next i
integral = (b - a) * (fdx(0) + 2 * suma + fdx(n)) / (2 * n)

Cells(6, 4).Value = integral

End If

End Sub

Function trapezoidal(a, b)

trapezoidal = (b - a) * (fx(a) + fx(b)) / 2

End Function

Function fx(x)

fx = 3 - (x) + 0.3 * (x ^ 2)

End Function

Simpson 13

Option Explicit

Sub calcularintegralesSimpson13()

Dim a As Double, b As Double, integral, Xi As Double

Dim h As Double

Sheets("simpson13").Activate

a = Cells(1, 2).Value

b = Cells(2, 2).Value

h = (b - a) / 2

Xi = a + h

integral = (b - a) * (f(a) + 4 * f(Xi) + f(b)) / 6


Cells(6, 6).Value = integral

End Sub

Function f(x)

f = 3 - (x) + 0.3 * (x ^ 2)

End Function

Multiple simpson

Option Explicit

Function f(x)

f = 3 - x + 0.3 * x ^ 2

End Function

Sub msimpsonl3()

Dim a, b, h, x(), sumaimpar, sumapar As Double

Dim n, i As Integer

Sheets("Multiplesimpson").Activate

a = Cells(1, 2).Value

b = Cells(2, 2).Value

n = Cells(3, 2).Value

ReDim Preserve x(n)

h = (b - a) / n

x(0) = a
For i = 1 To n

x(i) = x(i - 1) + h

Next i

sumaimpar = 0

For i = 1 To n - 1 Step 2

sumaimpar = sumaimpar + f(x(i))

Next i

sumapar = 0

For i = 2 To n - 2 Step 2

sumapar = sumapar + f(x(i))

Next i

Cells(4, 2).Value = (b - a) * (f(a) + 4 * sumaimpar + 2 * sumapar + f(b)) / (3 * n)

End Sub

Simpson 3.8

Option Explicit

Function f(x)

f = 3 - (x) + 0.3 * x ^ 2

End Function

Sub simpson38()

Dim a, b, h, n, simpson38, x() As Double

Dim i As Integer

Sheets("simpson38").Activate

a = Cells(1, 2).Value

b = Cells(2, 2).Value

n = Cells(3, 2).Value

ReDim Preserve x(n)

h = (b - a) / 3

x(0) = a

For i = 1 To n
x(i) = x(i - 1) + h

Next i

simpson38 = (b - a) * (f(x(0)) + 3 * f(x(1)) + 3 * f(x(2)) + f(b)) / 8

Cells(4, 2).Value = simpson38

End Sub

Cuadratura gauss

Option Explicit

Sub cuadraturagauss()

Dim puntos As Integer, a As Double, b As Double

Dim Ci() As Double, Xi() As Double, fila As Integer, i As Integer

Dim resultado As Double, x() As Double, fdx As Double, f() As Double

Sheets("metodogauss").Activate

puntos = Cells(2, 4).Value

a = Cells(2, 8).Value

b = Cells(3, 8).Value

resultado = 0

Select Case puntos

Case 2

fila = 6

Case 3

fila = 8

Case 4
fila = 11

Case 5

fila = 15

Case 6

fila = 20

Case Is < 0

MsgBox "NO permite numeros negativos"

puntos = 2

fila = 7

Case Else

MsgBox "NO permite numeros FUERA DE RANGO (PUNTOS DE 2 A 6)"

puntos = 2

fila = 7

End Select

ReDim Preserve Ci(puntos - 1)

ReDim Preserve Xi(puntos - 1)

ReDim Preserve x(puntos - 1)

ReDim Preserve f(puntos - 1)

fila = 7

For i = 0 To puntos - 1

Ci(i) = Cells(fila, 3).Value

Xi(i) = Cells(fila, 4).Value

x(i) = (b + a) / 2 + (b - a) / 2 * Xi(i)

f(i) = (3 - x(i) + 0.3 * (x(i) ^ 2)) * (b - a) / 2

fila = fila + 1

Next i

fila = 7

For i = 0 To puntos - 1

Cells(fila, 7).Value = i
Cells(fila, 8).Value = Ci(i)

Cells(fila, 9).Value = Xi(i)

Cells(fila, 10).Value = x(i)

Cells(fila, 11).Value = f(i)

resultado = resultado + Ci(i) * f(i)

Cells(fila, 12).Value = resultado

fila = fila + 1

Next i

Cells(4, 8).Value = resultado

End Sub

You might also like