Wednesday 13 February 2013

Numerical Methods: Solution of non-linear equation by using Secant method in C

Source Code:
///solution of non linear equations using secant method
#include<stdio.h>
#include<math.h>
float f(float x){
   return (pow(x,5) - 3*pow(x,3)-1);
}
int main(){
    float a, b, c;
    int count = 0;
    printf("Enter the initial value of a: ");
    scanf("%f", &a);
    printf("Enter the initial value of b: ");
    scanf("%f", &b);
    while(1){
         count++;
            c = (a*f(b)-b*f(a))/(f(b)-f(a));
         if(c==b){
            break;
         }
         if(count>=20){
            break;
         }
         a = b;
         b = c;
    }
    printf("\nThe root after %d iteration is %.7f\n",count, c);
    return 0;
}

No comments:

Post a Comment

Comment