Responsive Ads Here

Wednesday, November 12, 2014

NUmeric : Bisection code

Sample equation for Bisection

 

F(x)=7.50x4+8.5x3-11.5x2+12x1-55

//lab : bisection
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;

int main()
{
float xi,xu,xr,Es,a[40],old=0,Ea;
int n,K=0;
cout<< "HIghest power in the equation : " ;
cin>>n;
//.........................................
printf("Enter Xi =");
scanf("%f",&xi);
printf("Enter Xu =");
scanf("%f",&xu);
printf("Enter Es =");
scanf("%f",&Es);
//..........................................
printf("\n");
printf("enter coefficient :\n");
for (int i=n; i>=0; i--)
{
printf("X^%d =",i);
scanf("%f",&a[i]);
}
//...........................................
while(1)
{
xr=(xi+xu)/2;
float fxi=0,fxu=0,fxr=0;
//
for (int i=n; i>=0; i--)
{
fxi=fxi+a[i]*pow(xi,i);
fxu=fxu+a[i]*pow(xu,i);
fxr=fxr+a[i]*pow(xr,i);
}
printf("\n\n\nSTEP no: %d\n",K+1);
printf("Xi = %.2f",xi);
printf("\nfxi = %.2f",fxi);
//
printf("\nXu = %.2f",xu);
printf("\nfxu = %.2f",fxu);
//
printf("\nXr = %.2f",xr);
printf("\nfxr = %.2f",fxr);
//
Ea=((xr-old)/xr)*100;

old = xr;

//cout <<" now =" <<old;
if(Ea<0)
{
Ea=Ea*(-1);
}
printf("\nEa= %f",Ea);

if(fxr == 0)
{
break ;
}
if((fxr*fxu)<0)
{
xi=xr;
printf("\n\n xI = %f\n\n",xi);
}
else if ((fxr*fxi)<0)
{
xu=xr;
printf("\n\n Xu =%f\n\n",xu);
}
if(Ea<Es)
{
break;
}
K++;
}
   return 0;
}
 
 
 

No comments:

Post a Comment

php4

<?php    // Start the session  session_start();  ?>  <!DOCTYPE html>  <html>  <head>       <link rel=&q...