Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program should be written in the laguage C. The technique of interpolation

ID: 3553544 • Letter: T

Question

This program should be written in the laguage C.




The technique of interpolation can be used for estimating values between data points. The simplest method of interpolation is called linear interpolation, shown in the following figure, this case, two adjacent points are connected with a straight line. The value of y for any x between two adjacent points (xi, yi) and (xi+1 , yi+1) is given by the following equation: y = yi + yi + 1 - yi/xi+1 - xi(x - xi) In a linear interpolation, a straight line first-order polynomial is used to connect two adjacent points. For a smoother interpolation, a second or third-order polynomial (or even cubic splines) can be used to interpolate values between every two points. These latter techniques are outside the scope of this assignment. For purposes of this assignment, let's initialize our data set to contain the following points. (Later we may modify this program to read data points from an input file.) We'll store the time and distance values in two separate parallel arrays. Initialize time and distance arrays to the above values using initialization lists. Ask the user for some number of seconds between 0 and 18 inclusive. This should be a floating point data type, either float or double. If the user inputs a value outside that range, your program should print an error message and give the user the chance to enter another number. Determine where on the time scale the inputted number resides. For example, if the user inputs the number 11, that resides between the time values of 10 and 12. Calculate the interpolated result for y (meters) using linear interpolation and print the result on the screen. Give the user the chance to enter another number if desired. Besides the main () function, your program should, at minimum, contain the following function. Note that the exact specifications for the input parameters were deliberately not provided as part of the exercise. double getDistance (, , , Cnumber looking for>); This function will first scan through the time array to determine which points constitute the "end points" with respect to the calculation, i.e., the (xi, yi) and (Xi+1, yi+1) points in the above equation. It will then calculate the interpolated value ofy according to the equation above, and return the result as a double. Note that because of the way the formula works, if the user enters a value that is exactly equal to a time value in the array, the function will just return the corresponding y (meters) value. This includes the endpoints. For example, if the user enters an 18, the function should return the value 18. If the user inputs 0, just return a 2. The program should be able to accept multiple inputs from the user without having to stop and restart the program every time. After a given distance value is reported, have the program ask the user if he/she wants to continue. If yes, then get another time value from the user. If not, then exit the program.

Explanation / Answer

#include <stdio.h>


float getDistance(float t[],float y[],int n,float time)

{

float ans=0;

int i=0;

int value;

for(i=0;i<n;i++)

{

if(t[i]==time)

ans=y[i];

else if(t[i]<time && t[i+1]>time)

{

printf("Range is between %f and %f ",t[i],t[i+1]);

//value =i;

ans=y[i]+(y[i+1]-y[i])*(time-t[i])/(t[i+1]-t[i]);

break;

}

}


return ans;

}


int main() {

// your code goes here

float t[] = { 0,3,5,8,10,12,15,18};

float y[] = { 2,5.2,6,10,10.4,13.4,14.8,18};

float time;

int ask=1;

while(ask)

{

printf("Please enter the time value between 0 and 18 ");

scanf("%f",&time);

while(time<0 || time>18)

{

printf("Out of range..Enter again. ");

scanf("%f",&time);

}


float ans=getDistance(t,y,8,time);

printf("the interpolated value is %f ",ans);

printf("press 1 to find another value,else press 0 ");

scanf("%d",&ask);

}


return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote