Computer Scientists are Pretty Pessimistic

Showing posts with label Numerical Analysis. Show all posts
Showing posts with label Numerical Analysis. Show all posts

Thursday, 16 November 2017

Secant Method


#include <stdio.h>
#include <math.h>
double f(double x)
{
    return pow(x,2)-4*x-10; //Given equation may will be changed
}
int main()
{
    float x1,x2,x3,f1,f2,E;
    printf("Enter initial points X1 and X2: ");
    scanf("%f %f",&x2,&x3);
    printf("Enter the tolerance value: ");
    scanf("%f",&E);

    do
    {
        x1 = x2;
        x2 = x3;

        f1 = f(x1);
        f2 = f(x2);

        x3 = (f2*x1 - f1*x2)/(f2-f1);
    }
    while(fabs((x3-x2)/x3)>E);
    printf("Root is: %.2f\n",x3);

    return 0;
}

//Reference : Nemerical Methods by E Balagurusamy (2016-2017) pages (153 -154)

Friday, 20 October 2017

Newton-Raphson Method

#include <stdio.h>
#include <math.h>
double f(double x)
{
    return pow(x,2)-3*x+2; //Given equation may will be changed
}
double ff(double x)
{
    return 2*x-3; //Given equation may will be changed
}
int main()
{
    float x1,x2,x0,E;
    printf("Enter initial value of X: ");
    scanf("%f",&x1);
    printf("Enter the tolerance value: ");
    scanf("%f",&E); // Tolerance may vary 0.0001
    do
    {
        x0 = x1;
        x1 = x0 - (f(x0)/ff(x0));

    }while(fabs((x1-x0)/x1)>E);
    printf("Root is: %.2f\n",x0);
    return 0;
}
//Reference : Nemerical Methods by E Balagurusamy (2016-2017) pages (146 -147)

Thursday, 12 October 2017

False Position Method

#include <stdio.h>
#include <math.h>
double f(double x)
{
    return pow(x,2)+x-2; //Given equation may will be changed
}
int main()
{
    float x1,x2,x0,E;
    printf("Enter interval: ");
    scanf("%f %f",&x1,&x2);
    printf("Enter the tolerance value: ");
    scanf("%f",&E);
    if(f(x1)*f(x2)>=0)
    {
        printf("Incorrect Interval\n");
        return 0;
    }
    x0 = x1 - (f(x1)*((x2-x1)/(f(x2)-f(x1))));
    if(f(x0)==0)
    {
        printf("Root is: %.2lf\n",x0);
        return 0;
    }
    do
    {
        x0 = x1 - (f(x1)*((x2-x1)/(f(x2)-f(x1))));
        if(f(x1)*f(x0)<0)
        {
            x2 = x0;
        }
        else
        {
            x1 = x0;
        }
    }while(fabs((x2-x1)/x2)>E);
    printf("Root is: %.2f\n",(x1+x2)/2);
    return 0;
}
//Reference : Nemerical Methods by E Balagurusamy (2016-2017) pages (138 -144)

Tuesday, 28 February 2017

Bisection Method

#include <stdio.h>
#include <math.h>
double f(double x)
{
    return pow(x,2)-4*x-10; //Given equation may will be changed
}
int main()
{
    float x1,x2,x0,E;
    printf("Enter interval: ");
    scanf("%f %f",&x1,&x2);
    printf("Enter the tolerance value: ");
    scanf("%f",&E);
    if(f(x1)*f(x2)>=0)
    {
        printf("Incorrect Interval\n");
        return 0;
    }
    x0 = (x1+x2)/2;
    if(f(x0)==0)
    {
        printf("Root is: %.2lf\n",x0);
        return 0;
    }
    do
    {
        x0 = (x1+x2)/2;
        if(f(x1)*f(x0)<0)
        {
            x2 = x0;
        }
        else
        {
            x1 = x0;
        }
    }while(fabs((x2-x1)/x2)>E);
    printf("Root is: %.2f\n",(x1+x2)/2);
    return 0;
}
//Reference : Nemerical Methods by E Balagurusamy (2016-2017) pages (131 -138)