You are currently viewing Solution of Nonlinear Equations Using C Programming – Numerical Method

Solution of Nonlinear Equations Using C Programming – Numerical Method

Spread the love

Nonlinear Equations Using C Programming

 

Solution of Nonlinear Equations Using C Programming - Numerical Method

 

1.     Bisection Method

This method is known as the Bolzano method, bracketing method, binary chopping method, or half interval method.
Suppose we are given the continuous function f(x) in the interval [p, q] and we want to find the root of the equation f(x)=0 by a bisection method. The process is described as follows:-
1)      Find two points a and b such that f(a) * f(b) < 0. That is finding a and b so that f(a) and f(b) are of opposite sign. This process is called finding the initial root.
2)      Compute the middle point c using relation c= (a+b)/2. If f(c) = 0 then ‘c’ is the required root & stop the process if f (c) 0 then go to next step.
3)      If f(a) * f (c) <0 then root lies between a & c otherwise the root lies between c & b.
4)      Repeat steps 2 & 3 until the root is found to be desired of accuracy.
#include<stdio.h>
#include<conio.h>
float f(float x){
            return (x*x*x-4*x-9);
            }
void main(){
            int i,n;
            float a,b,c;
            clrscr();           
            printf(“Enter the no of iteration:”);
            scanf(“%d”,&n);
            printf(“enter two values where the root lies:”);
            scanf(“%f %f”,&a,&b);
            if(f(a)*f(b)>0)
              {
                 printf(“the initial values are out of range”);
                 getch();
                  exit(0);
              }
            else{
                        for(i=1;i<=n;i++){
                        c=(a+b)/2;
                        if(f(c)==0)
                                     break;
                        if(f(a)*f(c)<0)
                                    b=c;
                        else
                                    a=c;
            }
            printf(“the reqd. root is:%f”,c);
            getch();
            }
}



 2.     False Position Method

Suppose we are given the continuous function f(x) in the interval [p, q] and we want to find root of the equation f(x)=0 by false position method. The process is described as follows:-

1)      Find two points a and b such that f(a) * f(b) < 0.
2)      Compute c= (af(b)-bf(a))/(f(b)-f(a)).
3)      If f(c) = 0 then ‘c’ is the required root & stop the process if f (c) 0 then go to next step.
4)      If f(a) * f (c) <0 then root lies between a & c otherwise the root lies between c & b.
5)      Repeat step (2), (3) and (4) until the root is found to be desired of accuracy.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x){
  return (x*log10(x)-1.2);
}
void main(){
int i,n;
float a,b,c;
clrscr();
printf(“Enter the no of iteration:”);
scanf(“%d”,&n);
printf(“enter two values where the root lies:”);
scanf(“%f %f”,&a,&b);
if(f(a)*f(b)>0)
{
printf(“the initial values are out of range”);
getch();
exit(0);
}
else{
for(i=1;i<=n;i++){
c=(a*f(b)-b*f(a))/(f(b)-f(a));
if(f(c)==0)
 break;
if(f(a)*f(c)<0)
   b=c;
 else
   a=c;
}
printf(“the reqd. root is:%f”,c);
getch();
}
}

 

3.     Newton-Raphson Method

  • Also known as successive substitution method.

 

Algorithm
1.      Assign an initial value to x say x0.
2.      Evaluate f (x0) and f’(x0)
3.      Compute x1 =x0 –f(x0)/f(x0)
4.      If f(x1) =0 then x1, is the required root & stop the process.
5.      If f(x1) +0 then replace x0 by x1 & repeat the process till the root is found to the desired degree of accuracy.
            Let xo be the first approximation to the root of the equation f(x)=0. Let x1=x0 + h be the better approximation which approximately satisfy the equation f(x) = 0
Therefore,
            f(x0+h)= 0 approximately
            f(x0)+h f(x0)+h2/2! f”(x0)+h/3! f’”(x0) +….. =0
Assumption since h is very small, neglecting its square and higher power, we get,
f(x0) +h f’(x0)=0
 h= – f(x0)/ f’(x0)

 

