Skip to main content

Day 10: Statistics and Header Files

Software: Statistics and Header Files

Statistical Measurements

Important functions include maximum, minimum, average, and median.

Example 1.1
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the average or mean value of an */
/* array x with n elements. */
double mean(double x[],int npts)
{ /* Declare and initialize variables. */
int k;
double sum=0;
/* Determine mean value. */
for (k=0; k<=npts-1; k++)
sum += x[k];
/* Return mean value. */
return sum/n;

}

Sum was initialized to zero in declaration statement

Example 1.2
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the median value in the sorted */
/* array x with npts elements. */
double median(double x[],int npts)
{
/* Declare variables. */
int k;
double median_x;
/* Determine median value. */
k = floor(npts/2);
if (n%2 != 0)
median_x = x[k];
else
median_x = (x[k-1] + x[k])/2;
/* Return median value. */
return median_x;

}

Variance and Standard Deviation

Variance is one of the most important statistical measurements for a set of data. 
Figure 1.1
Random number sets
In both datasets, mean would be around 3, but data2 has more variance. 
Figure 1.2
Variance equation
xk-mu is difference between xk and mean aka deviation of xk from mean 
value squared so value is always positive
Standard deviation is square root of variance

Example 1.3
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the variance of an array x */
/* with npts elements. */
double variance(double x[],int npts)
{
/* Declare variables and function prototypes. */
int k;
double sum=0, mu;
double mean(double x[],int npts);
/* Determine variance. */
mu = mean(x,npts);
for (k=0; k<=n-1; k++)
sum += (x[k] - mu)*(x[k] - mu);
/* Return variance. */
return sum/(npts-1);
}
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the standard deviation of an array x */
/* with npts elements. */
double std_dev(double x[],int npts)
{
/* Declare function prototypes. */
double variance(double x[],int npts);
/* Return standard deviation. */
return sqrt(variance(x,npts));

Practice Problems

a. Write a function called rand_float using function prototype that uses the rand function to generate random floating point number between a specified range of values (in problem 1A for homework)
double rand_float(double interval_start, double interval_end);
b. Write program that uses rand_float function to generate sequences of random floating-point values between 4 and 10. Then compare the computed mean and variance with the computed theoretical values. As you use more random numbers, the computed values and the theoretical values should become closer. (1a for homework).

Custom Header File

To facilitate use of functions, they can be put in a custom header file called stat_lib.h and use a preprocessor directive to include the header file. 

Homework

1. Complete the random number problem but with all four parts:

a. Write a program that uses the rand_float function developed previously to generate sequences of random floating-point values between 4 and 10. Then compare the computed mean and variance with the computed theoretical values. As you use more and more random numbers, the computed values and the theoretical values should become closer.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

double rand_float(double interval_start, double interval_end);

double rand_float(double interval_start, double interval_end)
{
  float mynum=((float)rand())/(float)RAND_MAX;
  float diff=interval_end - interval_start;
  float r=mynum*diff;
  return interval_start + r;
}

int main(void)
{
  float mu;
  srand(time(NULL));
  for(int k=1; k<=20; k++)
  {
    printf("%1.3f",rand_float(4, 10));
    printf("\n");
  }
  return 0;
}

Comments

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 6: Analog Sensors | Functions and Modularity

Functions Functions are sets of statements that typically perform operation or compute value. They can help to make programs more accessible and usable for non-programmers e.g. create function that allows user to type "go forward" and move a robot forward. Modules -Functions can be split up into modules, "divide and conquer" -Each module has specific purpose, can be written and tested separately -Smaller than complete solution, therefore testing is easier -Can be used in new problem solutions without being retested -Reduces overall length of program -Allows for increased collaboration; modules can be worked on in parallel Debugging Longer Programs Use a compiler that gives meaningful information about errors. Adding comments around some sections of code can allow for better focus on other parts of the program. Test complicated functions by themselves. Programmer Defined Functions Execution of program always begins with main function. Additional ...

Day 3: Linear Interpolation, Mathematical Functions, and Arduino Buttons

Figure 1.1 Linear interpolation  assumes that a straight line joins two points f(a) and f(c), and that the value of f(b), where b lies between a and c, lies on this line. Figure 1.2 Cubic spline interpolation  is when the points f(a) and f(c) are joined by a cubic polynomial, and the value of f(b), a function of b, which lies between points a and c, lies on this curve. Formula Assumes a<b<c f(b)=f(a)+((b-a)/(c-a))[f(c)-f(a)] Example 1 The data was given, shown in Figure 1.1 at right. Figure 2.1 Example 2 In the example on the left, we used linear interpolation to find the freezing temperature of certain salinities of water. 1. Problem Statement Use linear interpolation to determine the freezing temperature of water with a certain salinity. 2. Input/Output Description Inputs: first salinity, second salinity, first freezing temperature, second freezing temperature, new salinity. Output: new freezing temperature 3. Hand Example See...