"C" Program Source Code Example Using Curve Fit Algorithms

  • Return to Main Menu
  • /* FILE NAME: example.c */

    /* DESCRIPTION: This "C" program demonstrates the use of algorithms developed using results of curve fit program CurvFitXYZ. */

    #include "stdio.h"
    #include "math.h"

    main ()
    {

    /* Coefficients from output data file (coeff_out) of curve fit program. */

    /* Coefficients for polynomial form equation y = f(x,z)*/
    /* 4th order for x and z variation */

    double coeff1[16] = {
    3.000000e+000, 1.156667e+000, -1.175000e-002, 4.583333e-005, 2.811667e+000,
    -1.126667e-001, 1.322917e-003, -4.895833ee-006, -3.525000e-002, 1.445833e-003,
    -1.734375e-005, 6.510417e-008, -1.416667ee-004, 3.541667e-006, -3.385417e-008,
    1.302083e-010};

    /* Coefficients for exponential form equation y = a(b)^x */
    /* 4th order for z variation */

    double coeff2[8] = {
    1.248455e-001, -1.617978e-003, 8.526417e-005, -3.889522e-007, -6.887188e-003,
    1.782482e-003, -3.024476e-005, 1.882200e-007};

    /* Coefficients for power form equation y = a(x)^b */
    /* 4th order for z variation */

    double coeff3[8] = {
    1.430039e+000, -4.103986e-003, 5.521120e-005, -2.075958e-007, -7.964598e-001,
    1.034717e-002, -1.595715e-004, 1.121378e-006};

    /* Coefficients for exponential form equation y = a(e)^kx */
    /* 4th order for z variation */

    double coeff4[8] = {
    1.434549e-001, -2.552266e-003, 1.054168e-004, -5.303089e-007, -9.354947e-003,
    1.973746e-003, -3.454836e-005, 2.172505e-007};

    double base; /* Base for exponent */
    double e_Nap; /* Natural logarithm, Napierian e value */
    double kterm; /* Exponent multiplier of Napierian e */
    double aterm; /* Exponential or power equation term a */
    double bterm; /* Exponential or power equation term b */
    double xval; /* Value of independent variable x */
    double yval; /* Value of dependent variable y */
    double zval; /* Value of independent variable z */
    int equat; /* Type equation */
    unsigned char ans; /* Answer to query */

    ans = 'y';
    base = 10.0;
    e_Nap = 2.7182818284;

    printf ("ENTER THE TYPE EQUATION YOU WISH TO CURVE FIT.\n");
    printf ("FOR POLYNOMIAL FORM, y = ax + b INPUT 1\n");
    printf ("FOR EXPONENTIAL FORM, y = a(b)^x, LOG BASE 10, INPUT 2\n");
    printf ("FOR POWER FORM, y = a(x)^b, LOG BASE 10 INPUT 3\n");
    printf ("FOR EXPONENTIAL FORM, y = a(e)^kx, LOG BASE e, INPUT 4\n");
    printf ("ENTER TYPE ? ");
    scanf ("%i", &equat);
    printf ("equat = %i\n", equat);

    while((ans == 'y') || (ans == 'Y')) {

    printf ("INPUT INDEPENDANT VARIABLE (xval) = ");
    scanf ("%lf", &xval);
    printf ("INPUT INDEPENDANT VARIABLE (zval) = ");
    scanf ("%lf", &zval);

    if (equat == 1)
    {

    /* Algorithm for polynomial form equation y = f(x,z) */
    /* 4th order for x and z variation */

    yval = coeff1[0] + coeff1[1] * zval + coeff1[2] * pow(zval, 2.0) + coeff1[3] * pow(zval, 3.0) + (coeff1[4] + coeff1[5] * zval + coeff1[6] * pow(zval, 2.0) + coeff1[7] * pow(zval, 3.0)) * xval + (coeff1[8] + coeff1[9] * zval + coeff1[10] * pow(zval, 2.0) + coeff1[11] * pow(zval, 3.0)) *
    pow(xval, 2.0) + (coeff1[12] + coeff1[13] * zval + coeff1[14] * pow(zval, 2.0) + coeff1[15] *
    pow(zval, 3.0)) * pow(xval, 3.0);
    }

    if (equat == 2)
    {

    /* Algorithms for exponential form equation y = a(b)^x */
    /* 4th order for z variation */

    aterm = coeff2[0] + coeff2[1] * zval + coeff2[2] * pow(zval, 2.0) +
    coeff2[3] * pow(zval, 3.0);
    bterm = coeff2[4] + coeff2[5] * zval + coeff2[6] * pow(zval, 2.0) +
    coeff2[7] * pow(zval, 3.0);
    yval = pow(base, aterm) * pow(pow(base, bterm), xval);
    printf ("aterm = %e\n", aterm);
    printf ("bterm = %e\n", bterm);
    }

    if (equat == 3)
    {

    /* Algorithms for power form equation y = a(x)^b */
    /* 4th order for z variation */

    aterm = coeff3[0] + coeff3[1] * zval + coeff3[2] * pow(zval, 2.0) +
    coeff3[3] * pow(zval, 3.0);
    bterm = coeff3[4] + coeff3[5] * zval + coeff3[6] * pow(zval, 2.0) +
    coeff3[7] * pow(zval, 3.0);
    yval = pow(base, aterm) * pow(xval, bterm);
    printf ("aterm = %e\n", aterm);
    printf ("bterm = %e\n", bterm);
    }

    if (equat == 4)
    {

    /* Algorithms for exponential form equation y = a(e)^kx */
    /* 4th order for z variation */
    aterm = coeff4[0] + coeff4[1] * zval + coeff4[2] * pow(zval, 2.0) +
    coeff4[3] * pow(zval, 3.0);
    bterm = coeff4[4] + coeff4[5] * zval + coeff4[6] * pow(zval, 2.0) +
    coeff4[7] * pow(zval, 3.0);
    kterm = log(pow(base, bterm));
    yval = pow(base, aterm) * pow(e_Nap, kterm * xval);
    printf ("aterm = %e\n", aterm);
    printf ("kterm = %e\n", kterm);
    }

    printf ("VALUE OF DEPENDENT VARIABLE (yval) = %e\n", yval);

    printf ("TO CHECK ADDITIONAL POINTS INPUT y (yes) or n (no) ?");
    scanf ("%s", &ans);

    }
    return 0;
    }