Skip to main content

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 Figure 2.1
4. Program Decomposition
1) Read the first and second points and the new salinity.
2) Compute new freezing temperature.
3) Print new freezing temperature.



Linear Interpolation Program for Example 2

/* This program uses linear interpolation to */
/* compute the freezing temperature of seawater. */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double a, f_a, b, f_b, c, f_c,tempc;
int outputtype;
/* Get user input from the keyboard. */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new salinity: \n");
scanf("%lf",&b);
// Use linear interpolation to compute new freezing temperature.
f_b = f_a + (b-a)/(c-a)*(f_c - f_a);
printf("Choose output type. Fahrenheit is 1, Celcius is 2\n");
scanf("%f",&outputtype);
switch(outputtype)
{
case 1:
printf("New freezing temperature in degrees F: %4.1f \n",f_b);
break;
case 2:
tempc=(5/9)*(f_b-32);
printf("New freezing temperature in degrees F: %4.1f \n",tempc);
break;
}
return 0; /* Exit program. */

}


Figure 3.1
Elementary Math Functions in C
There are several elementary math functions that C can perform.
  • fabs(x) finds absolute value of x
  • sqrt(x) finds the square root of x
  • pow(x,y) find x to the y power
  • ceil(x) rounds x up to the nearest integer
  • floor(x) rounds x down to the nearest integer
  • exp(x) finds e to the x power
  • log(x) finds the natural log of x
  • log10(x) finds the log of x to base 10
Trigonometric Functions in C
Trigonometric functions in C use radians.
  • sin(x) finds the sine of x
  • cos(x) finds the cosine of x
  • tan(x) finds the tangent of x
  • asin(x) finds inverses sine of x
  • acos(x) finds inverse cosine of x
  • atan(x) finds inverse tangent of x
  • atan2(x,y) finds inverse tangent of y/x


Figure 3.2

Figure 4.1

Example 3: Open Jet
In this example, we used given formulas for velocity and acceleration and plugged a value for time into this formula.
1. Problem Statement
Compute the new velocity and acceleration of an aircraft after a change in power level,
2. Input/Output Description
Input: time
Outputs: velocity, acceleration
3. Hand Example
See Figure 4.1
4. Program Decomposition Outline
1) Read new time value.
2) Compute velocity and acceleration
3) Print new velocity and acceleration
5. Generate Code
#include <stdio.h>
#include <math.h>
int main(void){
  float t,v,a;
  printf("Enter time\n");
  //Take time as input
  scanf("%f",&t);
  //Compute velocity
  v=0.00001*pow(t,3)-0.00488*pow(t,2)+0.75795*t+181.3566;
  //Compute acceleration
  a=3-0.000062*pow(v,2);
  //Print velocity and acceleration
  printf("Velocity: %4.3f \n Acceleration: %4.3f \n",v,a);
  return 0;

}
6. Test Code









Homework
 




2. Print in Centigrade
//Seawater Freezing Temperature
/* This program uses linear interpolation to */
/* compute the freezing temperature of seawater. */
#include <stdio.h>
#include <math.h>
int main(void)
{
  /* Declare variables. */
  double a, f_a, b, f_b, c, f_c,tempc;
  int outputtype;
  /* Get user input from the keyboard. */
  printf("Use ppt for salinity values. \n");
  printf("Use degrees F for temperatures. \n");
  printf("Enter first salinity and freezing temperature: \n");
  scanf("%lf %lf",&a,&f_a);
  printf("Enter second salinity and freezing temperature: \n");
  scanf("%lf %lf",&c,&f_c);
  printf("Enter new salinity: \n");
  scanf("%lf",&b);
  // Use linear interpolation to compute new freezing temperature.
  f_b = f_a + (b-a)/(c-a)*(f_c - f_a);
  printf("Choose output type. Fahrenheit is 1, Celcius is 2\n");
  scanf("%i",&outputtype);
  //Based on user input, print output in either fahrenheit or    centigrade
  switch(outputtype)
  {
   case 1:
     printf("New freezing temperature in degrees F: %4.3f \n",f_b);
     break;

   case 2:
     tempc=(5/9)*(f_b-32);
     printf("New freezing temperature in degrees C: %4.3f     
     \n",tempc);
     break;
  }
  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...