Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<math.h>
- #include<conio.h>
- using namespace std;
- #define f(x) (x*x)+x-2
- #define E 0.00001
- int main()
- {
- float x1,x2,f0,f1,f2,x0,root;
- int i=1;
- cout<<"Enter two limits: ";
- cin>>x1>>x2;
- f1=f(x1);
- f2=f(x2);
- if(f1*f2>0)
- {
- cout<<"x1 &x2 do not bracket any root";
- goto stop;
- }
- while (1)
- {
- x0=x1-((f1*(x2-x1))/(f2-f1));
- f0=f(x0);
- if(f1*f0<0)
- {
- x2=x0;
- f2=f0;
- }
- else{
- x1=x0;
- f1=f0;
- }
- cout.precision(6);
- cout<<"\nIteration " <<i <<"\t"<<fixed<<x1<<"\t\t"<<fixed<<x2;
- if (fabs((x2-x1)/x2)<E)
- {
- root = (x1+x2)*0.5;
- cout.precision(6);
- cout<<"\n\nFinal iteration "<<i+1<<"\tRoot = "<<fixed<<root;
- goto stop;
- }
- else
- i++;
- }
- stop:
- getch();
- return 0;
- }
- newton
- #include<iostream>
- #include<math.h>
- using namespace std;
- #define E 0.000001
- #define MAX 20
- #define F(x) (x*x)+x-2
- #define FD(x) 2*x+1
- int main()
- {
- float x0,xn,fx,fdx,fv;
- int i=1;
- cout<<"Input initial value of x"<<endl;
- cin>>x0;
- cout<<"SOLUTION BY NEWTON-RAPHSON METHOD"<<endl;
- m3014:
- fx=F(x0);
- fdx = FD(x0);
- xn=x0-fx/fdx;
- fv = F(xn);
- if(fabs((xn-x0)/xn)<E)
- {
- cout.precision(7);
- cout<<"Root = "<<fixed<<xn<<endl;
- cout.precision(7);
- cout<<"Function value = "<<fixed<<fv<<endl;
- cout<<"Number of iteration = "<<i<<endl;
- }
- else{
- x0 = xn;
- i++;
- if(i<MAX)
- goto m3014;
- else{
- cout<<"SOLUTION DOES NOT CONVERGE"<<endl;
- cout<<"IN "<<MAX<<" ITERATIOMS"<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement