sample prog

Upload: srikaanth06

Post on 05-Apr-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Sample Prog

    1/33

    Sample Programs -1

    1.To fit a straight line through a given set of data points data file- fit.data2. Interpolation of evenly spaced data3. Newton's divided difference interpolation____________________________________

    ______data file-data.dat4.Cubic Spline interpolation____________________________________________________

    ______data file- data.dat

    cubic.dat5. Interpolation using the Lagrange scheme6. A two parameter nonlinear fit using chisquare minimization___________________

    ______data file-non-linear.data7.To fit a function of the form f(x)=a*g(x) + b*h(x) through the given set of data____data file- gen-linear.data

    Sample Programs -21.To find the zero of f(x) using false position method.2.Find the zero of the function f(x) using the secant method3.Newton-Raphson method to find the zero of the function f(x)4.Newton-Raphson method to find the zero of the function f(x) when the zero is of higher order.

    5.To find the zero of a function f(x) by the mid point rule.6.Use false position method to find the zero of the function f(x) when f(x)= 0 can be written as x=g(x)7.Bairstows method to find the roots of a polynomial____________________________

    ______Data file- coeff.data

    Sample Program - 31. Solution of a set of linear equations by elimination method.2. To find the inverse of a matrix using LU decomposition.3. Solution of a set of linear equations by pivoted elimination method4. Solution of a set of linear equation by LU decomposition.

    Sample Programs - 4

    1.To compute the integral of a function using Gauss quadrature method.2. Interpolation by Newton's divided difference formula_______________________________Datafile- data.dat3. Find the solution of a system of non-linear equations4. To compute the integral of a function using Simpsons rule5. To compute the integral of a function using mid point rule

    ***********************************************************************************************************************

    Sample Programs -1***********************************************************************************************************************

    1.To fit a straight line through a given set of data points___________________________data file- fit.data----------------------------------------------------------------------------------------------------------

    1.000000 4.1000001.5200000 7.8400002.100000 9.0000002.300000 11.000003.200000 12.400000

  • 7/31/2019 Sample Prog

    2/33

    3.500000 13.9000004.200000 15.7000004.500000 16.0000005.560000 18.5000005.700000 18.9000006.000000 20.2999996.220000 21.0000017.000000 24.0600007.527000 22.5760008.130000 26.3720008.645000 29.5999999.210000 29.260019.500000 32.80000010.000000 31.000000

    /* To fit a straight line through a given set of data points */

    /* Standard Headers */#include#includemain(){

    /* Variable Declarations and Initialization */

    int i,j,n;float x1,*x,*y,df,df1,xl,a,b,y1;float S=0.0,Sx=0.0,Sy=0.0,Sxx=0.0,Syy=0.0,Sxy=0.0,D,chisq;FILE *FP,*fp1;n=125;

    /* Allocation of memory */x=(float*) malloc(n);y=(float*) malloc(n);

    /* Reading the data to be modelled */fp1=fopen("fit.data","r");i=0;

    while(!feof(fp1)){fscanf(fp1,"%f" "%f",&x1,&y1); x[i]=x1;y[i]=y1;printf("%d %f\n",i,x[i]);

    i++;}fclose(fp1);n=i-2;

    /* Calculating the coefficients "a" and "b" of the function f(x)=ax+b */for(i=0;i

  • 7/31/2019 Sample Prog

    3/33

    chisq=chisq+(a*x[i]+b-y[i])*(a*x[i]+b-y[i]);}printf(" %d xhisq = %f Standard error = %f\n",n-1,chisq, sqrt(chi

    sq/(n-1)));

    printf(" sigma_a = %f sigma_b = %f\n",S/D,Sxx/D);

    }

    2. Interpolation of evenly spaced data---------------------------------------

    /* Interpolation of evenly spaced data */

    /* Standard Headers */#include#includemain(){

    /* Declaration of the variables */int i,j,n;float x1,x[15],f[15],y,df,df1,h,s;FILE *FP;

    /* Here we create a data to be interpolated. this part can bereplaced with an "fscanf" while reading from a file */h=0.5;i=0;for(x1=1.0;x1

  • 7/31/2019 Sample Prog

    4/33

    fprintf(FP,"%f %f \n",x1, y);}fclose(FP);

    }

    3. Newton's divided difference interpolation_______________________________________data file-data.dat-----------------------------------------------------------------------------------------------------0.60000002 0.5188236240.75000002 0.7188236241.05928254 0.6374183891.11524534 0.3105729221.49329114 0.6345862751.84503245 0.1952163432.14226007 0.3588853482.39050388 -0.1365404282.72717857 -0.08725024012.80259895 -0.00997328665

    /* Newtion's divided difference interpolation /*

    /* Standard Headers */

    #include#includemain(){

    /* Declaration of variables */int i,j,n;float x1,x[15],f[15],y,df,df1,dx;FILE *FP;

    /* Open the data file to be interpolated */FP=fopen ("data.dat","r");

    /* Read the data till the end of the file */i=0;while(!feof(FP)){fscanf(FP,"%f %f \n",&x[i],&f[i]); i++;}fclose(FP);

    n=i-1;

    /* construct the divided difference table */for(j=0;j

  • 7/31/2019 Sample Prog

    5/33

    /* Interpolate between the first four points */FP=fopen ("newton.dat","w");dx=(x[3]-x[0])/15.0;for(x1=x[0];x1

  • 7/31/2019 Sample Prog

    6/33

    int i,j,n;float x1,x[16],f[16],e[16],d[16],h[16],r[16],z[16],y,df,dx,xs;FILE *FP,*fp1;

    /* Read the file to be interpolated */fp1=fopen("data.dat","r");i=0;while(!feof(fp1)){fscanf(fp1,"%f" "%f",&x[i],&f[i]);

    i++;}fclose(fp1);n=i-1;printf("%d\n",n);

    /* Calculating the coefficients of the polynomial forevery interval */

    for(i=0;i

  • 7/31/2019 Sample Prog

    7/33

    (-f[i]/d[i]+z[i]*d[i]/6.0)*(x1-x[i+1]);fprintf(FP,"%f %f \n",x1, y);}

    }fclose(FP);

    }5. Interpolation using the Lagrange scheme------------------------------------------

    /* Interpolation using the Lagrange scheme */

    /* Standard Headers */

    #include#includemain(){

    /* Variable declarations */

    int i,j,n;float x1,x[15],f[15],y,df,df1,xl;FILE *FP,*fp1;

    /* Read the file to be interpolated */fp1=fopen("tointer.data","r");i=0;while(!feof(fp1)){fscanf(fp1,"%f" "%f",&x[i],&f[i]);

    i++;}fclose(fp1);n=i-2;

    /* Interpolation using the nested Lagrange form */for(i=0;i

  • 7/31/2019 Sample Prog

    8/33

    ------------------------------------------------------------------------------------------------------0.000000 4.3489000.200000 2.8545990.400000 2.5725840.600000 1.9384920.800000 1.3268751.000000 1.0187191.200000 0.9787891.400000 0.7536491.600000 0.5001911.800000 0.3685442.000000 0.3012582.200000 0.3026942.400000 0.2085782.600000 0.1956562.800000 0.121445

    /* A two parameter nonlinear fit using chisquare minimization */

    /* Standard Headers */#include#includemain()

    {/* Variable declarations and initializations */int i,j,n;float x[25],y[25],D[2][2],d[2],a1,a2;float S=0.0,Sx=0.0,Sy=0.0,Sxx=0.0,Syy=0.0,Sxy=0.0,DD,L;float chisq,chisq_old;FILE *FP,*fp1;

    /* Read the data to be fitted with */fp1=fopen("non-linear.data","r");i=0;while(!feof(fp1))

    {fscanf(fp1,"%f" "%f",&x[i],&y[i]);i++;}fclose(fp1);n=i-2;

    /* read the initial guesses for the parameters from the screen */scanf("%f" "%f",&a1,&a2);

    /* Calculate the chisquare with the guess value */chisq=0.0;for(i=0;i0.01)

  • 7/31/2019 Sample Prog

    9/33

    {

    /* D[2][2] contains the second derivatives of chisquare w.r.t the parameters, andsummed over all points */

    D[0][0]=0.0;D[0][1]=0.0;D[1][0]=0.0;D[1][1]=0.0;d[0]=0.0;d[1]=0.0;

    /* the diagonal elements of the D[2][2] are weighted by (1+L) */for(i=0;i

  • 7/31/2019 Sample Prog

    10/33

    }

    7.To fit a function of the form f(x)=a*g(x) + b*h(x) through the given set of data____data file- gen-linear.data----------------------------------------------------------------------------------------------------------------0.000000 2.9900000.200000 2.7457950.400000 1.3477830.600000 1.5288730.800000 1.9708801.000000 1.0607591.200000 0.7887521.400000 0.9473611.600000 0.5307161.800000 0.7341692.000000 0.6540042.200000 0.2872312.400000 0.5314362.600000 0.1846582.800000 0.305304

    /* Fit a function of the form f(x)=a*g(x)+b*h(x) through the given set of data *

    /

    /* Standard Headers */#include#includemain(){

    /* Declaration of vairables */int i,j,n;float x1,x[25],y[25],df,df1,xl,a,b,al[2][2],be[2];float S=0.0,Sx=0.0,Sy=0.0,Sxx=0.0,Syy=0.0,Sxy=0.0,D;float chisq;FILE *FP,*fp1;

    /* Reading the data to be fitted with */fp1=fopen("gen-linear.data","r");i=0;while(!feof(fp1)){fscanf(fp1,"%f" "%f",&x[i],&y[i]);

    i++;}fclose(fp1);n=i-2;

    /* Initialize the array to calculate, the sum of, the product of derivatives */al[0][0]=0.0;

    al[0][1]=0.0;al[1][0]=0.0;al[1][1]=0.0;be[0]=0.0;be[1]=0.0;

    /* sum the product of the derivatives over all points */for(i=0;i

  • 7/31/2019 Sample Prog

    11/33

    al[1][1]=al[1][1]+1.0/((1.0+x[i])*(1+x[i]));be[0]=be[0]+y[i]*exp(-x[i]);be[1]=be[1]+y[i]/(1+x[i]);

    }

    /* Calculate the determinant of the matrix "a" */D=al[1][1]*al[0][0]-al[0][1]*al[1][0];

    /* Calculate the coefficients "a" and "b".

    a=(al[1][1]*be[0]-al[0][1]*be[1])/D;b=(al[0][0]*be[1]-al[1][0]*be[0])/D;printf("n = %d a = %f b = %f\n",n,a,b);

    /* Calculate the error in the fit. */chisq=0.0;for(i=0;i

  • 7/31/2019 Sample Prog

    12/33

    /s=false_position(x0,x1,&func);printf(" The zero is %f\n",s);}

    /* function to find the zero using false-position rule. */float false_position(x0,x1,function)

    /* declaration of the arguments recieved */float x0,x1;float function(float);{int i,j,n,m;float e,f;float x2;e=1.0;

    /* continue till close to the zero */while(e>0.0001){

    /* Find the new solution by fiding the point of intersection of theline connecting (x0,f(x0)) and (x1,f(x1)) with the x-axis. */

    x2=x1+function(x1)*(x0-x1)/(function(x1)-function(x0));if(function(x2)0){e=fabs((x1-x2)/x2);x1=x2;}

    printf(" %f %f %f\n",e,x2,function(x2));}

    /* return x2 as the zero when the error condition is satisfied */return x2;}

    /* user supplied routine that evaluates f(x).Note that the name "func" can be changed withoutchanging "function" in theroutine that evaluates false-position*/

    float func(float x){float y;y=1.0-x*x+log10(1.0+x);return y;

    }

    2.Find the zero of the function f(x) using the secant method-------------------------------------------------------------

    /* find the zero of the function f(x) using the secant method */

    /* Standard Headers */#include#include

    /* External function that returns the value of f(x) */float func(float);main(){

    /* Variable declarations */float s,x0,x1;float sec();

  • 7/31/2019 Sample Prog

    13/33

    FILE *FP,*fp1;/* the initial guesses. Unlike the bracketing methods these twovalues need not enclose the zero of f(x) */

    scanf("%f" "%f",&x0,&x1);

    /* call the funcion "sec" that computes the zero using secant method.Pass the two initial guess values and the pointer to the external functionthat evaluates f(x) */

    s=sec(x0,x1,&func);printf(" The zero is %f\n",s);}

    /* the function that compute the zero of f(x) using iterations based onsecant method. it recieves two guess values "x0" and "x1" and the pointer tothe function that computes f(x) */

    float sec(x0,x1,function)float x0,x1;float function(float);{int i,j,n,m;float e,f;float x2;e=1.0;

    /* continue the iteration till the error defined as the percentage

    change between two iterations is small */while(e>0.0001){

    /* the new approxiation is obtained from the the present one as thepoint of intersection of the line joining (x0,f(x0)) and (x1,f(x1))with the x-axis */

    x2=(function(x1)*x0-function(x0)*x1)/(function(x1)-function(x0));e=fabs((x1-x2)/x2);

    printf(" %f %f %f\n",e,x2,function(x2));/* always replace x0 with x1 and x1 with the new point */

    x0=x1;x1=x2;

    }

    return x2;}

    float func(float x){float y;y=1.0-x*x+log(1.0+x);return y;

    }

    3.Newton-Raphson method to find the zero of the function f(x)--------------------------------------------------------------

    /* Newton-Raphson method to find the zero of the function f(x) *//* Standard Headers */

    #include#include

    /* Declare the external function to evaluate f(x) and its first derivative*/void func(float,float*,float*);main(){

  • 7/31/2019 Sample Prog

    14/33

    /* Declaration of the variables */float s,x1;float newton();FILE *FP,*fp1;

    /* Initial guess for the zero */scanf("%f",&x1);

    /* call the function to find the zero using newton-raphson iteration.Pass the pointer to the external function that evaluate f(x) and its derivative*/

    s=newton(x1,&func);printf(" The zero is %f\n",s);}

    /* the function that computes the zero of f(x) using newton-raphson interationit recieves the external function to evaluate f(x) as "function" and thecurrent approximation of the zero as "x1" */

    float newton(x1,function)float x1;

    /* the "function" is called with two pointers through which the value of f(x)and its derivatives are updated. Since this function does not "return" anyvariables it is of type "void" */

    void function(float,float*,float*);

    {int i,j,n,m;float e,*f,*deriv;float x2;e=1.0;

    /* continue the iteration till the error defined as the percentagechange between two iterations is small */

    while(e>0.0001){

    /* call the "function" to get the value of f(x) in "f" and itsderivative in "deriv" */

    function(x1,f,deriv);/* the new approxiation is obtained from the Taylor expansion around

    the present one */x2=x1-(*f)/(*deriv);e=fabs((x1-x2)/x2);

    printf(" %f %f %f %f %f\n",e,x2,x1,*f,*deriv);x1=x2;

    }return x2;}

    /* the external function that recives "x" and evaluates f(x) and itsderivative. since "y" and "deriv" are pointers they are passed by reference.This means that any change in their value made here will be reflected in thefunction from which it is called and no explicit "return" statements are require

    d */void func(float x, float *y, float *deriv){*y=1.0-x*x+log(1.0+x);*deriv=-2*x+1.0/(1.0+x);//*y=x-exp(-2.0*x);//*deriv=1+2.0*exp(-2.0*x);}

    4.Newton-Raphson method to find the zero of the function f(x) when the zero is o

  • 7/31/2019 Sample Prog

    15/33

    f higher order.-----------------------------------------------------------------------------------------------

    /* Newton-Raphson method to find the zero of the function f(x) whenthe zero is of higher order, that is when the function and its first derivativeare going to zero*//* Standard Headers */

    #include#include

    /* Declare the external function to evaluate f(x) and its first two derivatives*/

    void func(float,float*,float[]);main(){

    /* Declaration of the variables */float s,x1;float newton();FILE *FP,*fp1;

    /* Initial guess for the zero */scanf("%f",&x1);

    /* call the function to find the zero using a higher order newton-raphson iteration.Pass the pointer to the external function that evaluate f(x) and its derivative*/

    s=newton(x1,&func);printf(" The zero is %f\n",s);}

    /* the function that computes the zero of f(x) using newton-raphson interationit recieves the external function to evaluate f(x) and its derivatives as"function" and the current approximation of the zero as "x1" */

    float newton(x1,function)float x1;

    /* the "function" is called with a pointer and an arraythrough which the value of f(x) and its derivatives are updated.Since this function does not "return" any variables it is of type "void" */

    void function(float,float*,float[]);{int i,j,n,m;float e,*f,deriv[2];float x2;e=1.0;

    /* continue the iteration till the error defined as the percentagechange between two iterations is small */

    while(e>0.000001){

    /* call the "function" to get the value of f(x) in "f" and itsderivatives in the array"deriv" */

    function(x1,f,deriv);/* the new approxiation is obtained from the Taylor expansion up to secondderivative, around the present one */

    x2=x1-(*f)*deriv[0]/( deriv[0]*deriv[0]-(*f)*deriv[1]);e=fabs((x1-x2)/x2);

    printf(" %e %e %e %e %e\n",e,x2,x1,*f,deriv[0]);x1=x2;

    }

  • 7/31/2019 Sample Prog

    16/33

    return x2;}

    * the external function that recieves "x" and evaluates f(x) and itsderivatives. since "y" is a pointer and "deriv" is an array, they are passed byreference.This means that any change in their value made here will be reflected in thefunction from which it is called and no explicit "return" statements are required */

    void func(float x, float *y, float deriv[2]){*y=pow(x,4)-6*pow(x,3)+12*pow(x,2)-10*x+3;deriv[0]=4*pow(x,3)-18*x*x+24*x-10;printf("%f\n",deriv[0]);

    deriv[1]=12*x*x-36*x+24;printf("%f\n",deriv[1]);}

    5.To find the zero of a function f(x) by the mid point rule.------------------------------------------------------------

    /* to find the zero of a function f(x) by the mid point ruleAlso demonstrates the use of pointer to a function */

    /* Standard Headers */#include#include

    /* Declare the external function that evaluates f(x) */float func(float);

    main(){

    /* Variable declarations */float s,x0,x1;float midpoint();FILE *FP,*fp1;

    /* the first guesses that bracket the zero. It isassumed that the function is negative for x0 and positive for x1 */scanf("%f" "%f",&x0,&x1);

    /* Call the function "midpoint" to obtain the zero using midpoint rule.Pass the pointer to the function "func" that evaluate the function at anypoint. This "func" must be declared global , that is outside the main function */

    s=midpoint(x0,x1,&func);printf(" The zero is %f\n",s);}

    /* function to find the zero using midpoint rule. */

    float midpoint(x0,x1,function)/* declaration of the arguments recieved */

    float x0,x1;float function(float);{int i,j,n,m;float e,f;float x2;e=1.0;

    /* continue till close to the zero */

  • 7/31/2019 Sample Prog

    17/33

    while(e>0.0001){

    /* find the midpoint */x2=(x1+x0)/2.0;

    /* if the finction if negative at midpoint replace x0 with x2 */if(function(x2)0){e=fabs((x1-x2)/x2);x1=x2;}

    printf(" %f %f %f\n",e,x2,function(x2));}

    /* return the midpoint value as the zero when the error condition is satisfied */

    return x2;}

    /* user supplied routine that evaluates the function whose zero is to be foundNote that the name "func" can be changed without changin "function" in theroutine that evaluates midpoint */

    float func(float x){float y;y=1.0-x*x+log10(1.0+x);

    return y;

    }

    6.Use false position method to find the zero of the function f(x) when f(x)= 0 can be written as x=g(x)-------------------------------------------------------------------------------------------------------

    /* False position method to find the zero of the function f(x) whenf(x)=0 can be written as x=g(x) *//* Standard Headers */

    #include#include/* Declare the external function to evaluate g(x) */

    float func(float);main(){

    /* Declaration of the variables */float s,x1;float fixedpoint();FILE *FP,*fp1;

    /* Initial guess for the zero */scanf("%f",&x1);

    /* call the function to find the zero using fixed point iteration.Pass the pointer to the external function that evaluate g(x) */

    s=fixedpoint(x1,&func);printf(" The zero is %f\n",s);}

    /* the function that computes the zero of f(x) using fixed point interationit recieves the external function to evaluate g(x) as "function" and thecurrent approximation of the zero as "x1" */

    float fixedpoint(x1,function)

  • 7/31/2019 Sample Prog

    18/33

    float x1;float function(float);{int i,j,n,m;float e,f;float x2;e=1.0;

    /* continue the iteration till the error defined as the percentagechange between two iterations is small */

    while(e>0.0001){

    /* use x=g(x) to get the next approximation to the zero */x2=function(x1);e=fabs((x1-x2)/x2);

    printf(" %f %f %f\n",e,x2,function(x2));x1=x2;

    }return x2;}

    float func(float x){float y;y=(1.0+log10(1.0+x))/x;

    return y;

    }

    7.Bairstows method to find the roots of a polynomial__________________Data file-coeff.data--------------------------------------------------------------------------------------------

    1.25-3.8752.125

    2.75-3.51.0

    /* Bairstows method to find the roots of a polynomial.*/

    /* Standard Headers */#include#includemain(){

    /* Variable Declarations */int i,j,n,m;

    float a[25],b[25],D[2][2],d[2],c[25];float r,s,DD,x;FILE *FP,*fp1;

    /* Read the coefficients of a polynomial from the file "coeff.data" */fp1=fopen("coeff.data","r");i=0;while(!feof(fp1)){fscanf(fp1,"%f",&a[i]);

    i++;

  • 7/31/2019 Sample Prog

    19/33

    }fclose(fp1);

    /* Order of the polynomial */n=i-2;

    /* Initial guess for the coefficients of the first quadratic factor */scanf("%f" "%f",&r,&s);

    m=n;for(i=0;i0.001){if(m>2){b[m]=a[m]; b[m-1]=a[m-1]+r*b[n];for(j=m-2;j>=0;j--){b[j]=a[j]+r*b[j+1]+s*b[j+2];}

    }if(m>2){c[m]=b[m]; c[m-1]=b[m-1]+r*c[n];for(j=m-2;j>=1;j--)

    {c[j]=b[j]+r*c[j+1]+s*c[j+2];}}printf("%d\n",i);D[0][0]=c[2];D[0][1]=c[3];D[1][0]=c[1];D[1][1]=c[2];d[0]=-b[1];d[1]=-b[0];

    DD=D[1][1]*D[0][0]-D[0][1]*D[1][0];printf("%f\n",DD);

    /* Update the coefficients */r=r+(D[1][1]*d[0]-D[0][1]*d[1])/DD;

    s=s+(D[0][0]*d[1]-D[1][0]*d[0])/DD;/* printf("m = %d r = %f s = %f b[0] = %f b[1] = %f \n",m,r,s,b[0],b[1]);*/

    }/* the roots of the quadratic fraction */

    x=r*r+4.0*s;if(x>=0.0){printf("\n");printf ("Solution x = %f x = %f \n",

    (r+sqrt(x))/2, (r-sqrt(x))/2.0);}if(x

  • 7/31/2019 Sample Prog

    20/33

    for(j=0;j

  • 7/31/2019 Sample Prog

    21/33

    x=D[j][k]/D[k][k];for(i=k;i

  • 7/31/2019 Sample Prog

    22/33

    array for the program to work */for(m=0;m

  • 7/31/2019 Sample Prog

    23/33

    printf(" %d %d %f \n", m,j,x );}}

    }

    /* The function that calcualtes the LU deomposed matrix.Note that it receives the matrix as a two dimensional arrayof pointers. Any change made to [D] here will also change itsvalue in the main function. So there is no need of an explicit"return" statement and the function is of type "void". */

    LU(float(*D)[3][3],int n){

    int i,j,k,m,an,am;float x;printf("The matrix \n");for(j=0;j

  • 7/31/2019 Sample Prog

    24/33

    D[2][0]=1.0;D[2][1]=4.0;D[2][2]=2.0;d[0]=7.65;d[1]=13.5;d[2]=16.0;for(j=0;j

  • 7/31/2019 Sample Prog

    25/33

    The function receives a one dimensional array and returns the index correspondingto the maximum value in the array. Note that since this function "returns" a variableit has to have the same "type" as the variable */

    int max(int nm,float s[]){int j,jmax;float smax;jmax=0;

    /* start with the assuption that the first element is the largest */smax=s[0];

    /* go through the array sequentially */for(j=1;jsmax){ jmax=j; smax=s[j];}

    /*return the index corresponding to the largest value to the main program.*/return jmax;

    }}

    4. Solution of a set of linear equation by LU decomposition.

    ------------------------------------------------------------

    /* Solution of a set of linear equation by LU decompostion */

    /* Standard Headers */#include#includemain(){

    /* Variable declarations */int i,j,n,k,m;float D[3][3],d[3],x,s[3],y[3];

    FILE *FP,*fp1;n=2;/* the equation is [D][s]=[d] *//* read the matrix [D] and [d] */

    D[0][0]=3.0;D[0][1]=2.0;D[0][2]=1.1;D[1][0]=6.0;D[1][1]=2.0;D[1][2]=1.;D[2][0]=1.0;D[2][1]=4.0;

    D[2][2]=2.0;d[0]=7.65;d[1]=13.5;d[2]=16.0;

    /* print the matrix elements and the RHS on the screen */for(j=0;j

  • 7/31/2019 Sample Prog

    26/33

    /* eliminate column by column to construct the upper triangular matrix*/for(k=0;k

  • 7/31/2019 Sample Prog

    27/33

    /* define paramter constants */#define pi 4*atan(1.0)#define EPS 3.0e-11

    /* declare the routine that compute the points at which thefunction value to be evaluated and the corresponding weights */

    void gauleg(float x1, float x2, float x[], float w[], int n);

    main(){/* variable declarations */

    FILE *fp1;int i,j,Npoints,n;

    /* also specify the upper and lower limit of the integral */float *w,*x,upper_lmt=1.0,lower_lmt=.0,S;char fname[20];

    printf("\n Here, upper limit=%f; lower limit=%f\n",upper_lmt,lower_lmt);/* Read from the screen the number of points at which the function has to be evaluated */

    printf("\n Enter no of gausleg pts you want:");scanf("%d",&Npoints);

    /* Allocate memory to store the abscissa and the corresponding weigths */w=(float *)malloc((Npoints+2)*sizeof(float));

    x=(float *)malloc((Npoints+2)*sizeof(float));

    n=Npoints;for(i=0;i

  • 7/31/2019 Sample Prog

    28/33

    int m,j,i;double z1,z,xm,xl,pp,p3,p2,p1;

    m=(n+1)/2;xm=0.5*(x2+x1);xl=0.5*(x2-x1);

    /* find m zeros of the Legandere polynomial in the interval -1 to +1 */for (i=1;i

  • 7/31/2019 Sample Prog

    29/33

    while(!feof(FP)){fscanf(FP,"%f %f \n",&x[i],&f[i]); i++;}fclose(FP);

    n=i-1;

    /* construct the divided difference table and store it in atwo dimensional array. The first index is for the order of thedivided diffrence. We store all the divided difference valuesto reatin the freedom for constructing a polynomial startingwith any tabulated value and not necessarily the first one */

    for(i=0;i

  • 7/31/2019 Sample Prog

    30/33

    3. Find the solution of a system of non-linear equations---------------------------------------------------------

    /find the solution of a system of non-linear equations *//* Standard Headers */

    #include#include

    /* Declare the external function to evaluate the functionsf[i] and its first derivatives w.r.t x[j]*/

    void func(float[],float[],float[][]);main(){

    /* Declaration of the variables */float s,x[2];int n;float newton();FILE *FP,*fp1;

    /* number of unknowns */n=2;

    /* read from the terminal the inital guess for each variable */scanf("%f %f",&x[0],&x[1]);

    /* pass the pointer to the external function , the initial guesses and thenumber of unknowns to the routine that find the solution */

    non_line_eqns(x,&func,n);printf(" The zero is %f %f\n",x[0],x[1]);}

    /* the function to find the solution of a set of nonlinar euqations byNR method. The pointer to the external function to evalute the equationsshould be supplied */

    non_line_eqns(x,function,m)int m;float x[m];void function (float[], float[], float[][]);{

    int i,j,n,k,jmax,nm;float D[m][m],d[m],s[m],st,x2[m],xx;int max();float e;n=m-1;e=1.0;for(i=1;i.00001){for(j=0;j

  • 7/31/2019 Sample Prog

    31/33

    /* call the function "max" to get the maximum value in [s] */jmax=max(nm,s);printf("%d %f \n",jmax,s[jmax]);

    /* if the largest element is not in the k'th row , exchange the rows on theLHS and RHS* /

    if(jmax>0){for(i=0;i

  • 7/31/2019 Sample Prog

    32/33

    }

    void func(float x[2], float y[2], float deriv[2][2]){y[0]=-(pow(x[0],2)+x[0]*x[1]-10);y[1]=-(x[1]+3*x[0]*pow(x[1],2)-57);deriv[0][0]=2*x[0]+x[1];deriv[0][1]=x[0];deriv[1][0]=3*pow(x[1],2);deriv[1][1]=1+6*x[0]*x[1];}

    4. To compute the integral of a function using Simpsons rule-------------------------------------------------------------

    /* To compute the integral of a function using simpsons rule */#include #include #include main(){/* Variable declarations */

    FILE *fp1;int i,j,Npoints,n;

    float *x,xp,upper_lmt=1.0,lower_lmt=0.0,S;char fname[20];/* also specify the upper and lower limit of the integral */

    printf("\n Here, upper limit=%f; lower limit=%f\n",upper_lmt,lower_lmt);/* Read from the screen the number of required sub-intervals */

    printf("\n Enter no of pts you want:");scanf("%d",&Npoints);

    /* allocate memory to the pointer to store the intervals */x=(float *)malloc((Npoints+2)*sizeof(float));

    /* divide the whole interval to the number of sub-intervals required */n=Npoints;xp=(upper_lmt-lower_lmt)/Npoints;for(i=0;i

  • 7/31/2019 Sample Prog

    33/33

    {/* variable decalrations */

    FILE *fp1;int i,j,Npoints,n;

    /* also specify the upper and lower limit of the integral */float *x,xp,upper_lmt=1.0,lower_lmt=0.0,S;char fname[20];

    printf("\n Here, upper limit=%f; lower limit=%f\n",upper_lmt,lower_lmt);

    /* Read from the screen the number of required sub-intervals */printf("\n Enter no of pts you want:");scanf("%d",&Npoints);

    /* allocate memory to the pointer to store the intervals */x=(float *)malloc((Npoints+2)*sizeof(float));

    /* divide the whole interval to the number of sub-intervals required */n=Npoints;xp=(upper_lmt-lower_lmt)/Npoints;for(i=0;i