MIDPOINT 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 derivative(float x)
{
float d;
d=((2*x)-4);
return d;
}
float point(float x0,float f0, float f1)
{
float x1;
x1=(x0-(f0/f1));
return x1;
}
float error(float x0,float x1)
{
float value;
value=(fabs((x0-x1)/x0)*100);
return value;
}
void main()
{
clrscr();
int i;
float x0=0,x1=0,f0=0,f1=0,e=0;
printf("ENTER INITIAL VALUE:");
scanf("%f",&x0);
f0=function(x0);
if(f0==0)
{
printf("root is =%.4f",x0);
}
else
{
for(i=0;i<=6;i++)
{
printf("x0=%.4f",x0);
f0=function(x0);
printf("\nf0=%.4f",f0);
f1=derivative(x0);
printf("\nf1=%.4f",f1);
x1=point(x0,f0,f1);
printf("\nx1=%.4f",x1);
e=error(x0,x1);
printf("\nERROR=%.4f",e);
x0=x1;
printf("\n---------------------------------------------\n");
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
float function(float x,float y)
{
float c;
c=((2*y)/x);
return c;
}
float yvalue(float y0,float f0,float h)
{
float d;
d=(y0+((h*f0)/2));
return d;
}
float xvalue(float x,float h)
{
float x1;
x1=(x+(h/2));
return x1;
}
int iteration(float a, float b, float h)
{
float n;
n=fabs((b-a)/h);
return n;
}
void main()
{
clrscr();
#include<conio.h>
#include<math.h>
float function(float x,float y)
{
float c;
c=((2*y)/x);
return c;
}
float yvalue(float y0,float f0,float h)
{
float d;
d=(y0+((h*f0)/2));
return d;
}
float xvalue(float x,float h)
{
float x1;
x1=(x+(h/2));
return x1;
}
int iteration(float a, float b, float h)
{
float n;
n=fabs((b-a)/h);
return n;
}
void main()
{
clrscr();
float x0=0,y0=0,x=0,f0=0,x1=0,rvalue=0,lvalue=0,fvalue=0,now=0,h=0,y1=0,i=0,n=0;
printf("ENTER 1st VALUE OF X:");
scanf("%f",&x0);
printf("\nENTER 1st VALUE OF Y:");
scanf("%f",&y0);
printf("\nWHICH VALUE OF X YOU WANT TO REACH:");
scanf("%f",&x);
printf("\nENTER STEP SIZE:");
scanf("%f",&h);
n=iteration(x,x0,h);
if(x>x0)
{
for(i=0;i<n;i++)
{
x1=x0+h;
f0=function(x0,y0);
rvalue=yvalue(y0,f0,h);
lvalue=xvalue(x0,h);
fvalue=function(lvalue,rvalue);
now=(fvalue*h);
y1=(y0+now);
printf("\n\nY(%.3f)=%.3f\n",x1,y1);
y0=y1;
x0=x1;
}
}
else
{
for(i=0;i<n;i++)
{
x1=x0-h;
f0=function(x0,y0);
rvalue=yvalue(y0,f0,h);
lvalue=xvalue(x0,h);
fvalue=function(lvalue,rvalue);
now=(fvalue*h);
y1=(y0+now);
printf("\n\nY(%.3f)=%.3f\n",x1,y1);
y0=y1;
x0=x1;
}
}
getch();
}
printf("ENTER 1st VALUE OF X:");
scanf("%f",&x0);
printf("\nENTER 1st VALUE OF Y:");
scanf("%f",&y0);
printf("\nWHICH VALUE OF X YOU WANT TO REACH:");
scanf("%f",&x);
printf("\nENTER STEP SIZE:");
scanf("%f",&h);
n=iteration(x,x0,h);
if(x>x0)
{
for(i=0;i<n;i++)
{
x1=x0+h;
f0=function(x0,y0);
rvalue=yvalue(y0,f0,h);
lvalue=xvalue(x0,h);
fvalue=function(lvalue,rvalue);
now=(fvalue*h);
y1=(y0+now);
printf("\n\nY(%.3f)=%.3f\n",x1,y1);
y0=y1;
x0=x1;
}
}
else
{
for(i=0;i<n;i++)
{
x1=x0-h;
f0=function(x0,y0);
rvalue=yvalue(y0,f0,h);
lvalue=xvalue(x0,h);
fvalue=function(lvalue,rvalue);
now=(fvalue*h);
y1=(y0+now);
printf("\n\nY(%.3f)=%.3f\n",x1,y1);
y0=y1;
x0=x1;
}
}
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 derivative(float x)
{
float d;
d=((2*x)-4);
return d;
}
float point(float x0,float f0, float f1)
{
float x1;
x1=(x0-(f0/f1));
return x1;
}
float error(float x0,float x1)
{
float value;
value=(fabs((x0-x1)/x0)*100);
return value;
}
void main()
{
clrscr();
int i;
float x0=0,x1=0,f0=0,f1=0,e=0;
printf("ENTER INITIAL VALUE:");
scanf("%f",&x0);
f0=function(x0);
if(f0==0)
{
printf("root is =%.4f",x0);
}
else
{
for(i=0;i<=6;i++)
{
printf("x0=%.4f",x0);
f0=function(x0);
printf("\nf0=%.4f",f0);
f1=derivative(x0);
printf("\nf1=%.4f",f1);
x1=point(x0,f0,f1);
printf("\nx1=%.4f",x1);
e=error(x0,x1);
printf("\nERROR=%.4f",e);
x0=x1;
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 point(float x1,float x2,float f1, float f2)
{
float x3;
x3=(x2-((f1*(x2-x1))/(f2-f1)));
return x3;
}
float error(float x1,float x2)
{
float value;
value=(fabs((x2-x1)/x2)*100);
return value;
}
void main()
{
clrscr();
int i;
float x1=0,x2=0,x3=0,f1=0,f2=0,f3=0,e=0,p=0,q=0;
printf("ENTER 1st INITIAL VALUE:");
scanf("%f",&p);
printf("ENTER 2nd INITIAL VALUE:");
scanf("%f",&q);
f1=function(p);
f2=function(q);
if(f1*f2==0)
{
if(f1==0)
{
printf("ROOT IS=%.3f",p);
}
else
{
printf("ROOT IS=%.3f",q);
}
}
else
{
if(p>q)
{
x1=p;
x2=q;
}
else
{
x1=q;
x2=p;
}
for(i=0;i<=6;i++)
{
printf("x1=%.4f",x1);
f1=function(x1);
printf(" f1=%.4f",f1);
printf("\nx2=%.4f",x2);
f2=function(x2);
printf(" f2=%.4f",f2);
x3=point(x1,x2,f1,f2);
printf("\nx3=%.4f",x3);
f3=function(x3);
printf(" f3=%.4f",f3);
e=error(x1,x2);
printf("\n ERROR=%.4f",e);
x1=x2;
x2=x3;
printf("\n---------------------------------------------\n");
}
}
getch();
}
#include<conio.h>
#include<math.h>
float function(float x)
{
float c;
c=((x*x)-(4*x)+3);
return c;
}
float point(float x1,float x2,float f1, float f2)
{
float x3;
x3=(x2-((f1*(x2-x1))/(f2-f1)));
return x3;
}
float error(float x1,float x2)
{
float value;
value=(fabs((x2-x1)/x2)*100);
return value;
}
void main()
{
clrscr();
int i;
float x1=0,x2=0,x3=0,f1=0,f2=0,f3=0,e=0,p=0,q=0;
printf("ENTER 1st INITIAL VALUE:");
scanf("%f",&p);
printf("ENTER 2nd INITIAL VALUE:");
scanf("%f",&q);
f1=function(p);
f2=function(q);
if(f1*f2==0)
{
if(f1==0)
{
printf("ROOT IS=%.3f",p);
}
else
{
printf("ROOT IS=%.3f",q);
}
}
else
{
if(p>q)
{
x1=p;
x2=q;
}
else
{
x1=q;
x2=p;
}
for(i=0;i<=6;i++)
{
printf("x1=%.4f",x1);
f1=function(x1);
printf(" f1=%.4f",f1);
printf("\nx2=%.4f",x2);
f2=function(x2);
printf(" f2=%.4f",f2);
x3=point(x1,x2,f1,f2);
printf("\nx3=%.4f",x3);
f3=function(x3);
printf(" f3=%.4f",f3);
e=error(x1,x2);
printf("\n ERROR=%.4f",e);
x1=x2;
x2=x3;
printf("\n---------------------------------------------\n");
}
}
getch();
}
Very informative and worthy post. Thanks for the sharing such a precious updates with us.
ReplyDeletePanasonic - 10.1" Toughbook Notebook - 4 GB Memory - 320 GB Hard Drive
Panasonic - Refurbished - 10.4" Toughbook Notebook - 1 GB Memory - 80 GB Hard Drive - Magnesium Alloy