Saturday

Newton-Raphson Method in C++

The Newton Raphson Method is an open method used to find the roots of a function.
It employs the technique of linear approximation and involves using the tangent line to approximate the roots.
The method is highly efficient and converges rapidly, providing robust solutions in fewer iterations.

File content:

{{{{{

#include <iostream>

#include <cmath>

using namespace std;


const double EPS = 0.000001;


double function(double x0)

{

    // x0^2 - 3*x0 + 2 = (x0 - 1)(x0 - 2)

    return x0*x0 - 3*x0 + 2;

}


double derivative_of_the_function(double x0)

{

    return 2*x0 - 3;

}


int main()

{

    int max;

    int i = 0;

    double x1, x0;


    cout << "Newton-Raphson Method in C++" << endl;

    cout << "Maximum number of iterations: ";

    max = 100;

    cout << max << endl;

    cout << "The first approximation: ";

    x0 = 0; // you get the root 1

    //x0 = 3; // you get the root 2

    cout << x0 << endl;


    bool flagStopLoop = false;

    do

    {

        x1 = x0 - (function(x0) / derivative_of_the_function(x0));

        i++;


        if( fabs(x1 - x0) <= EPS )

        {

            flagStopLoop = true;

        }

        else if( i > max )

        {

            flagStopLoop = true;

        }

        else

        {

            x0 = x1;

        }

    } while( !flagStopLoop );

    cout << "The root of the function after " << i << " iteration is "

         << x1 << endl;


    return 0;

}

}}}}}

Change Codeblocks line number background and foreground back to its default colors

If background and foreground of line numbers did not change back to its default colors, go to 

Menu > Settings > Environment > Colors

Category > select: Editor

Click the “Default rectangle color at the right panel” for each of the following. 

* editor-caret

* line number background color

* line number foreground number 

* margin chrome color

* margin chrome highlight color

Click the button OK to close the dialog window.

Measure execution time with Julia, example using sorting algorithms

# random integers between 1 and 100 inclusive, generate thousands of them

x = rand(1:100, 100000)


@time sort(x; alg=InsertionSort, rev=false)

# 1.091568 seconds (35.79 k allocations: 1.899 MiB, 2.55% compilation time)


@time sort(x; alg=QuickSort, rev=false)

# 0.099377 seconds (107.61 k allocations: 5.397 MiB, 98.81% compilation time)

Polymorphism with Julia using functions, example using type Shape

Program: 

abstract type Shape end

# poymorhpic function computation

function area(shape::Shape)

    throw(MethodError("area() is not implemented for abstract type Shape"))

end


struct Circle <: Shape

    radius::Real

end

function area(c::Circle)

    return 3 * c.radius ^ 2

end


struct Rectangle <: Shape

    width::Real

    height::Real

end

function area(r::Rectangle)

    return r.width * r.height

end


struct Square <: Shape

    width::Real

end

function area(s::Square)

    return s.width * s.width

end


function main()

    shapes = Shape[Circle(1), Square(2), Rectangle(3, 4)]

   

    println("Area Circle(1) = ", area(shapes[1]))

    println("Area Square(2) = ", area(shapes[2]))

    println("Area Rectangle(3, 4) = ", area(shapes[3]))

end


main()


Output:

Area Circle(1) = 3

Area Square(2) = 4   

Area Rectangle(3, 4) = 12

Newton-Raphson Method in C++

The Newton Raphson Method is an open method used to find the roots of a function. It employs the technique of linear approximation and invol...