How to resolve the algorithm Roots of a function step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Roots of a function step by step in the Julia programming language

Table of Contents

Problem Statement

Create a program that finds and outputs the roots of a given function, range and (if applicable) step width.
The program should identify whether the root is exact or approximate.

For this task, use:     ƒ(x)   =   x3 - 3x2 + 2x

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Roots of a function step by step in the Julia programming language

The provided code snippet is a Julia implementation of the Newton-Raphson method for finding the root of a function. The function takes three arguments: a function f to find the root of, the derivative of f, and an initial guess for the root.

The main function, newton, iteratively updates the guess for the root using the formula:

x = x - f(x) / fp(x)

where fp is the derivative of f. The iteration stops when either the maximum number of steps has been reached, or the change in the guess is less than a specified tolerance.

The code also includes a function, f, which defines the function to find the root of, and a function, fp, which defines the derivative of f.

To use the code, you can call the newton function with the function to find the root of and the derivative of that function. For example, to find the root of the function x^3 - 3*x^2 + 2*x, you would call:

newton(x -> x^3 - 3*x^2 + 2*x, x -> 3*x^2 - 6*x + 2, 1.00)

This would return the root of the function, which is approximately 1.

Source code in the julia programming language

using Roots

println(find_zero(x -> x^3 - 3x^2 + 2x, (-100, 100)))


function newton(f, fp, x::Float64,tol=1e-14::Float64,maxsteps=100::Int64)
         ##f: the function of x
         ##fp: the derivative of f
 
	 local xnew, xold = x, Inf
	 local fn, fo = f(xnew), Inf
	 local counter = 1
 
	 while (counter < maxsteps) && (abs(xnew - xold) > tol) && ( abs(fn - fo) > tol )
	   x = xnew - f(xnew)/fp(xnew) ## update x
	   xnew, xold = x, xnew
           fn, fo = f(xnew), fn
	   counter += 1
	 end
 
	 if counter >= maxsteps
	    error("Did not converge in ", string(maxsteps), " steps")
         else
	   xnew, counter
         end 
end


f(x) = x^3 - 3*x^2 + 2*x
fp(x) = 3*x^2-6*x+2

x_s, count = newton(f,fp,1.00)


  

You may also check:How to resolve the algorithm Multiple distinct objects step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Haskell programming language
You may also check:How to resolve the algorithm 21 game step by step in the 11l programming language
You may also check:How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the D programming language
You may also check:How to resolve the algorithm Inverted syntax step by step in the Raku programming language