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 25: Python, Graphing, 3D Visualization and Inertial Measurement

Python Arduino Code int trigPin=13; //Sensor Trig pin connected to Arduino pin 13 int echoPin=11; //Sensor Echo pin connected to Arduino pin 11 float pingTime; //time for ping to travel from sensor to target and return float targetDistance; //Distance to Target in inches float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees. void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(trigPin, LOW); //Set trigger pin low delayMicroseconds(2000); //Let signal settle digitalWrite(trigPin, HIGH); //Set trigPin high delayMicroseconds(15); //Delay in high state digitalWrite(trigPin, LOW); //ping has now been sent delayMicroseconds(10); //Delay in low state pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds pingTime=pingTime/1000000; //convert pin...

Day 19: Project 2

Our project was a test stand for rocket motors Using an LCD screen, we could choose a display mode, and it would display the selected value. Unfortunately, we were unable to calibrate the pressure sensor, so all we ended up with was a load cell. We used the HX711 library for this. The LCD screen displayed the maximum force experienced during the test Code for MotorSensors.h #ifndef _MOTORSENSORS_ #define _MOTORSENSORS_ #include <LiquidCrystal.h> //Include Liquid Crystal Library #include <HX711.h> //Include HX711 Load Cell library #include "HX711.h" //Include Load Cell header file #include <math.h> //Include math library #define calibration_factor -8815 //set calibration factor #define DAT 3 //define DAT pin #define CLK 2 //define CLK pin #define pressurePin A1 #define tempPin A2 HX711 scale(DAT,CLK);  LiquidCrystal lcd(8,9,4,5,6,7); //Define LCD pins //Initialize variables float maxkg=0;  float currentval=0; float maxforce=0; char mo...