This is the voltage18.txt 0 23 9 -12 19 90 14 0 45 98 34 -23 29 18 5 78 37 76 7
ID: 3718818 • Letter: T
Question
This is the voltage18.txt
0 23 9 -12 19 90 14 0 45 98 34 -23 29 18 5 78 37 76 7 89 D Voltage measurements h C Secure | https://learn.unm.edu/bbcswebdav/pid-3718820-dt-content-rid-25134897-1/courses/XLSVXFUV201810/ece1 31sp18assn8%2819629.pdf Voltage measurements have been made for an electronic circuit in sequence at various times. However, after collecting the data, it is desired to know the voltage at a time that is different from the times at which the data is collected. Write a C program that will read the collected voltage data, the corresponding times, and the time at which the voltage is desired. Make the program compute the desired voltage based on a linear interpolationu values. The program will read the time (1 column) and voltage data (2h column) from a file (called voltagel8.txt), the desired time from the keyboard, and print the interpolated voltage to the screen. the nearest measured Explanation of Equation To interpolate linearly between two given points, (xI, y/) and (x2, y2), you can draw a straight line connecting the points and obtaining a y value on that line for a given x value (somewhere between the x values of the two endpoints). The slope of a line, m, is: m y2 -yl)/(x2 -xl) Therefore, the x distance from xl to the given x value is (x-value -xl). So, the calculated y value is: y-value ((y2-yf)(x2-x1)) (x-value-x1)+ y1 where x-value is the given x value and y-value is the calculated y value Example Using the following sorted data: 0 23 78 89 12 ivate Windows o PC settings to activate Windows Ac 0Explanation / Answer
ScreenShot
-----------------------------------------------------------------------
Program
//HeaderFiles
#include<stdio.h>
#include <stdlib.h>
#include<math.h>
//Function prototypes
int read(double time[],double voltage[]);
void sort(double time[], double voltage[],int size);
int interpolation(double time[], double voltage[], int size,double xValue,double *yValue);
//Main Function
int main()
{
//Variables for array to store file data
double time[200],voltage[200],xValue,yValue;
int size,val;
size = read(time, voltage);
//Prompt user to read the xvalue
printf("Please enter the x-value:");
scanf("%lf",&xValue);
//call interpolation function
val=interpolation(time,voltage,size,xValue,&yValue);
//Check value within the range
if (val == 0) {
printf("Error!!!");
}
else {
printf("yValue=%.2lfmilliVolts ",yValue);
}
return 0;
}
//Read from file and return size
int read(double time[], double voltage[]) {
FILE *fptr;
int size = 0;
double col_one,col_two;
char filename[37] = "C:/Users/deept/Desktop/voltage18.txt";
/* open the file for reading */
fptr = fopen(filename, "r");
//error check
if (fptr == NULL)
{
printf("Cannot open file ");
exit(0);
}
//Read into array
while (!feof(fptr))
{
fscanf(fptr, "%lf %lf", &col_one, &col_two);
time[size] = col_one;
voltage[size] = col_two;
size++;
}
return size;
fclose(fptr);
}
//Sort the function in ascending order
void sort(double time[], double voltage[], int size) {
int i, j;
double temp, temp1;
for (i = 0; i < size; i++)
{
for (j = 0; j < (size - i - 1); j++)
{
if (time[j] > time[j + 1])
{
temp = time[j];
temp1 = voltage[j];
time[j] = time[j + 1];
voltage[j] = voltage[j + 1];
time[j + 1] = temp;
voltage[j + 1] = temp1;
}
}
}
printf("Sorted array is... ");
for (i = 0; i < size; i++)
{
printf("%.2lf and %.2lf ", time[i],voltage[i]);
}
}
//Interpolation equation to find y value
int interpolation(double time[], double voltage[], int size, double xValue, double *yValue) {
int i;
double x1=0,x2=0,y1=0,y2=0;
size = read(time, voltage);
printf("Size of the file = %d ", size);
sort(time,voltage,size);
if (xValue < time[0] || xValue>time[size-1]) {
return 0;
}
else {
for (i =0;i <size;i++) {
if (xValue>=time[i] ) {
x1 = time[i];
y1 = voltage[i];
x2 = time[i+1];
y2 = voltage[i + 1];
}
}
printf("x1=%lf & y1= %lf ", x1,y1);
printf("x2=%lf & y2=%lf ", x2, y2);
*yValue = ((y2 - y1) / (x2 - x1))*(xValue - x1) + y1;
return 1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.