Sample equation for Newton Rapson
where X=3;
F(x)=2x3-11.7x2+17.7x1-5
//NETON RAPSON
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
float xi,Es,a[40],xiii,old=0,Ea;
int n,K=0;
cout<< "HIghest power in the equation : " ;
cin>>n;
//.........................................
printf("Enter Xi =");
scanf("%f",&xi);
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)
{
float fxi=0,deff=0;
printf("\n\n\nSTEP no: %d\n",K+1);
for (int i=n; i>=0; i--)
{
fxi=fxi+a[i]*pow(xi,i);
}
for(int i=n; i>=0; i--)
{
deff=deff+i*a[i]*pow(xi,i-1);
}
printf("Xi = %.2f",xi);
printf("\nfxi = %.2f",fxi);
printf("\ndeff = %.2f",deff);
xiii=(xi-(fxi/deff));
printf("\nnew xi = %.2f",xiii);
Ea=((xiii-old)/xiii)*100;
if(Ea<0)
{
Ea=Ea*(-1);
}
printf("\nEa= %f",Ea);
old=xiii;
if(Ea<Es)
{
break;
}
xi=xiii;
K++;
}
return 0;
}
No comments:
Post a Comment