Well I am asked to do this : (VBA EXCEL) Write a user-defined vba function to de
ID: 3591151 • Letter: W
Question
Well I am asked to do this : (VBA EXCEL)
Write a user-defined vba function to determine the real root of f(x) = 5x^3 - 5x^2 + 6x - 2 when xl = 0 xu = 1 and es = 1%.
Below is my code, and I do not know why it is not working or what is wrong with it. It does not work. Please help!!
Function f(x) f=5*x^3-5*x^2+6*x-2 End Function Function Bisect(xl, xu, es) Performs Bisection method Dim xrold As Double, test As Double su=1 tsl = f(xl ) ea-100 Relative Error to test xtold =su 'Initial guess of the root loops 0 'Will be used for counting Do While ea eAnd loops 50 XC = (x1 +xy) / 2 loops loops +1 If O Then End If test-f(xl) * f(&W; If test 0 Then Else End If lfea= 50 Then Exit Do Loop Bisectx Sheets("Q3" Cells(4, 7) End FunctionExplanation / Answer
Module VBModule
Function Fun(ByVal x As Double)
return (5 * (x ^ 3)) - (5 * (x ^ 2)) + (6 * x) - 2
End Function
Sub Bisect(ByVal xl As Double, ByVal xu As Double, ByVal es As Double)
Dim itr As Integer
Dim xr As Double, xrOld As Double, err As Double, test As Double, newErr As Double
itr = 0
xr = xl
es = es / 100.0
Do while True
xrOld = xr
xr = (xl + xu) / 2.0
itr = itr + 1
test = Fun(xl) * Fun(xr)
newErr = Math.Abs((xr - xrold) / xr) * 100
If test < 0 Then
xu = xr
ElseIf test > 0 Then
xl = xr
Else
newErr = 0
End If
If newErr <= es Then
Exit Do
End If
Loop
Console.WriteLine(xr)
End Sub
Sub Main()
Bisect(0, 1, 1)
End Sub
End Module
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.