Bisection Method Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float function(float x)
{
float c;
c=((x*x)-(4*x)+3);
return c;
}
float middle(float a,float b)
{
float d;
d=((a+b)/2);
return d;
}
float error(float x1,float x2)
{
float value;
value=(fabs(((x2-x1)/x2)*100));
return value;
}
void main()
{
clrscr();
float x1=0,x2=0,f1=0,f2=0,E,x0=0,f0=0,p,q,root;
int i;
printf("ENTER 1st VALUE:");
scanf("%f",&x1);
printf("\nENTER 2nd VALUE:");
scanf("%f",&x2);
f1=function(x1);
printf("\n\nf1=%.4f",f1);
f2=function(x2);
printf("\n\nf2=%.4f",f2);
if(f1*f2==0)
{
if(f1==0)
{
printf("\n\nROOT IS X1=%.4f",x1);
}
else
{
printf("\n\nROOT IS X2=%.4f",x2);
}
}
else if(f1*f2<0)
{
for(i=0;i<=6;i++)
{
printf("\nx1=%.3f",x1);
printf("\ x2=%.3f",x2);
f1=function(x1);
printf("\nf(x1)=%.3f",f1);
f2=function(x2);
printf(" f(x2)=%.3f",f2);
E=error(x1,x2);
printf("\nERROR=%.2f",E);
x0=middle(x1,x2);
printf("\nX0=%.4f",x0);
f0=function(x0);
printf(" f(x0)=%.4f",f0);
p=(f1*f0);
q=(f2*f0);
if(p<0&&q>0)
{
x2=x0;
printf("\n--------------------------------------\n");
}
else
{
x1=x0;
printf("\n--------------------------------------\n");
}
}
}
else
{
printf ("\n\nANSWER DOESNOT EXISTS BETWEEN THIS LIMIT");
}
getch();
}
False Position Method:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float function(float x)
{
float c;
c=((x*x)-(4*x)+3);
return c;
}
float middle(float a,float b,float g,float h)
{
float d;
d=(a+(g*(b-a)/(g-h)));
return d;
}
float error(float x1,float x2)
{
float value;
value=(fabs((x2-x1)/x2));
return value;
}
void main()
{
clrscr();
float x1=0,x2=0,f1=0,f2=0,E,x0=0,f0=0,p,q,root;
int i;
printf("ENTER 1st VALUE:");
scanf("%f",&x1);
printf("\nENTER 2nd VALUE:");
scanf("%f",&x2);
f1=function(x1);
printf("\n\nf1=%.4f",f1);
f2=function(x2);
printf("\n\nf2=%.4f",f2);
if(f1*f2==0)
{
if(f1==0)
{
printf("\n\nROOT IS X1=%.4f",x1);
}
else
{
printf("\n\nROOT IS X2=%.4f",x2);
}
}
else if(f1*f2<0)
{
for(i=0;i<=6;i++)
{ printf("\nx1=%.3f",x1);
printf("\ x2=%.3f",x2);
f1=function(x1);
printf("\nf(x1)=%.3f",f1);
f2=function(x2);
printf(" f(x2)=%.3f",f2);
E=error(x1,x2);
printf("\nERROR=%.2f",E);
x0=middle(x1,x2,f1,f2);
printf("\nX0=%.4f",x0);
f0=function(x0);
printf(" f(x0)=%.4f",f0);
p=(f1*f0);
q=(f2*f0);
if(p<0&&q>0)
{
x2=x0;
printf("\n--------------------------------------\n");
}
else
{
x1=x0;
printf("\n--------------------------------------\n");
}
}
}
else { printf ("\n\nANSWER DOESNOT EXISTS BETWEEN THIS LIMIT"); }
getch();
}
HUNS Method Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float function(float x,float y)
{
float c;
c=((2*y)/x);
return c;
}
float euler(float y0,float m, float h )
{
float d;
d=(y0+(h*m));
return d;
}
float next(float y0,float h, float m, float n)
{
float e;
e=(y0+((h*(m+n))/2));
return e;
}
int iteration(float a, float b, float h)
{
float n;
n=((a-b)/h);
return n;
}
void main()
{
clrscr();
float x0=0,y0=0,x1=0,yeuler=0,yreal=0,h=0,m1=0,m2=0,n=0,xfinal=0;
int i;
printf("ENTER 1st VALUE OF X:");
scanf("%f",&x0);
printf("\nENTER 2nd VALUE OF Y:");
scanf("%f",&y0);
printf("\nENTER STEP SIZE H:");
scanf("%f",&h);
printf("\nWHICH VALUE OF X YOU WANT TO REACH:");
scanf("%f",&xfinal);
n=iteration(xfinal,x0,h);
for(i=1;i<=n;i++)
{
m1=function(x0,y0);
printf("\n\nm1=%.4f",m1);
x1=x0+h;
yeuler=euler(y0,m1,h);
printf("\n\ny(%.3f)=%.4f",x1,yeuler);
m2=function(x1,yeuler);
printf("\n\nm2=%.4f",m2);
yreal=next(y0,h,m1,m2);
printf("\n\ny(%.3f)=%.4f",x1,yreal);
x0=x1;
y0=yreal;
printf("\n-----------------------------------\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
float function(float x)
{
float c;
c=((x*x)-(4*x)+3);
return c;
}
float middle(float a,float b)
{
float d;
d=((a+b)/2);
return d;
}
float error(float x1,float x2)
{
float value;
value=(fabs(((x2-x1)/x2)*100));
return value;
}
void main()
{
clrscr();
float x1=0,x2=0,f1=0,f2=0,E,x0=0,f0=0,p,q,root;
int i;
printf("ENTER 1st VALUE:");
scanf("%f",&x1);
printf("\nENTER 2nd VALUE:");
scanf("%f",&x2);
f1=function(x1);
printf("\n\nf1=%.4f",f1);
f2=function(x2);
printf("\n\nf2=%.4f",f2);
if(f1*f2==0)
{
if(f1==0)
{
printf("\n\nROOT IS X1=%.4f",x1);
}
else
{
printf("\n\nROOT IS X2=%.4f",x2);
}
}
else if(f1*f2<0)
{
for(i=0;i<=6;i++)
{
printf("\nx1=%.3f",x1);
printf("\ x2=%.3f",x2);
f1=function(x1);
printf("\nf(x1)=%.3f",f1);
f2=function(x2);
printf(" f(x2)=%.3f",f2);
E=error(x1,x2);
printf("\nERROR=%.2f",E);
x0=middle(x1,x2);
printf("\nX0=%.4f",x0);
f0=function(x0);
printf(" f(x0)=%.4f",f0);
p=(f1*f0);
q=(f2*f0);
if(p<0&&q>0)
{
x2=x0;
printf("\n--------------------------------------\n");
}
else
{
x1=x0;
printf("\n--------------------------------------\n");
}
}
}
else
{
printf ("\n\nANSWER DOESNOT EXISTS BETWEEN THIS LIMIT");
}
getch();
}
False Position Method:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float function(float x)
{
float c;
c=((x*x)-(4*x)+3);
return c;
}
float middle(float a,float b,float g,float h)
{
float d;
d=(a+(g*(b-a)/(g-h)));
return d;
}
float error(float x1,float x2)
{
float value;
value=(fabs((x2-x1)/x2));
return value;
}
void main()
{
clrscr();
float x1=0,x2=0,f1=0,f2=0,E,x0=0,f0=0,p,q,root;
int i;
printf("ENTER 1st VALUE:");
scanf("%f",&x1);
printf("\nENTER 2nd VALUE:");
scanf("%f",&x2);
f1=function(x1);
printf("\n\nf1=%.4f",f1);
f2=function(x2);
printf("\n\nf2=%.4f",f2);
if(f1*f2==0)
{
if(f1==0)
{
printf("\n\nROOT IS X1=%.4f",x1);
}
else
{
printf("\n\nROOT IS X2=%.4f",x2);
}
}
else if(f1*f2<0)
{
for(i=0;i<=6;i++)
{ printf("\nx1=%.3f",x1);
printf("\ x2=%.3f",x2);
f1=function(x1);
printf("\nf(x1)=%.3f",f1);
f2=function(x2);
printf(" f(x2)=%.3f",f2);
E=error(x1,x2);
printf("\nERROR=%.2f",E);
x0=middle(x1,x2,f1,f2);
printf("\nX0=%.4f",x0);
f0=function(x0);
printf(" f(x0)=%.4f",f0);
p=(f1*f0);
q=(f2*f0);
if(p<0&&q>0)
{
x2=x0;
printf("\n--------------------------------------\n");
}
else
{
x1=x0;
printf("\n--------------------------------------\n");
}
}
}
else { printf ("\n\nANSWER DOESNOT EXISTS BETWEEN THIS LIMIT"); }
getch();
}
HUNS Method Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float function(float x,float y)
{
float c;
c=((2*y)/x);
return c;
}
float euler(float y0,float m, float h )
{
float d;
d=(y0+(h*m));
return d;
}
float next(float y0,float h, float m, float n)
{
float e;
e=(y0+((h*(m+n))/2));
return e;
}
int iteration(float a, float b, float h)
{
float n;
n=((a-b)/h);
return n;
}
void main()
{
clrscr();
float x0=0,y0=0,x1=0,yeuler=0,yreal=0,h=0,m1=0,m2=0,n=0,xfinal=0;
int i;
printf("ENTER 1st VALUE OF X:");
scanf("%f",&x0);
printf("\nENTER 2nd VALUE OF Y:");
scanf("%f",&y0);
printf("\nENTER STEP SIZE H:");
scanf("%f",&h);
printf("\nWHICH VALUE OF X YOU WANT TO REACH:");
scanf("%f",&xfinal);
n=iteration(xfinal,x0,h);
for(i=1;i<=n;i++)
{
m1=function(x0,y0);
printf("\n\nm1=%.4f",m1);
x1=x0+h;
yeuler=euler(y0,m1,h);
printf("\n\ny(%.3f)=%.4f",x1,yeuler);
m2=function(x1,yeuler);
printf("\n\nm2=%.4f",m2);
yreal=next(y0,h,m1,m2);
printf("\n\ny(%.3f)=%.4f",x1,yreal);
x0=x1;
y0=yreal;
printf("\n-----------------------------------\n");
}
getch();
}
Very informative post. Keep up the good work. I would really look forward to your other posts
ReplyDeletePanasonic - 13.1" Toughbook Notebook - 4 GB Memory - 500 GB Hard Drive
Panasonic - 15.4" Toughbook Notebook - 4 GB Memory and 256 GB Solid State Drive