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

PLEASE HELP ME i have written a code to find eulers method and the linear regres

ID: 3708232 • Letter: P

Question

PLEASE HELP ME i have written a code to find eulers method and the linear regression. I need to write a code to solve it anatically and then compare that to eulers method and find the percent error i also dont think the linear regression is calculated right i think i only found r not r^2 but i have no idea how to do these things. PLEASE HELP and just use/ revise my code.

This is the prompt the i need help with part 4

This is my code so far

#include<stdio.h>

#include <math.h>

// Prototype of functions

double sum(double [], double [], int n);

double r(double [], double [], int , double , double );

float func(float ,float );

float euler(float ,float ,float ,int );

// Creates a union num

union num

{

int n;

};// End of union

// Creates a structure info

struct info

{

double sumx, sumy,sumxx,sumxy;

double a,b;

};// End of structure

// main function definition

int main()

{

// Creates double array and assigns data

double x[] = {2, 3};

double y[] = {3};

double one[] = {1, 1};

float xn, y0, e, x0 = 2;

int n = 2;

  

// Accepts data from the user

printf("Input n: ");

scanf("%d",&n);

  

printf("Input x0: ");

scanf("%f",&x0);

  

printf("Input xn: ");

scanf("%f",&xn);

  

printf("Input y0: ");

scanf("%f",&y0);

  

// Declares a structure object

struct info aObj;

// Declares a structure pointer

struct info *aPtr;

// Structure pointer points to structure object

aPtr = &aObj;

  

// Calls the function to calculate sum and stores the result in structure data member

aPtr->sumx = sum(x, one, n);

aPtr->sumy = sum(y, one, n);

aPtr->sumxx = sum(x, x, n);

aPtr->sumxy = sum(x, y, n);

  

// Calculates and stores the result in structure data member

aPtr -> a = (n * aPtr -> sumxy - aPtr -> sumx * aPtr -> sumy) / (n * aPtr -> sumxx - aPtr -> sumx * aPtr -> sumx);

aPtr -> b = (aPtr -> sumy * aPtr -> sumxx - aPtr -> sumx * aPtr -> sumxy) / (n * aPtr -> sumxx - aPtr -> sumx * aPtr -> sumx);

  

// Calls the function to do calculation

e = euler(x0, xn, y0, n);

  

printf(" By linear regression, the function is: y = %f x + %f ",aPtr -> a, aPtr -> b);

  

printf(" When x = %f, y = %f ", aPtr -> a * x0 + aPtr -> b, x0);

  

printf(" r = %f ", r(x, y, n, aPtr -> a, aPtr -> b));

  

return 0;

}// End of main function

// Function to calculate sum and return the result

double sum(double x[], double y[], int n)

{

// To store sum

double s = 0.0;

// Loop variable

int i = 0;

// Loops n times

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

// Calculates sum

s = s + x[i] * y[i];

// Returns sum

return s;

}// End of function

// Function to calculate and return result

double r(double x[], double y[], int n, double a, double b)

{

double s0 = 0,s = 0,ybar = 0,total = 0;

// Loop variable

int i = 0;

// Loops n times

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

{

// Calculates sum of y array

total += y[i];

}// End of for loop

  

// Stores the average

ybar = total/n;

  

// Loops n times

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

{

// Calculates power

s0 += pow((y[i] - ybar), 2);

s += pow((y[i] - b - a * x[i]), 2);

}// End of for loop

// Returns the result

return pow((s0 - s) / s0, 0.5);

}// End of function

// Function to calculate the difference

float func(float x, float y)

{

return ((x * x) - (3 * x));

}// End of function

// Euler function

float euler(float x0, float xn, float y0, int n)

{

float x, y, h;

// Loop variable

int i;

x = x0;

y = y0;

h = (xn - x0) / n;

// Displays x0 and y0 value

printf("y(%f) = %6.4f ", x0, y0);

// Loops n times

for(i = 1; i <= n; i++)

{

y = y + h * func(x, y);

x = x0 + i * h;

printf("y(%f) = %6.4f ", x, y);

}// End of for loop

return y;

}

