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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Roots of a function step by step in the Liberty BASIC 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 Liberty BASIC programming language

Source code in the liberty programming language

'   Finds and output the roots of a given function f(x),
'       within a range of x values.

'   [RC]Roots of an function

    mainwin 80 12

    xMin  =-1
    xMax  = 3
    y     =f( xMin) '   Since Liberty BASIC has an 'eval(' function the fn
    '                       and limits would be better entered via 'input'.
    LastY =y

    eps  =1E-12 '   closeness acceptable

    bigH=0.01

    print
    print " Checking for roots of x^3 -3 *x^2 +2 *x =0 over range -1 to +3"
    print

    x=xMin: dx = bigH
    do
        x=x+dx
        y = f(x)
        'print x, dx, y
        if y*LastY <0 then 'there is a root, should drill deeper
            if dx < eps then    'we are close enough
                print " Just crossed axis, solution f( x) ="; y; " at x ="; using( "#.#####", x)
                LastY = y
                dx = bigH   'after closing on root, continue with big step
            else
                x=x-dx  'step back
                dx = dx/10  'repeat with smaller step
            end if
        end if
    loop while x

    print
    print " Finished checking in range specified."

    end

    function f( x)
        f =x^3 -3 *x^2 +2 *x
    end function

  

You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Flow-control structures step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the 11l programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Sidef programming language
You may also check:How to resolve the algorithm Dominoes step by step in the Perl programming language