Académique Documents
Professionnel Documents
Culture Documents
Create a macro enabled Excel file with name “Solving a Polynomial Equation.xlsm”.
Rename the first sheet as “Data” and type the details given in the image below.
Add a command button on the sheet and rename it as “Start”.
Now click on Developer menu, click on Macros, delete all previous macros and add the following
code in a new Macro. Make sure that line continuation delimiter “_” is added for long lines of code.
After adding the code, enter some values as coefficients and click on “Start” button to check the
result.
Sub Main()
' Solve a polynomial equation using Newton Raphson method
' Set up application progress message
Application.DisplayStatusBar = True
Application.StatusBar = "Calculations in progress. Please wait"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
cgpi = 0
Call init_params
Call solve_eqn
Sub init_params()
' Open files
Workbooks("Solving a Polynomial Equation.xlsm").Activate
Worksheets("Data").Activate
Call clear_chart
Cells(6, 4) = ""
Cells(6, 5) = ""
Cells(7, 4) = ""
Cells(8, 4) = ""
row_no = 3
col_no = 3
coeff(0) = 0
der_coeff(0) = 0
k=5
For i = 1 To 11
coeff(i) = Cells(row_no, col_no)
der_coeff(i) = k * Cells(row_no, col_no)
col_no = col_no + 1
k=k–1
Next
iterations = 1000
x=1
chart_row_no = 11
Call compute_fn
Cells(chart_row_no, 2) = Round(x, 4)
Cells(chart_row_no, 3) = Round(fn, 2)
chart_row_no = chart_row_no + 1
End Sub
Sub solve_eqn()
' Generate successive approximations of X
For j = 1 To iterations
If Abs(fn) > 0.0001 Then
x = (x - new_ratio)
Call compute_fn
Cells(chart_row_no, 2) = Round(x, 4)
Cells(chart_row_no, 3) = Round(fn, 2)
chart_row_no = chart_row_no + 1
Else
Cells(5, 4).Value = x
Cells(6, 4).Value = fn
Cells(7, 4) = j
j = iterations
End If
Next
End Sub
Sub compute_fn()
' Compute the value of the function
fn = ((coeff(1) * x ^ 10) + (coeff(2) * x ^ 9) + (coeff(3) * x ^ 8) + (coeff(4) * x ^ 7) + (coeff(5) * x ^ 6)) +
_ ((coeff(6) * x ^ 5) + (coeff(7) * x ^ 4) + (coeff(8) * x ^ 3) + (coeff(9) * x ^ 2) + (coeff(10) * x) +
coeff(11))
End Sub
Sub create_chart()
Sheets("Data").Select
Range("F20").Select
x_values = "B11:B" + Trim(Str(chart_row_no))
chart_range = "C11:C" + Trim(Str(chart_row_no))
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.ChartType = xlLine
.SetSourceData Source:=Workbooks(1).Sheets("Data").Range("B10:C1011")
.SeriesCollection(1).XValues = Range(x_values)
.SeriesCollection(1).Values = Range(chart_range)
.Axes(xlCategory).TickLabels.Font.FontStyle = "Bold"
' .Axes(xlCategory).TickLabels.NumberFormat = "3"
.Axes(xlValue).TickLabels.Font.FontStyle = "Bold"
.HasTitle = True
.HasLegend = False
.ChartTitle.Text = "Y = f(x)"
.Parent.RoundedCorners = True
.ChartArea.Height = 200
.ChartArea.Width = 300
.ChartArea.Format.Fill.ForeColor.RGB = RGB(204, 255, 255)
.SetElement (msoElementChartTitleAboveChart)
.ChartTitle.Characters.Font.Size = 12
.SeriesCollection(1).Select
End With
ActiveChart.SeriesCollection(2).Select
Selection.Delete
' Resize and Position the chart on worksheet
With ActiveChart.Parent
.Left = 230
.Width = 375
.Top = 110
.Height = 225
End With
End Sub
Sub clear_chart()
On Error Resume Next
ActiveSheet.ChartObjects(1).Select
Selection.Delete
chart_row_no = 11
For i = 1 To 1000
Cells(chart_row_no, 2) = ""
Cells(chart_row_no, 3) = ""
chart_row_no = chart_row_no + 1
Next
End Sub