Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Bisection :
- #include<bits/stdc++.h>
- #define E 0.01
- using namespace std;
- double functionn(double x){
- return x*x+log(x)-2;
- }
- int n=1;
- double c;
- void bisection(double a , double b){
- if(functionn(a)*functionn(b)>=0)
- {
- cout<<"Incorrect a and b";
- return;
- }
- c=a;
- while((b-a)>=E)
- {
- c=(a+b)/2;
- if(functionn(c)*functionn(a)<0)
- {
- cout<<"Approaximation No"<<n<<" : "<< c<<endl;
- cout<<"functional values"<<c*c+log(c)-2<<endl;
- b=c;}
- else{
- cout<<"Aproaximation No"<<n<<" : " << c<< endl;
- cout<<"functional values="<<c*c+log(c)-2<<endl;
- a=c;
- }
- n++;
- cout<<endl;
- }}
- int main(){
- cout<<"Enter input a and b : ";
- double a,b;
- cin>>a>>b;
- cout<<"Here , a="<<a<<" and b="<<b<<endl;
- bisection(a,b);
- cout<<endl;
- cout<<"So the required root is = "<<c<<endl;
- return 0;
- }
- False Position:
- #include<stdio.h>
- #include<math.h>
- #define t 0.0001
- #define F(x) x*x*x-2*x*x-4
- int main(){
- float x0,x1,x2,f0,f1,f2;
- printf("Enter the value of x0: ");
- scanf("%f",&x0);
- printf("Enter the value of x1: ");
- scanf("%f",&x1);
- printf("\n_______________________________________________");
- printf("\nx0\t x1\t x2\t f0\t f1\t f2 ");
- printf("\n_______________________________________________");
- do{
- f0=F(x0);
- f1=F(x1);
- x2=x0-((f0*(x1-x0))/(f1-f0));
- f2=F(x2);
- printf("\n%f %f %f %f %f",x0,x1,x2,f1,f2);
- if(F(x0)*F(x2)<0){
- x1=x2;
- }
- else{
- x0=x2;
- }
- }while(fabs(f2)>t||F(x2)==0);
- printf("\n___________________________________________");
- printf("\n\nApproaximation root:%f",x2);
- }
- Newton:
- #include<stdio.h>
- #include<math.h>
- #define E 0.0001
- #define f(x) sin(x)-1+x*x
- #define g(x) cos(x)+2*x
- int main(){
- float x0,x1,x2,f0,g0,root;
- int i=1;
- printf("Using Newton Raphson Method to find root of the equation (x*x)-4*x-10\n");
- printf("\n Enter the value of x0 :");
- scanf("%f",&x0);
- printf("Step \t x0\t\t x1\t\t f0\t\tg0\n");
- b:f0=f(x0);
- g0=g(x0);
- x1=x0-(f0/g0);
- printf("%d\t %4f\t %4f\t %4f \t%4f \n",i,x0,x1,f0,g0);
- i++;
- if(fabs((x1-x0)/x1)<=E){
- root=x1;
- printf("The root is %4f",root);
- goto c;}
- else {
- x0=x1;
- goto b ;
- }
- c:
- getch();
- }
- Trapezoid:
- #include<stdio.h>
- #include<math.h>
- #define f(x) x*x*x*x
- int main()
- {
- int i ,n;
- double a,b,h,x, sum=0, integral;
- printf("Enter hte number of sub-interval :");
- scanf("%d",&n);
- printf("Enter the initial limit : ");
- scanf("%lf",&a);
- printf("Enter the final limit : ");
- scanf("%lf",&b);
- h=fabs(b-a)/n;
- for(i=1;i<n ;i++){
- x=a+i*h;
- sum+=f(x);
- }
- integral=(h/2)*(f(a)+f(b)+2*sum);
- printf("\nThe integral is : %lf \n ",integral);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement