Numerical method-labsheet.pdf

Numerical method-labsheet.pdf - Bisection...

Info icon This preview shows pages 1–13. Sign up to view the full content.

Source: Page 1 Bisection Method #include<iostream.h> #include<conio.h> #include<math.h> #define EPS 0.000001 #define F(x) log(x)-cos(x) void bim(float*a, float*b, float*root, float*s, int*count); void main() { int count; float a, b, root, s; cout<<"SOLUTION BY BISECTION METHOD:"<<endl; cout<<"input starting values:"<<endl; cin>>a>>b; bim(&a, &b, &root, &s, &count); if (s==0) { cout<<"starting points donot bracket any root"<<endl; cout<<"check whether they bracket EVEN roots."<<endl; } else { cout<<"root="<<root<<endl; float fun = F(root); cout<<"f(root)="<<fun<<endl; csitnepal
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Source: Page 2 cout<<"Iterations="<<count<<endl; } getch(); clrscr(); } void bim(float*a, float*b, float*root, float*s, int*count) { float x1, x2, x0, f0, f1, f2; x1=*a; x2=*b; f1=F(x1); f2=F(x2); if(f1*f2> 0) { *s=0; return; } else { *count=0; begin: x0=(x1+x2)/2.0; f0=F(x0); if(f0==0) { *s=1; *root=x0; return; csitnepal
Image of page 2
Source: Page 3 } if(f1*f0<0) { x2=x0; } else { x1=x0; f1=f0; } if(fabs((x2-x1)/x2) < EPS) { *s=1; *root=(x1+x2)/2.0; return; } else { *count=*count+1; goto begin; } } } csitnepal
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Source: Page 4 Output of Bisection Method csitnepal
Image of page 4
Source: Page 5 Secant Method #include<iostream.h> #include<conio.h> #include<math.h> #define EPS 0.000001 #define MAXIT 50 #define F(x) exp(x)-x-2 int sec(float*a, float*b, float*x1, float*x2, float*root, int*count, int*status); void main() { float a, b, root, x1, x2, fr; int count, status; clrscr(); cout<<"SOLUTION BY SECANT METHOD"<<endl; cout<<"Input two starting points:"<<endl; cin>>a>>b; sec(&a, &b, &x1, &x2, &root, &count, &status); if(status==1) { cout<<"DIVISION BY ZERO"<<endl; cout<<"last x1="<<x1<<endl<<"last x2="<<x2<<endl; cout<<"number of iterations="<<count<<endl; } if(status==2) csitnepal
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Source: Page 6 { cout<<"NO CONVERGENCE IN"<<MAXIT<<" ITERATIONS."<<endl; } else { cout<<"root="<<root<<endl; fr=F(root); cout<<"function Value at root="<<fr<<endl; cout<<"no. of iterations="<<count<<endl; } getch(); } int sec(float*a, float*b, float*x1, float*x2, float*root, int*count, int*status) { float x3, f1, f2, error; *x1=*a; *x2=*b; f1=F(*a); f2=F(*b); *count=1; begin: if (fabs(f1-f2)<=1.E-10) { *status=1; return 0; } x3=*x2-f2*(*x2-*x1)/(f2-f1); error=fabs((x3-*x2)/x3); csitnepal
Image of page 6
Source: Page 7 if (error>EPS) { if (*count==MAXIT) { *status=2; return 0; } else { *x1=*x2; } *x2=x3; f1=f2; f2=F(x3); *count=*count+1; goto begin; } else { *root=x3; *status=3; return 0; } } csitnepal
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Source: Page 8 Output of Secant Method csitnepal
Image of page 8
Source: Page 9 Newton-Raphson Method #include<iostream.h> #include<conio.h> #include<math.h> #define EPS 0.000001 #define MAXIT 20 #define F(x) (x)*(x)*(x)+(x)*(x)-3*(x)-3 #define FD(x) 3*(x)*(x)+2*(x)-3 void main() { clrscr(); int count; float x0, xn, fx, fdx, fxn; cout<<"SOLUTION BY NEWTON RAPHSON'S METHOD"<<endl; cout<<"input initial value of x:"<<endl; cin>>x0; count=1; begin: fx=F(x0); fdx=FD(x0); xn=x0-fx/fdx; if (fabs((xn-x0)/xn) < EPS) { cout<<"root="<<xn<<endl; csitnepal
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Source: Page 10 fxn =F(xn); cout<<"function value="<<fxn<<endl; cout<<"no. of iterations="<<count<<endl; } else { x0=xn; count=count+1; if(count<MAXIT) { goto begin; } else { cout<<"SOLUTION DOESNOT CONVERGE."<<endl; cout<<"iterations="<<MAXIT<<endl; } } getch(); } csitnepal
Image of page 10
Source: Page 11 Output of Newton-Raphson Method csitnepal
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Source: Page 12 Fixed-point Iteration Method #include<iostream.h> #include<conio.h> #include<iomanip.h>
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern