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 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 4: RGB Nightlight | Algorithm Development, Conditional Expressions, Selection Statements

Hardware: RGB Nightlight const int red=11; const int green=10; const int blue=9; const int button=4; boolean lastbutton=LOW; boolean currentbutton=LOW; int ledMode=0; void setup() {   // put your setup code here, to run once:   pinMode(button,INPUT);   pinMode(red,OUTPUT);   pinMode(green,OUTPUT);   pinMode(blue,OUTPUT); } boolean debounce(boolean last) {   boolean current=digitalRead(button);   if(last!=current)   {     delay(5);     current=digitalRead(button);   }   return current; } void setMode(int mode) {   //RED   if(mode==1)   {     digitalWrite(red,HIGH);     digitalWrite(green,LOW);     digitalWrite(blue,LOW);   }   if(mode==2)   {     //orange     digitalWrite(red,HIGH);     analogWrite(green,40);     digitalWrite(blue,LOW);...