Monday, December 9, 2013

Newton Raphson Method to solve non-linear equations

Introduction

It is one of the most widely used methods of solving equation as it is more rapidly convergent than other methods. Starting from single initial estimate, x0, that is not too far from a root, we move along the tangent to its intersection with the x-axis, and take that as the next approximation. This is continued as
x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} \,
until either the successive x-values are sufficiently close or the value of the function is sufficiently near zero.


Newton Raphson Method in Codes

/*A program to find a root of function using Newton Raphson Method */

#include<stdio.h>
#include<math.h>

float f(float x)
{
    return x*sin(x) + cos(x);
}

float g(float x)
{
    return x*cos(x);        /* Derivative of function f(x) */
}

int main()
{
    float x0, x1, a, b, c;

    printf("Enter the 'a' and 'b' of interval (a,b)\n\n");
    scanf("%f%f",&a,&b);

    x0= (a+b)/2;

    for(c=0;c<20;c++)     /* The loop is repeated just 20 times as this method is expected to give root in 5/10 steps generally */
    {
        if(g(x0)==0)
            printf("\n\nErooooooooorrrrrr");
        else
            x1 = x0 - f(x0) / g(x0);

        x0=x1;
    }

    printf("\n\nThe required root is %f\n\n",x0);
    return 0;

}

Friday, December 6, 2013

Roots by Bisection Method

Introduction

Bisection method is one of the most ancient and surely the simplest method to find the root of a function. But it is relatively time consuming method. First, we must know an interval in which a root lies. Root is found by repeatedly bisecting an interval. Successive values converge on a root of function f(x) when we begin with a pair of values that bracket the root. Let's take the interval (x1, x2) , x3 is halfway between x1 and x2, x4 is halfway between x2 and x3. We always take the next x-value as the midpoint of the last pair that bracket the root : these values bracket the root when there is sign change of f(x) at the two points. We repeat the process until we converge to the root.



Using C programming language to solve a function by Bisection Method


/*  To determine the root of a function by Bisection Method */

#include<stdio.h>
#include<math.h>

float f(float x)
{
    return x*sin(x) + cos(x);                   /* A continuous equation between given interval*/
 
}


int main()
{
    float a,b,c,d,e;

    printf("\nEnter  the interval (a,b)\n");
    scanf("%f%f",&a,&b);

    printf("\nEnter the tolerance value\n");
    scanf("%f",&d);

    do
    {
        c = (a+b)/2;
        if(f(c) * f(a) < 0)
            b=c;
        else
            a=c;
    }while( fabs(a-b)>d||f(c)==0);

    printf("Root is %f",c);

    return 0;

}

Sunday, February 19, 2012

String concatenation using pointer


/* A program to concatenate two string using pointer  */

#include<stdio.h>

void concatenate_string(char*, char*);

int main()
{
    char original[100], add[100];

    printf("Enter source string\n");
    gets(original);

    printf("Enter string to concatenate\n");
    gets(add);

    concatenate_string(original, add);

    printf("String after concatenation is \"%s\"\n", original);

    return 0;
}

void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;

   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}

Matrix multiply using pointer


/*A program to multiply two matrices using pointer */

#include<stdio.h>
int main()
{
    int a[10][10],b[10][10],c[10][10],sum=0;
    int m1,m2,n1,n2,i,j,k;
    int *ptr1,*ptr2,*ptr3;
    ptr1=a;ptr2=b;ptr3=c;

    printf("enter no of row and column for 1st matrix\n");
    scanf("%d%d",&m1,&n1);
    printf("enter no of row and column for 2nd matrix\n");
    scanf("%d%d",&m2,&n2);

    if(n1==m2)
    {
        printf("for 1st matrix\n");
        for(i=0;i<m1;i++)
            for(j=0;j<n1;j++)
            {
                printf("a[%d][%d]=",i,j);
                scanf("%d",ptr1+i*10+j);
            }

        printf("\nfor 2nd matrix\n");
        for(i=0;i<m2;i++)
            for(j=0;j<n2;j++)
            {
                printf("b[%d][%d]=",i,j);
                scanf("%d",ptr2+i*10+j);
            }


        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
               for(k=0;k<n2;k++)
                 sum+=*(ptr1+i*10+k)**(ptr2+k*10+j);
            *(ptr3+i*10+j)=sum;
            sum=0;
            }
        }

        printf("product of entered matrices :-\n");
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
                printf("%d\t",*(ptr3+i*10+j));
            printf("\n");
        }


    }
    else
    printf("multiplication is not possible");
    return 0;
}

Matrix multiply


/* A program to multiply two matrices */

#include<stdio.h>
int main()
{
    int a[10][10],b[10][10],c[10][10],sum=0;
    int m1,m2,n1,n2,i,j,k;

    printf("enter no of row and column for 1st matrix\n");
    scanf("%d%d",&m1,&n1);
    printf("enter no of row and column for 2nd matrix\n");
    scanf("%d%d",&m2,&n2);

    if(n1==m2)
    {
        printf("for 1st matrix\n");
        for(i=0;i<m1;i++)
            for(j=0;j<n1;j++)
            {
                printf("a[%d][%d]=",i,j);
                scanf("%d",&a[i][j]);
            }

        printf("\nfor 2nd matrix\n");
        for(i=0;i<m2;i++)
            for(j=0;j<n2;j++)
            {
                printf("b[%d][%d]=",i,j);
                scanf("%d",&b[i][j]);
            }
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
               for(k=0;k<n2;k++)
                 sum+=a[i][k]*b[k][j];
            c[i][j]=sum;
            sum=0;
            }
        }

        printf("product of entered matrices :-\n");
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
                printf("%d\t",c[i][j]);
            printf("\n");
        }


    }
    else
    printf("multiplication is not possible");
    return 0;
}

Sunday, January 15, 2012

sum using recursive function


/* A program to find the sum of given non-negative integer numbers using a recursive function */

#include<stdio.h>

int sum(int a)
{
    scanf("%d",&a);
    if(a<=0)
        return a; /*can return zero value as well*/
    else
        return(a+sum(a));
}
int main()

{
    int a;
    printf("enter numbers\n");
    printf("sum= %d",sum(a));
    return 0;

}

Saturday, January 14, 2012

Vowel count


/*A program to  Count vowel*/

#include<stdio.h>

int count_vowels(char []);
int check_vowel(char);

main()
{
char array[100];
int c;

printf("Enter a string\n");
gets(array);

c = count_vowels(array);

printf("Number of vowels: %d\n", c);

return 0;
}

int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;

do
{
d = a[c];

flag = check_vowel(d);

if ( flag == 1 )
count++;

c++;
}while( d != '\0' );

return count;
}

int check_vowel(char a)
{
if ( a >= 'A' && a <= 'Z' )
a = a + 'a' - 'A'; /* Converting to lower case */

if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;

return 0;
}