December 2018

C program to find value using Bisection Method | C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define EST 0.05
#define F(x) pow(x,3)-x-3
int main()
{
int i=1;
float xl,xu,xm,f1,f2,f3,error;
printf("Enter the values for xl and xu respectively\n");
scanf("%f%f",&xl,&xu);
printf("Iteration\tXl\t Xu\t f(Xm)\t Error\n");
do
{
xm=(xl+xu)/2;
f1=F(xl);
f2=F(xu);
f3=F(xm);
error=fabs((xl-xu)/xu);
if((f1*f3)<0)
{
xu=xm;
}
else
{
xl=xm;
}
f3=F(xm);
error=fabs((xl-xu)/xu);
printf("%d\t %.4f\t %.4f\t%.4f\t %.4f\n",i,xl,xu,xm,error);
i++;
}while (error>=EST);

}

C program to find value using secant method | C programming


#include<stdio.h>
#include <math.h>
#include<conio.h>
#define EST 0.05
#define F(x) x-exp(x)+2
int main()
{
int i = 1;
float x0,x1,a,b,c,d,f1,x2,f0,error;
printf("\nEnter the value of x0: ");
scanf("%f",&x0);
printf("\nEnter the value of x1: ");
scanf("%f",&x1);
printf("\n__________________________________________________________________\n");
printf("\niteration\tx0\t x1\t f0\t f1\t x2\t\terror");
printf("\n___________________________________________________________________\n");
f0=F(x0);
f1=F(x1);
x2=x1-((f1*(x1-x0))/(f1-f0));
printf("%f",x2);
error=fabs((x2-x1)/x2);
printf("\n %d \t %.2f\t %.2f\t %.2f \t %.3f \t %.3f \t %.3f", i, x0,x1,f0,f1,x2,error);
do{
i++;
x0=x1;
f0= f1;
x1=x2;

f0=F(x0);
f1=F(x1);
x2=x1-((f1*(x1-x0))/(f1-f0));
error=fabs((x2-x1)/x2);
printf("\n %d \t %.2f\t %.2f\t %.2f \t %.4f \t %.4f \t %.4f", i, x0,x1,f0,f1,x2,error);
}while(error>EST);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",x2);
return 0;
}

C program to find the value using Newton Raphson

Before starting with the program let us learn something about Newton Raphson Method

Newton Raphson Method is open method and starts with one initial guess for finding real root of non-linear equations. In other Words, Newton Raphson method, also called the Newton’s method, is the fastest and simplest approach of all methods to find the real root of a nonlinear function. It is an open bracket approach, requiring only one initial guess. This method is quite often used to improve the results obtained from other iterative approaches




C program to find the value using Newton Raphson

#include<stdio.h>
#include <math.h>
#include<conio.h>
#define EST 0.05
#define F(x) 4*(x)*(x)*(x) - 2*(x) - 6
#define F1(x) 12*(x)*(x) - 2
int main()
{
int i = 1;
float xl;
double f1,fm,f0,error;
printf("\nEnter the value of x0: "); scanf("%f",&xl);
printf("\n__________________________________________________________________\n");
printf("\niteration\tx0\tf0\t f1\t fm\t error");
printf("\n___________________________________________________________________\n");
do
{
f0=F(xl); f1=F1(xl); fm=xl-(f0/f1);
error=(fabs((fm-xl)/fm));
printf("\n %d \t%.2lf\t %.2lf\t %.2lf\t %.4lf \t %.4lf", i,xl,f0,f1,fm,error);
if(error>EST)
{
 xl=fm; f0=F(xl); f1=F1(xl); fm=xl-(f0/f1);
error=(fabs((fm-xl)/fm));
i++;
printf("\n %d \t%.2lf\t %.2lf\t %.2lf\t %.4lf \t %.4lf", i,xl,f0,f1,fm,error);
}
else
{
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",fm);
}
}while(error>EST);
return 0;
}