Question

In: Computer Science

Please write a VBA code to solve the solution for below non-linear equation by NewtonRaphson method...

Please write a VBA code to solve the solution for below non-linear equation by NewtonRaphson method with convergence criteria e = 10 ^ -6, where index is your class index number. If your index is odd number, please use initial trial x = -5, if your index number is even number, please use initial trial x = 5. Return your solution in a message box. ? = ? 2 ????? + 1 − ???(?) = 0

Index = 12

Solutions

Expert Solution

Option Infer On

' The NewtonRaphsonSolver class resides in the 
' Extreme.Mathematics.EquationSolvers namespace.
Imports Extreme.Mathematics.EquationSolvers
' Function delegates reside in the Extreme.Mathematics
' namespace.
Imports Extreme.Mathematics

Namespace Extreme.Numerics.QuickStart.VB
    ' Illustrates the use of the Newton-Raphson equation solver 
    ' in the Extreme.Mathematics.EquationSolvers namespace of the Extreme
    ' Optimization Numerical Libraries for .NET.
    Module NewtonEquationSolver

        Sub Main()
            ' The Newton-Raphson solver is used to solve 
            ' non-linear equations in one variable.
            '
            ' The algorithm starts with one starting value,
            ' and uses the target function and its derivative
            ' to iteratively find a closer approximation to
            ' the root of the target function.
            '
            ' The properties and methods that give you control
            ' over the iteration are shared by all classes
            ' that implement iterative algorithms.

            '
            ' Target function
            '
            ' The function we are trying to solve must be
            ' provided as a Func(Of Double, Double). For more
            ' information about this delegate, see the
            ' Functions QuickStart sample.
            Dim f As Func(Of Double, Double) = AddressOf Math.Sin
            ' The Newton-Raphson method also requires knowledge
            ' of the derivative:
            Dim df As Func(Of Double, Double) = AddressOf Math.Cos
            ' Now let's create the NewtonRaphsonSolver object.
            Dim solver As NewtonRaphsonSolver = New NewtonRaphsonSolver
            ' Set the target function and its derivative:
            solver.TargetFunction = f
            solver.DerivativeOfTargetFunction = df
            ' Set the initial guess:
            solver.InitialGuess = 4
            ' These values can also be passed in a constructor:
            Dim solver2 As New NewtonRaphsonSolver(f, df, 4)

            Console.WriteLine("Newton-Raphson Solver: sin(x) = 0")
            Console.WriteLine("  Initial guess: 4")
            Dim result As Double = solver.Solve()
            ' The Status property indicates
            ' the result of running the algorithm.
            Console.WriteLine("  Result: {0}", solver.Status)
            ' The result is also available through the
            ' Result property.
            Console.WriteLine("  Solution: {0}", solver.Result)
            ' You can find out the estimated error of the result
            ' through the EstimatedError property:
            Console.WriteLine("  Estimated error: {0}", solver.EstimatedError)
            Console.WriteLine("  # iterations: {0}", solver.IterationsNeeded)

            '
            ' When you don't have the derivative...
            '
            ' You can still use this class if you don't have
            ' the derivative of the target function. In this
            ' case, use the static CreateDelegate method of the
            ' NumericalDifferentiation class (Extreme.Mathematics.Calculus
            ' namespace) to create a Func(Of Double, Double)
            ' that represents the numerical derivative of the
            ' target function:
            f = AddressOf Special.BesselJ0
            solver.TargetFunction = f
            solver.DerivativeOfTargetFunction = _
                FunctionMath.GetNumericalDifferentiator(f)
            solver.InitialGuess = 5
            Console.WriteLine("Zero of Bessel function near x=5:")
            result = solver.Solve()
            Console.WriteLine("  Result: {0}", solver.Status)
            Console.WriteLine("  Solution: {0}", solver.Result)
            Console.WriteLine("  Estimated error: {0}", solver.EstimatedError)
            Console.WriteLine("  # iterations: {0}", solver.IterationsNeeded)

            '
            ' Controlling the process
            '
            Console.WriteLine("Same with modified parameters:")
            ' You can set the maximum # of iterations:
            ' If the solution cannot be found in time, the
            ' Status will return a value of
            ' IterationStatus.IterationLimitExceeded
            solver.MaxIterations = 10
            ' You can specify how convergence is to be tested
            ' through the ConvergenceCriterion property:
            solver.ConvergenceCriterion = _
                ConvergenceCriterion.WithinRelativeTolerance
            ' And, of course, you can set the absolute or
            ' relative tolerance.
            solver.RelativeTolerance = 0.00000000000001
            ' In this example, the absolute tolerance will be 
            ' ignored.
            solver.AbsoluteTolerance = 0.0001
            solver.InitialGuess = 5
            result = solver.Solve()
            Console.WriteLine("  Result: {0}", solver.Status)
            Console.WriteLine("  Solution: {0}", solver.Result)
            ' The estimated error will be less than 5e-14
            Console.WriteLine("  Estimated error: {0}", solver.EstimatedError)
            Console.WriteLine("  # iterations: {0}", solver.IterationsNeeded)

            Console.Write("Press Enter key to exit...")
            Console.ReadLine()
        End Sub

    End Module

End Namespace

Related Solutions

Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation.
Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation. y''+2y'+5y=3sin(2t)
Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation.
Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation. y''+4y=3csc2t   
Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation.
Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation. y''+2y'+y=2e^t
Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation.
Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation.
Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation.
Use Method of Undetermined Coefficients te find a particular solution of the non-homogeneous equation. Find general solution of the non-homogeneous equation. y''+2y'+y=2e^{-t}
QUESTION 1: Solve the linear programming model given below using the simplex method. Write the primal...
QUESTION 1: Solve the linear programming model given below using the simplex method. Write the primal and dual results from the optimal table you obtained. MAX Z = 10?1 + 20?2 + 5?3 6?1 + 7?2 + 12?3 ≥ 560 5?1 − 3?2 +   x3 ≤ 100 2000?1 + 1000?2 + 1000?3 ≤ 62298 ?1,?2,?3 ≥ 0 IMPORTANT REMINDER ABOUT THE QUESTION SOLUTION: NEW ORDER CALCULATIONS SHOULD BE WRITTEN DETAILED WHEN CREATING THE SYMPLEX TABLES. WHEN THE CALCULATIONS ARE SHOWED AND...
What is the equation for the weighted least square solution for a non-linear problem? Which two...
What is the equation for the weighted least square solution for a non-linear problem? Which two conditions are satisfied when using the method of least squares?
Classify the following system with output current given (by the equation below) as linear/non linear i0(t)=...
Classify the following system with output current given (by the equation below) as linear/non linear i0(t)= 5 i1(t) + 8 i2 (t-10) + 0.25 i3 (t+2) Is this linear or non linear
Write a MATLAB code for the conjugate gradient method and apply it to solve the system...
Write a MATLAB code for the conjugate gradient method and apply it to solve the system Hx = b, where H is the n×n Hilbert matrix, and b is A times the vector of all ones, for (a) n = 4; (b) n = 8. Compare your numerical solutions with the exact solution (which is the vector of all ones), and report your numerical errors.
Write the second order differential equation as a system of two linear differential equations then solve...
Write the second order differential equation as a system of two linear differential equations then solve it. y" + y' - 6y = e^-3t y(0) =0   y'(0)=0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT