This document describes using the modified secant method in VBA macros in Excel to calculate the root of a non-linear equation. It provides code to calculate 10 sample points of the equation X^3+2X^2+10X-20, plot the results on a graph, and then implement the modified secant method iteratively to calculate the root, displaying the results and number of iterations.
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
0.0. secante modificada
1. Cálculo de la raíz de una ecuación no lineal mediante el Método Numérico de la Secante
Modificado
Utilizando macros de VBA de Excel
Ing. Néstor Augusto Oyarce Linares
naoyarcel@gmail.com
2. aSecanteModificadoGrafica - 1
Option Explicit
Sub XY_14_Dic_2020_6()
Dim X As Double, Y As Double, i As Integer
'Método Numérico de LA SECANTE MODIFICADA
'14 de Diciembre de 2020, ejercicio 6
Cells.Clear
Cells(1, 1).Value = " ECUACION : "
Cells(3, 1).Value = " X^3+2*X^2+10*X-20"
Cells(1, 2).Value = " ENSAYOS : "
Cells(3, 2).Value = " X "
Cells(3, 3).Value = " Y "
For i = 1 To 10
X = 1 + i / 10
Y = (X) ^ (3) + 2 * (X) ^ (2) + 10 * X - 20
Cells(i + 3, 2).Value = X
Cells(i + 3, 3).Value = Y
If Y > 0 Then Exit For
Next i
Range("A1", "A3").Font.Bold = True
Cells.EntireColumn.AutoFit
End Sub
Sub Crea_grafico()
Dim grafico As ChartObject
Dim wks As Worksheet
Set wks = ActiveWorkbook.Sheets("Hoja1")
Set grafico = wks.ChartObjects.Add(Left:=400, Width:=450, Top:=50, Height:=200)
grafico.Name = "Grafico_1"
grafico.Chart.ChartType = xlXYScatterSmoothNoMarkers
grafico.Chart.SetSourceData Source:=wks.Range("B4:D15")
End Sub
' Ing. Néstor Augusto Oyarce Linares
4. bSecanteModificadaCalculo - 1
Option Explicit
Sub Parte2_SECANTEmodificada()
Dim X1 As Double, Y1 As Double, i As Integer, X2 As Double, kX1 As Double, Yb As Double
Dim Err As Double, Ym As Double, X As Double, j As Integer, Y As Double, Y2 As Double
'Función de Recurrencia :
'Metodo de la Secante Modificada
Cells.Clear
Cells(1, 1).Value = "Método Numérico de la Secante Modificada"
Cells(2, 2).Value = "Valor de X"
Cells(2, 3).Value = "Valor de Y"
Cells(2, 5).Value = " Xf "
Cells(2, 6).Value = " Yf "
Cells(2, 7).Value = "Iteraciones"
X1 = 1.3
For j = 1 To 20
kX1 = 0.1 * X1
Y1 = (X1) ^ (3) + 2 * (X1) ^ (2) + 10 * X1 - 20
Y2 = (X1 + kX1) ^ (3) + 2 * (X1 + kX1) ^ (2) + 10 * (X1 + kX1) - 20
X2 = X1 - Y1 * kX1 / (Y2 - Y1)
Y = (X2) ^ (3) + 2 * (X2) ^ (2) + 10 * X2 - 20
Err = Abs(X2 - X1)
Cells(j + 3, 2).Value = X2
Cells(j + 3, 3).Value = Y2
If Err <= 0.000001 Then Exit For
X1 = X2
Next j
X = X2
Y = (X) ^ (3) + 2 * (X) ^ (2) + 10 * X - 20
Cells(4, 5).Value = X
Cells(4, 6).Value = Y
Cells(4, 7).Value = j
Range("E1", "E4").Font.Bold = True
Range("E1", "E4").Font.Color = RGB(8, 44, 196)
Cells.EntireColumn.AutoFit
End Sub
' Ing. Néstor Augusto Oyarce Linares
5. Método Numérico de la Secante Modificada
Valor de X Valor de Y Xf Yf Iteraciones
1.367588428 1.314007 1.368808105 -5.85303E-08 5
1.368761073 2.97402333
1.368806277 3.00345259
1.368808037 3.00458749
1.368808105 3.004631658