Develop a C-code to meet the requirements below 1) Find the solution of the differential equation below by using Euler's method. The range of x is 2 to 3. dyldx-x2 3x with y-3 at x-2 2) Find the appropriate step size for this specific problem 3) Print all the y values within the range of x-2 to x-3. 4) Solve the differential equation analytically and compare with the Euler's method 5) Obtain a linear regression y-ax+b and show the r value Your C-code must contain the following features: (1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union, (5) use of functions and function calls, (6) formatted output on screen and (7) saving of the same output on a file.

Explanation / Answer

#include<stdio.h>

#include <math.h>

// Prototype of functions

double sum(double [], double [], int n);

double r(double [], double [], int , double , double );

float func(float ,float );

float euler(float ,float ,float ,int );

// Creates a union num

union num

{

    int n;

};// End of union

// Creates a structure info

struct info

{

    double sumx, sumy,sumxx,sumxy;

    double a,b;

};// End of structure

// main function definition

int main()

{

    // Creates double array and assigns data

    double x[] = {2, 3};

    double y[] = {3};

    double one[] = {1, 1};

    float xn, y0, e, x0 = 2;

    int n = 2;

   

    // Accepts data from the user

    printf("Input n: ");

    scanf("%d",&n);

   

    printf("Input x0: ");

    scanf("%f",&x0);

   

    printf("Input xn: ");

    scanf("%f",&xn);

   

    printf("Input y0: ");

    scanf("%f",&y0);

   

    // Declares a structure object

    struct info aObj;

    // Declares a structure pointer

    struct info *aPtr;

    // Structure pointer points to structure object

    aPtr = &aObj;

   

    // Calls the function to calculate sum and stores the result in structure data member

    aPtr->sumx = sum(x, one, n);

    aPtr->sumy = sum(y, one, n);

    aPtr->sumxx = sum(x, x, n);

    aPtr->sumxy = sum(x, y, n);

   

    // Calculates and stores the result in structure data member

    aPtr -> a = (n * aPtr -> sumxy - aPtr -> sumx * aPtr -> sumy) / (n * aPtr -> sumxx - aPtr -> sumx * aPtr -> sumx);

    aPtr -> b = (aPtr -> sumy * aPtr -> sumxx - aPtr -> sumx * aPtr -> sumxy) / (n * aPtr -> sumxx - aPtr -> sumx * aPtr -> sumx);

   

    // Calls the function to do calculation

    e = euler(x0, xn, y0, n);

   

    printf(" By linear regression, the function is: y = %f x + %f ",aPtr -> a, aPtr -> b);

   

    printf(" When x = %f, y = %f ", aPtr -> a * x0 + aPtr -> b, x0);

   

    printf(" r = %f ", r(x, y, n, aPtr -> a, aPtr -> b));

   

    return 0;

}// End of main function

// Function to calculate sum and return the result

double sum(double x[], double y[], int n)

{

    // To store sum

    double s = 0.0;

    // Loop variable

    int i = 0;

    // Loops n times

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

        // Calculates sum

        s = s + x[i] * y[i];

    // Returns sum

    return s;

}// End of function

// Function to calculate and return result

double r(double x[], double y[], int n, double a, double b)

{

    double s0 = 0,s = 0,ybar = 0,total = 0;

    // Loop variable

    int i = 0;

    // Loops n times

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

    {

        // Calculates sum of y array

        total += y[i];

    }// End of for loop

   

    // Stores the average

    ybar = total/n;

   

    // Loops n times

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

    {

        // Calculates power

        s0 += pow((y[i] - ybar), 2);

        s += pow((y[i] - b - a * x[i]), 2);

    }// End of for loop

    // Returns the result

    return pow((s0 - s) / s0, 0.5);

}// End of function

// Function to calculate the difference

float func(float x, float y)

{

    return ((x * x) - (3 * x));

}// End of function

// Euler function

float euler(float x0, float xn, float y0, int n)

{

    float x, y, h;

    // Loop variable

    int i;

    x = x0;

    y = y0;

    h = (xn - x0) / n;

    // Displays x0 and y0 value

    printf("y(%f) = %6.4f ", x0, y0);

    // Loops n times

    for(i = 1; i <= n; i++)

    {

        y = y + h * func(x, y);

        x = x0 + i * h;

        printf("y(%f) = %6.4f ", x, y);

    }// End of for loop

    return y;

}

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