Está en la página 1de 6

ORGANIZAR macros en EXCEL PARA DESARROLLAR POR LOS METODOS DE

LAGRANGE Y DE NEWTON COMO SE INDICA EN IMAGEN. ORGANICE SU


INFORMACION EN UNA HOJA POR METODO.

RESULTADOS METODO DE LAGRANGE. Interpola 𝑓(𝑥) = 𝑒 para x=3.8 con polinomio de orden
2.

CODIGO PARA IMPRIMIR TITULOS


Sub imprimirTitulos()

Cells(1, 1).Value = "a"


Cells(2, 1).Value = "b"
Cells(3, 1).Value = "I"
Cells(3, 2).Value = "aleatorios"
Cells(3, 3).Value = "x(I)"
Cells(3, 4).Value = "y(I)"
Cells(1, 5).Value = "x a interpolar"
Cells(2, 5).Value = "orden polinomio"
Cells(3, 5).Value = "valor interpolado"
Cells(6, 5).Value = "Valor Verdadero"
Cells(9, 5).Value = "Error %"
Cells(3, 8).Value = "TABLAS DE DATOS DEPENDIENDO DEL ORDEN"
Cells(4, 8).Value = "i"
Cells(4, 9).Value = "xi(i)"
Cells(4, 10).Value = "yi(i)"
Cells(1, 7).Value = "Pos"
Cells(2, 7).Value = "Pos1"
Cells(1, 3).Value = "N:total datos"

End Sub

RESULTADOS METODO DE NEWTON Interpola 𝑓(𝑥) = 𝑒 para x=3.8 con polinomio de orden 2.
CODIGO DE LLAMADO A PROCESOS PARA REALIZAR PASOS 1 A PASO 7
PARA ALGORITMOS METODOS DE NEWTON Y LAGRANGE. LUEGO DE
HACER PASOS DE 1 A 7 SE HACE LLAMADO AL PROCEDIMIENTO QUE
CALCULE POR EL RESPECTIVO METODO, SE PUEDE UTILIZAR UN
OPCION PARA ESCOGER POR CUAL METODO VA A RESOLVER.
RECORDAR QUE CADA METODO SE TRABAJA EN UNA HOJA APARTE.
AQUÍ LINEA Sheets("Lagrange").Activate
ACTIVA LA HOJA “Lagrange” PARA PROCESO DE LAGRANGE

Option Explicit
Sub interpolacion()

Dim x() As Double, y() As Double, a As Double, b As Double, n_datos As Integer


Dim fila As Integer, xainterpolar As Double, Pos As Integer, Pos1 As Integer, orden
As Integer
Dim valor As Double, elerror As Double
Dim valorV As Double

Sheets("Lagrange").Activate
Range("A5:Z32767").ClearContents

Call imprimirTitulos

'ENTRADA DE DATOS
'leer a,b,n_datos, xainterpolar y orden

'INICIALICE LAS VARIABLES

elerror = 1
fila = 4

'DIMENSIONE LOS ARREGLOS x() y y()


ReDim x(n_datos)
ReDim y(n_datos)

'LLAMADO A PROCESO PARA CALCULAR LOS NUMEROS ALEATORIOS E


IMPRIMIRLOS EN AL COLUMNA 2, imprima los valores de la variable temporal i en
la columna 1.

Call calcularAleatorios(n_datos,x,a,b,fila)
'LLAMADO A PROCESO PARA ORDENAR arreglo x() IMPRIMIR datos ordenados
en la columna 3

Call OrdenarBurbuja(x, n_datos)

'ACTUALIZA LA FILA
fila = 4

'LLAMADO a proceso para calcular y almacenar valores para y(). Imprimir valores de
y() en la columna 4.

Call calcularfdx(n_datos,x,y,fila)

'LLAMADO a proceso para calcular Pos

Call calculaPos(n_datos, xainterpolar,x,Pos)

'IMPRIMIR a Pos

Cells(1, 8).Value = Pos

'LLAMADO a proceso para calcular Pos1

Call calculaPos1(Pos,Pos1)

'IMPRIMIR a Pos1

Cells(2, 8).Value = pos1

'ACTUALIZA LA FILA

fila = 5

'DIMENSIONA ARREGLOS TABLA PEQUENA DEL TAMANNO DE orden. xi()


and yi()

Dim xi() As Double, yi() As Double

ReDim xi(orden), yi(orden)


'LLAMADO A PROCESO ALMACENAR INFO EN arreglos xi() y yi() a partir de
la posicion pos1 del arreglo x() y y(). IMPRIMIR EN COLUMNAS H,I,J a partir de la
fila=5
Call almacenaxiyi(orden,xi,yi,fila)

'HASTA AQUÍ PASOS 1 A 7. METODO DE LAGRANGE O NEWTON A


PARTIR DE AQUÍ.
'PROCESO DE INTERPOLACION DE LAGRANGE – crear FUNCTION
calculaLagrange(orden, xainterpolar,xi)

valor= calculaLagrange(orden, xainterpolar,xi)

‘IMPRIME RESULTADO DEL VALOR INTERPOLADO Y CALCULA EL


ERROR

Cells(3, 6).Value = valor


'valor verdadero con la funcion

valorV = f(xainterpolar)
Cells(7, 5).Value = valorV

If (valorV <> 0) Then


elerror = Abs((valorV - valor) / valorV) * 100
End If
Cells(10, 5).Value = elerror

End Sub

Sub calculaPos1(pos,pos1)
Select Case pos
Case 0 'first position
pos1 = 0
Case n_datos 'last position
pos1 = pos - orden
Case n_datos - orden To n_datos - 1 'any position between N-order and the one
before last
pos1 = n_datos - orden
Case Else 'any position in between
pos1 = pos - 1
End Select
End Sub

Sub imprimirTitulos()
Cells(1, 1).Value = "a"
Cells(2, 1).Value = "b"
Cells(3, 1).Value = "I"
Cells(3, 2).Value = "aleatorios"
Cells(3, 3).Value = "x(I)"
Cells(3, 4).Value = "y(I)"
Cells(1, 5).Value = "x a interpolar"
Cells(2, 5).Value = "orden polinomio"
Cells(3, 5).Value = "valor interpolado"
Cells(6, 5).Value = "Valor Verdadero"
Cells(9, 5).Value = "Error %"
Cells(3, 8).Value = "TABLAS DE DATOS DEPENDIENDO DEL ORDEN"
Cells(4, 8).Value = "i"
Cells(4, 9).Value = "xi(i)"
Cells(4, 10).Value = "yi(i)"
Cells(1, 7).Value = "Pos"
Cells(2, 7).Value = "Pos1"
Cells(1, 3).Value = "N:total datos"
End Sub

Function generaaletorio(a, b)
generaaletorio = (b - a) * Rnd() + a
End Function

Function f(x)
f = Exp(x)
End Function

Sub OrdenarBurbuja(VectorOriginal, n)
Dim Temp As Double, i As Integer, j As Integer
For i = 0 To n - 1
For j = i + 1 To n
If VectorOriginal(i) > VectorOriginal(j) Then
Temp = VectorOriginal(i)
VectorOriginal(i) = VectorOriginal(j)
VectorOriginal(j) = Temp
End If
Next j
Next i
End Sub

=============================

También podría gustarte