You are on page 1of 7

1

BISECTION METHOD
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define f(x) ((x*x*x)-18)
int main(){
float a=0,b=0,error=0,m,mold;
int i=0;
printf("Input Interval: ");
scanf("%f %f",&a,&b);
if((f(a)*f(b))>0){
printf("Invalid Interval Exit!"); //to test whether search interval
exit(1); //is okay or not
}
else if(f(a)==0 || f(b)==0){
printf("Root is one of interval bounds. Root is %f\n",f(a)==0?a:b);
exit(0);
}
printf("Ite\ta\t\tb\t\tm\t\tf(m)\t\terror\n");
do{
mold=m;
m=(a+b)/2;
printf("%2d\t%4.6f\t%4.6f\t%4.6f\t%4.6f\t",i++,a,b,m,f(m));
if(f(m)==0){
printf("Root is %4.6f\n",m);
}else if ((f(a)*f(m))<0){
b=m;
}else a=m;
error=fabs(m-mold);
if(i==1){
printf("----\n");
}else printf("%4.6f\n",error);
}while(error>0.00005);
printf("Approximate Root is %4.6f",m);
return 0;
}

Secant Method
#include<stdio.h>
#include<math.h>
#define f(x) (pow(x,3)-18)
int main(){
float x0,x1,x2,error;
int i=0;
printf("Input Two Approximations: ");
scanf("%f %f",&x0,&x1);
printf("Ite\tX0\t\tX1\t\tf(X0)\t\tf(X1)\t\tError\n");
do{
x2=((x0*f(x1))-((x1)*f(x0)))/((f(x1)-f(x0)));
printf("%2d\t%4.6f\t%4.6f\t%4.6f\t%4.6f\t%4.6f\n",i++,x0,x1,f(x0),f(x1),error);
error=fabs((x2)-(x1));
x0=x1;
x1=x2;
}while(error>0.00005);
2

return 0;
}

NEWTON RAPSON
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define f(x) ((x*x*x)-18)
#define fd(x) (3*x*x)
#define fdd(x) 6*x
int main(){
float x0,x1,error,errorold,converge,order;
int i=0;
printf("Input the approximation : ");
scanf("%f",&x0);
converge= (f(x0)*fdd(x0))/(fd(x0)*fd(x0));
if(converge >1)
exit(1);
printf("Ite\tX0\t\tX1\t\tError\t\tOrder\n");
do{
errorold=error;
x1=x0-(f(x0)/fd(x0));
if(f(x1)==0){
break;
}
error=fabs(x1-x0);
printf("%2d\t%4.6f\t%4.6f\t%4.6f\t",++i,x0,x1,error);
if(i==1||error==0||errorold==1){
printf("-----\n");
}
else {order=log(error)/log(errorold);
printf("%4.6f\n",order);
}
x0=x1;
}while(error>0.00005);
printf("Root is %4.6f",x0);
return 0;
}

REGULA FALSI METHOD


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define f(x) ((x*x*x)-18)
int main(){
float a=0,b=0,error=0,c,cold;
int i=0;
printf("Input Interval: ");
scanf("%f %f",&a,&b);
if((f(a)*f(b))>0){
printf("Invalid Interval Exit!");
exit(1);
}
3

else if(f(a)==0 || f(b)==0){


printf("Root is one of interval bounds. Root is %f\n",f(a)==0?a:b);
exit(0);
}
printf("Ite\ta\t\tb\t\tc\t\tf(c)\t\terror\n");
do{
cold=c;
c=(((a*f(b))-(b*f(a)))/(f(b)-f(a)));
printf("%2d\t%4.6f\t%4.6f\t%4.6f\t%4.6f\t",i++,a,b,c,f(c));
if(f(c)==0){
break;
}else if(f(a)*f(c)<0){
b=c;
}else a=c;
error=fabs(c-cold);
if(i==1){
printf("----\n");
}else printf("%4.6f\n",error);

}while(error>0.00005);
printf(" Root is %4.6f \n",c);
return 0;
}

INTEGRATION
TRAPEZOIDAL RULE
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h:\n");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\nrefined value of n and h are:%d %f\n",n,h);
printf("\n Y values \n");
4

for(i=0; i<=n; i++)


{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n%f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\nfinal integration is %f",ans);
getch();
}

SIMPSON METHOD
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(1/(1+x));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
5

printf("\n Refined value of n and h are:%d %f\n",n,h);


printf("\n Y values: \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n %f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}

}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\n Final integration is %f",ans);

getch();
}

LINEAR EQUATION SOLUTION


GAUSSIAN ELIMINATION
#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10],sum=0.0;
printf("\nEnter the order of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
6

printf("A[%d][%d] : ", i,j);


scanf("%f",&A[i][j]);
}
}
for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
{
for(i=1; i<=n; i++)
{
if(i>j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
x[n]=A[n][n+1]/A[n][n];
/* this loop is for backward substitution*/
for(i=n-1; i>=1; i--)
{
sum=0;
for(j=i+1; j<=n; j++)
{
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
}
printf("\nThe solution is: \n");
for(i=1; i<=n; i++)
{
printf("\nx%d=%f\t",i,x[i]); /* x1, x2, x3 are the required solutions*/
}
return(0);
}
GAUSSIAN JORDAN
#include<stdio.h>
2 int main()
3 {
4 int i,j,k,n;
5 float A[20][20],c,x[10];
6 printf("\nEnter the size of matrix: ");
7 scanf("%d",&n);
8 printf("\nEnter the elements of augmented matrix row-wise:\n");
7

9 for(i=1; i<=n; i++)


10 {
11 for(j=1; j<=(n+1); j++)
12 {
13 printf(" A[%d][%d]:", i,j);
14 scanf("%f",&A[i][j]);
15 }
16 }
17 /* Now finding the elements of diagonal matrix */
18 for(j=1; j<=n; j++)
19 {
20 for(i=1; i<=n; i++)
21 {
22 if(i!=j)
23 {
24 c=A[i][j]/A[j][j];
25 for(k=1; k<=n+1; k++)
26 {
27 A[i][k]=A[i][k]-c*A[j][k];
28 }
29 }
30 }
31 }
32 printf("\nThe solution is:\n");
33 for(i=1; i<=n; i++)
34 {
35 x[i]=A[i][n+1]/A[i][i];
36 printf("\n x%d=%f\n",i,x[i]);
37 }
38 return(0);
39 }

You might also like