VideoBar

This content is not yet available over encrypted connections.

Wednesday, October 5, 2016

প্রোগ্রামিং-এর গল্প (A tale of Programming) : Numerical Method- Coding for Newton Raphson Method

//Program for Newton-Raphson Method
#include<stdio.h>
#include<math.h>
float f(float x)
{
    return x*x-4*x-10;
}
float fdx(float x)
{
    return 2*x-4;
}
main()
{
float x1,x2,f1,f2,e,error,root;
int i=0;
printf("Enter the initial value x1:");
scanf("%f",&x1);
printf("Enter the error limit e:");
scanf("%f",&e);
while(1)
{
i++;//i=i+1;
f1=f(x1);
f2=fdx(x1);
x2=x1-(f1/f2);
error=fabs((x2-x1)/x2);
if(error<e)
{
    root=x2;
    printf("\n\n\n\nNo. of iteration:%d",i-1);
    printf("\n\nFinal functional value:%f",f1);
    printf("\n\nFinal root:%f",root);
    break;
}
else
{
    x1=x2;
    printf("\n\nIteration=%d  x1=%.2f  x2=%.2f  f1=%.2f  f2=%.2f  error=%.2f",i,x1,x2,f1,f2,error);
}

}

}
Output for Newton Raphson Method

No comments:

Post a Comment