| Figure 1.1 |
| Figure 1.2 |
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 |
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 |
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
Post a Comment