Therefore ,
x1=x0-f(x0)/f’(x0)
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.0001
float f(float x){
      return (x*x*x-6*x+4);
}
float f1(float x){
      return (3*x*x-6);
}
void main(){
float x0,x1;
clrscr();
printf(“enter initial value:”);
scanf(“%f”,&x0);
do{
x1=x0-(f(x0)/f1(x0));
if(f(x1)==0)
 break;
else
 x0=x1;
}while(fabs((x1-x0)/x1)>e);
printf(“the reqd. root is:%f”,x1);
getch();
}

4.     Fixed Point Iteration Method

This method is also known as a direct substitution method or method of iteration or method of fixed iteration.
It is applicable if the equation f(x) = 0 can be expressed as x=g(x).
If x0 id the initial approximation to the root, then the next approximation to the root is given by,
       x1=g(x0)
and the next iteration will be
       x2= g(x1)
In general,
      xn+1=g(xn)
This method converges if |g(x0)|<1
#include<stdio.h>
#include<conio.h>
#include<math.h>
float g(float x){
return (sqrt(2*x+8));
}
void main(){
int i,n;
float x0,x1;
clrscr();
printf(“Enter the no of interation:”);
scanf(“%d”,&n);
printf(“Enter the initial value:”);
scanf(“%f”,&x0);
for (i=0;i<n;i++){
x1=g(x0);
x0=x1;
}
printf(“the reqd. root is:%f”,x1);
getch();
}

5.     Secant Method

Suppose we are given the continuous function f(x) in the interval [ p,q] & we want to find the root of the equation f(x)=0 by secant method. The process is described as follows:-
1.   Find two members a & b.
2.   Complete c= (af(b) – bf (a))/ (f(b) – f(a))
3.   If f(c) =0 then c is the required root & step the process.
4.   If f(c) != 0 then set a=b, f (a) =f(b) & b=c , f(b) = f(c)
5.   Repeat the steps (2), (3) & (4) until the root is found to the desired degree of accuracy.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x){
         return (x*x*x-6*x+4);
}
void main(){
int i,n;
float a,b,c;
clrscr();
printf(“enter two values where the root lies:”);
scanf(“%f %f”,&a,&b);
printf(“Enter the no of iteration:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++){
c=(a*f(b)-b*f(a))/(f(b)-f(a));
if(f(c)==0)
break;
   a=b;
   b=c;
 }
printf(“the reqd. root is:%f”,c);
getch();
}

6.     Horner’s Rule

Let us consider the evaluation of a polynomial using Horner’s rule as follows:-
f(x)= ((…((anx+an-1)x+an-2)x+…+a1)x+a0)….(1)
Here,
The innermost expression (anx+an-1) is evaluated first. The resulting value constitutes a multiplicand for the expression at the next level. The number of level equals n, the degree of polynomial.
Horner’s rule, also known as nested multiplication, is implemented using following algorithm.
The quantities pn, pn-1,…, p0 are evaluated recursively. The final quantity p0 gives the value of the function f(x).   
pn= an
pn-1= pnx + an-1
.
.
p1= p2x+a1
f(x)= p0=p1x+a0
#include<stdio.h>
#include<conio.h>
void main(){
int i,x,n,p,a[10];
clrscr();
printf(“enter the degree of polynomial:”);
scanf(“%d”,&n);
printf(“enter the cofficientn”);
for(i=n;i>=0;i–){
printf(“a[%d]:”,i);
scanf(“%d”,&a[i]);
}
printf(“enter the point of evaluation value:”);
scanf(“%d”,&x);
p=a[n];
for(i=n-1;i>=0;i–){

 

p=p*x+a[i];
}
printf(“The required value is:%d”,p);
getch();
}

 


Spread the love

Santosh Adhikari

Hello, it's me Santosh Adhikari, from Kathmandu, Nepal. I'm a student of Science and Technology. I love new challenges and want to explore more on Software Quality Assurance. I want to be a professional SQA. I'm also interested in Blogging, SEO activities.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments