Daily Archives: March 20, 2015

Pract: C Program for finding root of non-linear equation using Newton-Raphson Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<dos.h>
#define f(x) (x*x*x-4*x-9)
#define ff(x) (3*x*x-4)
void main()
{
float x0,x1,f1,f2,epsilon,delta,re;
int n,i;
clrscr();
printf(“\n\t\t Finding Root using Newton Rapson Method “);
printf(“\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n”);
printf(“\nf(x)=(x*x*x-4*x-9) = 0\n”);
printf(“Enter x0 : “);
scanf(“%f”,&x0);
printf(“Enter e : “);
scanf(“%f”,&epsilon);
printf(“Enter d : “);
scanf(“%f”,&delta);
printf(“Enter n : “);
scanf(“%d”,&n);
printf(“\n==========================================================”);
printf(“\niter’s        x0            f(x0)             ff(x0)         x1          re \n”);
printf(“===========================================================\n”);
for(i=1;i<=n;i++)
{
if(fabs(f2)<delta)
{
printf(“\n\nSLOPE OF THE FUNCTION BECOMES TOO SMALL………”);
exit(1);
}
f1 = f(x0);
f2 = ff(x0);
x1 = x0 – (f1 / f2);
re = fabs((x1-x0)/x1);
printf(“\n %d        %.4f          %.4f          %.4f         %.4f        %.4f\n”,i,x0,f1,f2,x1,re);
printf(“……………………………………………………………..”);

x0 = x1;
if(re<=epsilon)
{
printf(“\nRoot := %.4f”,x1);
}
}
printf(“\nSOLUTIONS DOES NOT CONVERGE IN %d ITERATIONS “,n);
getch();
}

Pract: C Program for finding root of non-linear equation using Secant Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<dos.h>

float f(float x) {
return ((x*x*x)-(4*x)-9);
}

void main()
{
float x1,x2,x3=0,x3p,e,f1,f2,f3;
int count = 0;
clrscr();
printf(“\n\t Finding Root using Secant Method “);
printf(“\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n”);
printf(“\nf(x)=(x*x*x)-(4*x)-9 = \n”);
printf(“Enter x1 : “);
scanf(“%f”,&x1);
printf(“Enter x2 : “);
scanf(“%f”,&x2);
f1=f(x1); //***********
f2=f(x2); //***********
if((f1*f2) > 0)
{
printf(“Starting values are not suitable “);
exit(1);
}
printf(“Enter e : “);
scanf(“%f”,&e);

printf(“\n==================ITERATIONS ========================\n”);
printf(“Itera’s    x1        x2         x3         f(x1)      f(x2)     f(x3) \n”);
printf(“======================================================”);

do
{
count++;
x3p = x3;
x3 = (x1*f2 – x2*f1) / (f2-f1);
f3 = f(x3);
printf(“\n%d | %.6f | %.6f | %.6f | %.6f | %.6f | %.6f “,count,x1,x2,x3,f(x1),f(x2),f(x3));
x1 = x2; 
f1 = f2;
x2 = x3; 
f2 = f3;
}while(fabs((float)((x3 – x3p)/x3)) > e);
printf(“\n===================================================\n”);
printf(“\t=*=*=*==*=*=*=*==*=*==*=*”);
printf(“\n\tRoot := %.4f”,x3);
printf(“\twith total iterations := %d”,count);
printf(“\n\t=*=*=*==*=*=*=*==*=*==*=*\n”);

getch();
}

Pract: C Program for finding root of non-linear equation using False Position (Regula Falsi) Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<dos.h>

float f(float x) {
return ((x*x*x)-(4*x)-9);
}

void main()
{
float x1,x2,x3=0,x3p,e,f1,f2,f3;
int count = 0;
clrscr();
printf(“\n\t Finding Root using False Position (Regula Falsi) Method “);
printf(“\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n”);
printf(“\nf(x)=(x*x*x)-(4*x)-9 = \n”);
printf(“Enter x1 : “);
scanf(“%f”,&x1);
printf(“Enter x2 : “);
scanf(“%f”,&x2);
 f1=f(x1);
f2=f(x2);
if((f1*f2) > 0)
{
printf(“Starting values are not suitable “);
exit(1);
}
printf(“Enter e : “);
scanf(“%f”,&e);

printf(“\n==================ITERATIONS ========================\n”);
printf(“Itera’s    x1        x2         x3         f(x1)      f(x2)     f(x3) \n”);
printf(“======================================================”);
do
{
count++;
//x3 = (x1+x2)/2;
 x3p = x3;
x3 = (x1*f2 – x2*f1) / (f2-f1);
 f3 = f(x3);
printf(“\n%d | %.6f | %.6f | %.6f | %.6f | %.6f | %.6f “,count,x1,x2,x3,f(x1),f(x2),f(x3));
if(f1*f3 < 0)
{ x2 = x3;   //Both values have opposite sign
f2 = f3;
}
else
{ x1 = x3;  //Both values have same sign
f1 = f3;
}
//        }while(fabs((float)(x1 – x2)) > e);
//        printf(“\nNo1: %f & No2: %f\n”,fabs((float)(x1 – x2)),fabs((float)((x3 – x3p)/x3)));
}while(fabs((float)((x3 – x3p)/x3)) > e);
printf(“\n===================================================\n”);
printf(“\t=*=*=*==*=*=*=*==*=*==*=*”);
printf(“\n\tRoot := %.4f”,x3);
printf(“\twith total iterations := %d”,count);
printf(“\n\t=*=*=*==*=*=*=*==*=*==*=*\n”);

getch();
}

Pract: C Program for finding root of non-linear equation using Bisection Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<dos.h>

float f(float x) {
return ((x*x*x)-(4*x)-9);
}

void main()
{
float x1,x2,x3,e;
int count = 0;
clrscr();
printf(“\n\t Finding Root using Bisection Method “);
printf(“\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n”);
printf(“\nf(x)=(x*x*x)-(4*x)-9 = \n”);
printf(“Enter x1 : “);
scanf(“%f”,&x1);
printf(“Enter x2 : “);
scanf(“%f”,&x2);

if((f(x1)*f(x2)) > 0)
{
printf(“Starting values are not suitable “);
exit(1);
}
printf(“Enter e : “);
scanf(“%f”,&e);

printf(“\n==================ITERATIONS ========================\n”);
printf(“Itera’s    x1        x2         x3         f(x1)      f(x2)     f(x3) \n”);
printf(“======================================================”);
do
{
count++;
x3 = (x1+x2)/2;
printf(“\n%d | %.6f | %.6f | %.6f | %.6f | %.6f | %.6f “,count,x1,x2,x3,f(x1),f(x2),f(x3));
if((f(x1)*f(x3)) < 0)
x2 = x3;   //Both values have opposite sign
else
x1 = x3;  //Both values have same sign
}while(fabs((float)(x1 – x2)) > e);
printf(“\n===================================================\n”);
printf(“\t=*=*=*==*=*=*=*==*=*==*=*”);
printf(“\n\tRoot := %.4f”,x3);
printf(“\twith total iterations := %d”,count);
printf(“\n\t=*=*=*==*=*=*=*==*=*==*=*\n”);

getch();
}