IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Methods in C version Toma 0.0.1

3 posters

Go down

User verified solution Methods in C version Toma 0.0.1

Post by BIT0107-Toma Fri May 21, 2010 6:28 am

CHAPTER 6:

6.6. Bisection Method:
Code:
#include<stdio.h>
#include<math.h>

double f(double x);

int main(void){
    double a=-2, b=-1, c, fa = f(a), fb = f(b), fc, e = .000001;
    int count=0;

    if(fa*fb>0){
        printf("Root is not in range\n");
        return 0;
    }

    do{
        c=(a+b)/2;
        fc = f(c);

        if(fa*fc
            b = c;
            fb = fc;
        }
        else{
            a = c;
              fa = fc;
        }
        count++;
    }while(fabs(b-a)>e);

    printf("The root is %f & solved by %d iterations\n", c, count);

    return 0;
}

double f(double x){
    return (x*x) - (4*x) -10;
}

6.7. False Position Method:
Code:
#include<stdio.h>
#include<math.h>

double f(double x);

int main(void){
    double a=1, b=3, c, fa = f(a), fb = f(b), fc, e = .000001;
    int count=0;

    if(fa*fb>0){
        printf("Root is not in range\n");
        return 0;
    }

    do{
        c = a - fa*((b-a)/(fb-fa));
        fc = f(c);

        if(fa*fc
            b = c;
            fb = fc;
        }
        else{
            a = c;
              fa = fc;
        }
        count++;
    }while(fabs(fc)>e);

    printf("The root is %f & solved by %d iterations\n", c, count);

    return 0;
}

double f(double x){
    return (x*x) - x - 2;
}

6.8. Newton-Raphson Method:
Code:
#include<stdio.h>
#include<math.h>

double f(double x);
double f_(double x);

int main(void){
    double a=0, b, fa=f(a), f_a=f_(a), fb, e=.000001;
    int count=0;

    do{
        b = a - (fa/f_a);
        fb = f(b);
        a = b;
        fa = fb;
        f_a = f_(b);
        count++;
    }while(fabs(fb)>e);

    printf("The root is %f & solved by %d iterations\n", b, count);
   
    return 0;
}
double f(double x){
    return (x*x) - (3*x) + 2;
}
double f_(double x){
    return (2*x) - 3;
}

6.9. Secant Method:
Code:
#include<stdio.h>
#include<math.h>

double f(double x);

int main(void){
    double a=4, b=2, c, fa=f(a), fb=f(b), fc, e=.000001;
    int count=0;

    do{
        c = ((fb*a)-(fa*b))/(fb-fa);
        a = b;
        fa = fb;
        b = c;
        fb = f(c);
        count++;
    }while(fabs(a-b)>e);

    printf("The root is %f & solved by %d iterations\n", c, count);

    return 0;
}
double f(double x){
    return (x*x) - (4*x) - 10;
}

6.10. Fixed-Point Method:
Code:
#include
#include

double f(double x);
double g(double x);

int main(void){
    double a=0, b, c, e=.000001;
    int count=0;

    do{
        b = g(a);
        c = a;
        a = b;
        count++;
    }while(fabs(b-c)>e);

    printf("The root is %f & solved by %d iterations\n", b, count);

    return 0;
}
double f(double x){//no need of this. only for understanding the function
    return (x*x) + x - 2;
}
double g(double x){
    return 2 - (x*x);
}


Last edited by BIT0122-Amit on Fri May 21, 2010 1:46 pm; edited 2 times in total (Reason for editing : Better descriptive title :p)
BIT0107-Toma
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

Back to top Go down

User verified solution Re: Methods in C version Toma 0.0.1

Post by BIT0107-Toma Fri May 21, 2010 6:33 am

CHAPTER 8:

8.2. Jacobi Iteration Method:
Code:
#include<stdio.h>
#include<math.h>

int main(void){
    int n=3;
    double x0[3], x[3], b[3] = {5, 15, 8}, e=.000001;
    double a[3][3] = {
    2, 1, 1,
    3, 5, 2,
    2, 1, 4
    };

    int count=0;

    int i, j;
    for(i=0; i<n; i++){
        x0[i] = b[i]/a[i][i];
    }

    int isError=0;
    do{
        isError = 0;

        for(i=0; i<n; i++){
            double sum = b[i];
            for(j=0; j<n; j++)
                if(i!=j)
                    sum = sum - a[i][j]*x0[j];
            x[i] = sum/a[i][i];
            if(isError==0 && fabs(x[i]-x0[i])>e){
                isError = 1;
                x0[i] = x[i];
            }
        }
        count++;
    }while(isError);

    printf("Roots are: ");
    for(i=0; i<n; i++)
        printf("%f  ", x[i]);
    printf("& solved by %d iterations\n", count);

    return 0;
}


8.3. Guass-Seidel Method:
Code:
#include<stdio.h>
#include<math.h>

int main(void){
    int n = 3;
    double x[3], b[3] = {5, 15, 8}, e=.000001;
    double a[3][3] = {
    2, 1, 1,
    3, 5, 2,
    2, 1, 4   
    };

    int count=0;

    int i, j;
    for(i=0; i<n; i++)
        x[i] = b[i]/a[i][i];

    int isError;
    do{
        isError=0;

        for(i=0; i<n; i++){
            double sum = b[i];
            for(j=0; j<n; j++)
                if(i!=j)
                    sum = sum - a[i][j]*x[j];
            double dummy = sum/a[i][i];
            if(isError==0 && fabs(dummy-x[i])>e)
                isError = 1;
            x[i] = dummy;
        }
        count++;
    }while(isError);

    printf("Roots are: ");
    for(i=0; i<n; i++)
        printf("%f  ", x[i]);
    printf("& solved by %d iterations\n", count);

    return 0;
}

8.4. Method of Relaxation:
I have not done it coz I dont know what to assume the value of r [You must be registered and logged in to see this image.] .
BIT0107-Toma
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

Back to top Go down

User verified solution Re: Methods in C version Toma 0.0.1

Post by BIT0129-Tabassum Fri May 21, 2010 6:51 am

Oh!!! Thanks dear..Very Happy
rep ++
BIT0129-Tabassum
BIT0129-Tabassum
Global Moderator
Global Moderator

Course(s) :
  • BIT

Blood Group : A+
Posts : 1496
Points : 2298

http://probe-tabassum.blogspot.com

Back to top Go down

User verified solution Re: Methods in C version Toma 0.0.1

Post by BIT0101-rayhan Fri May 21, 2010 1:07 pm

its exactly same with gauss seidel method... whatever.... the value of here should be inside 1 and 2.. try with setting a value of r as 1.5
BIT0101-rayhan
BIT0101-rayhan
Release Candidate
Release Candidate

Posts : 107
Points : 177

Back to top Go down

User verified solution Re: Methods in C version Toma 0.0.1

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum