Scilab Program for Newton Raphson Method to Find Roots

Problem:

Write a Scilab program for Newton Raphson Method.

Algorithm or Solution:

In numerical analysis, Newton's method which is also known as Newton Raphson method is used to find the roots of given function/equation. This method is named after Sir Isaac Newton and Joseph Raphson. Get more information about Derivation of Newton Raphson formula. Here is algorithm or the logical solution of Scilab program for Newton Raphson Method
  1. Start
  2. Define a function f(x)=0 as required using deff keyword in scilab.
  3. Define the derivative function f'(x)=0 using deff keyword in scilab.
  4. If two points are given in which the root lies then calculate the average of both, if they are not given then choose a suitable starting value according to the function.
  5. Initialize the counter variable i=1.
  6. Use any loop (for loop, while loop) according to your convenience, if you are using for loop there is no need to initialize 'i' earlier.
  7. Use the formula of Newton Raphson method inside the loop. We are using x to store the current value as well as next approximation as we are using loop.
  8. Loop up to desired iterations and stop.
  9. Print the value of root i.e value of 'x'.
  10. Stop.
Newton Raphson formula is
Xn+1=Xn-
f(Xn) f'(Xn)
to implement scilab program for Newton Raphson method.

Scilab Program / Source Code:

Here is the source code of Scilab program for Newton Raphson Method.
// Scilab code for newton raphson methos to find the roots of given equation

deff('y=f(x)','y=x^4-x-10');  // Defining the function f(x)=0
deff('z=f1(x)','z=4*x^3-1');  // Defining the function f'(x)=0
a=1;b=2;
x=(a+b)/2;  // Calculate average if values in between the root lies are given
i=1; // Initialize counter variable i=1
while(i<=15)  // Loop up to desired number of iterations
    x=(x-(f(x)/f1(x)));  // use the Newton Raphson Formula
    i=i+1;
    [x]  // Print value of root every time to see the difference
end;