mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
NR
This commit is contained in:
parent
19e4f0329c
commit
e1c238105d
53
Computer Oriented Statistical Methods/Newton_Raphson.CPP
Normal file
53
Computer Oriented Statistical Methods/Newton_Raphson.CPP
Normal file
@ -0,0 +1,53 @@
|
||||
#include<iostream.h>
|
||||
#include<conio.h>
|
||||
#include<math.h>
|
||||
|
||||
float eq(float i)
|
||||
{
|
||||
return (pow(i,3)-(4*i)-9); // original equation
|
||||
}
|
||||
float eq_der(float i)
|
||||
{
|
||||
return ((3*pow(i,2))-4); // derivative of equation
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
float a,b,z,c,m,n;
|
||||
clrscr();
|
||||
for(int i=0;i<100;i++)
|
||||
{
|
||||
z=eq(i);
|
||||
if(z>=0)
|
||||
{
|
||||
b=i;
|
||||
a=--i;
|
||||
goto START;
|
||||
}
|
||||
}
|
||||
|
||||
START:
|
||||
|
||||
cout<<"\nFirst initial: "<<a;
|
||||
cout<<"\nSecond initial: "<<b;
|
||||
c=(a+b)/2;
|
||||
|
||||
for(i=0;i<100;i++)
|
||||
{
|
||||
float h;
|
||||
m=eq(c);
|
||||
n=eq_der(c);
|
||||
|
||||
z=c-(m/n);
|
||||
c=z;
|
||||
|
||||
if(m > 0 && m<0.009) // stoping criteria
|
||||
{
|
||||
goto END;
|
||||
}
|
||||
}
|
||||
|
||||
END:
|
||||
cout<<"\n\nRoot: "<<z;
|
||||
getch();
|
||||
}
|
Loading…
Reference in New Issue
Block a user