Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Applied Programming TCU Engineering z = ax^2 + bxy + cy^2 + dx + ey + f, the mat

ID: 3683490 • Letter: A

Question

Applied Programming TCU Engineering z = ax^2 + bxy + cy^2 + dx + ey + f, the matrix implementation will be [z_1 z_2 z_3 ... z_N] = [x^2_1 x_1y_1 y^2_1 x_1 y_1 1 x^2_2 x_2y_2 y^2_2 x_2 y_2 1 ... ... ... ... ... ... x^2_N x_Ny_N y^2_N x_N y_N 1][a b c d e f]. The least-squares solution can again be determined by x = [a b c d e f] where x = (A^TA)^-1A^T. However, in this case the y-vector is N times 1 and holds the z-values from (x, y, z), x-vector is 6 times 1 and holds the least-squares fit quadratic parameters, and A is an N times 6 matrix of rank N and is populated using the x- and y-values from (x, y, z) that are to be quadratically fit. The code for the two dimensional quadratic fit has been provided as an example. The change required to this code is that your algorithm for the calculation of the pseudo-inverse must be implemented. Assignment: Write program scripts with supporting functions to implement your own least-squares estimation algorithms outlined above for fitting the data generated by the two different routines for one-dimensional data. Use the provided functions to generate the data to be used in the fit process. Characterize the performance of the algorithms by presenting the fit parameters in a table. You know what the answer should be by examining the routines to generate the data. Also plot the least-squares model along with the data on the same plot. Plot the error of the fit approximation by using the difference from each data point and calculate the mean and standard deviation of the error. Graph the error values and plot them with a line showing the standard deviation of the error. Remember to appropriately label the axes and title the plot.

Explanation / Answer

//code in C language

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include<conio.h>

double **accepting_data(char *name);

void freeData(double **data);

double *calcofcoeffs(double **data);

int main()

{

    char name[100];

    double **data;

    double *result;

     printf("Please Enter The Name : ");

    scanf("%99s", name);

     data = accepting_data(name);

    result = calcofcoeffs(data);

     printf(" ");

    printf(" A = %.20lf B = %.20lf Sigma A = %.20lf Sigma B = %.20lf", result[0], result[1], result[2], result[3]);

     freeData (data);

    free (result);

     return 0;

}

double **accepting_data(char *name)

{

  FILE *userfile; // file declaration.

    double **data; // double array

    int i, j; //declares loop variables.

    int nrows; //declares a variable that tracks the row number.

    int ncolumns; // the number of columns of data declaration and inilization

     //allocating memory using malloc for the array to store the data from the file.

    data = (double**)malloc(10000*sizeof(double*));

    for(i=0; i<10000; i++)

        {

            data[i] = (double*)malloc(4*sizeof(double));

        }

     userfile = fopen(name,"r");

    //initialising all the array elements to zero

    for(i=0; i<10000; i++)

        {

            for(j=0; j<4; j++)

            {

                data[i][j] = 0;

            }

        }

    if (userfile==NULL) //check to see if file was found.

        {

            printf("File not found!.");

            return data;

        }

        nrows=0;

        ncolumns=4;

    while (ncolumns==4 && nrows<10000);

    {

        ncolumns = fscanf(userfile, "%lf %lf %lf %lf ", &data[nrows][0], &data[nrows][1], &data[nrows][2], &data[nrows][3]);

        nrows++;

    }

    fclose(userfile);

    printf("File read ");

    return data;

}

void freeData(double **data)

{

    int i;

    for (i=0; i<10000; i++)

    {

        free(data[i]);

    }

    free (data);

    return;

}

double *calcofcoeffs(double **data)

    //declaring and initialising the variables used for summations

  int i;

    double x;

    double y;

    double xy;

    double xx;

    double one;

    double sigmai2;

    //declaring variables to be given as final results.

    double A;

    double B;

    double sigmaA;

    double sigmaB;

    //declaring a pointer to be used in int main to display final results.

    double *result;

    result = (double*)malloc(4*sizeof(double));

    i=0;

    x=0;

    y=0;

    xy=0;

    xx=0;

   >

    //a while loop that reads through the data to calculate the summations

        while(i<10000 && data[i][3]!=0)

        {

                    sigmai2 = data[i][3]*data[i][3];//making a more simple shorthand for calculation

            // summations used in working out coeffs A and B.

            y+=(data[i][1]/sigmai2);

            xx+=((data[i][0]*data[i][0])/sigmai2);

            x+=(data[i][0]/sigmai2);

            xy+=((data[i][0]*data[i][1])/sigmai2);

            one+=(1/sigmai2);

            i++;

        }

    //working out coeffs A and B.

    A = ((y*xx)-(x*xy))/((one*xx)-(x*x));

    B = ((one*xy)-(x*y))/((one*xx)-(x*x));

    //working out errors in coeffs.

    sigmaA = sqrt((xx)/((one*xx)-(x*x)));

    sigmaB = sqrt((one)/((one*xx)-(x*x)));

    //output the values required.

    result[0] = A;

    result[1] = B;

    result[2] = sigmaA;

    result[3] = sigmaB;

    return result;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote