Responsive Ads Here

Wednesday, November 12, 2014

NUmeric : False Position code

 

Sample equation for False position

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

 

 

//false position
#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=1;
    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]);
    }
     printf("\n");
    //...........................................
    while(1)
    {
 
        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);
        }
        xr=xu-((fxu*(xi-xu))/(fxi-fxu));
        for (int i=n; i>=0; i--)
        {
            fxr=fxr+a[i]*pow(xr,i);
        }
  printf("\n\n\nSTEP no: %d\n",K);
        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...