So, this is a C programming problem I was given for class. To do this problem, you need to know the basic form of a derivative. Here are the instructions.
Here is what I was trying to do...
Given the txt file, I would read in a value, then read in the value ahead of the just read value. I tried to use two for
loops, but that doesn't seem to work as expected. I can't use arrays in this program--(this would make this problem trivial, in my opinion).
There are currently four for
loops. The first pair are used for calculating f'(x) and the second pair is used for calculating f''(x).
Here is my code. It's a lot of commenting, sorry in advance.
// Lab7_Prob2.cpp : Computes derivatives of functions... FML
// nxt3
#include "stdafx.h"
#include <stdio.h>
#define SUCCESS printf("File operations were successful!\n");
/*function that calculates derivative*/
double derive(double x_i, double fX_i, double x_i2, double fX_i2) {
/*x_i, f(x_i); x_i2 = (x_i + 1), fX_i2 = f(x_i + 1)*/
double fprime = (fX_i2 - fX_i) / (x_i2 - x_i); //derivative of x, f'(x)
return fprime; //returns f'(x)
}
int main() {
double x, fX, x2, fX2; //init variable for x and f(x); also, (x_i+1) and (f(x_i+1))
double fP, f2P, xp2, fXp2; //init variables for f'(x), f''(x), f'(x_i) and f'(x_i+1)
int i = 1, j = 1; //indices for both MAIN loops, i = MainLoop1, j = MainLoop2
int ss = -99; //sentinal signal for secondderiv.txt
FILE *dt; //ptr for deriv_testdata.txt
FILE *fd; //ptr for firstderiv.txt
FILE *sd; //ptr for secondderiv.txt
dt = fopen("C:/Users/ng00947/Downloads/DataFiles/deriv_testdata.txt", "r"); //opens deriv_testdata.txt
fd = fopen("C:/Users/ng00947/Downloads/DataFiles/firstderiv.txt", "w"); //creats firstderiv.txt
sd = fopen("C:/Users/ng00947/Downloads/DataFiles/secondderiv.txt", "w"); //creats secondderiv.txt
int mNumDataPts; //number of records from deriv_testdata.txt
fscanf(dt, "%i", &mNumDataPts); //grabs number of records from file
fprintf(fd, "%i\n", (mNumDataPts - 1)); //prints number of records to firstderiv.txt
//209 data points for f'(x)
//208 data points for f''(x)
/*loop scans in a uses derive function to calculate f'(x)*/
for (i; i <= (mNumDataPts - 1); i++) { //MainLoop1
fscanf(dt, "%lf %lf", &x, &fX); //grabs values in each row for x and f(x)
/*loop grabs values one ahead of x and f(x); this is for (x_i+1) and f(x_i+1)*/
for (int k = i; k <= (i + 1); k++) { //SubLoop1
fscanf(dt, "%lf %lf", &x2, &fX2);
break; //leaves this loop to continue with main loop
}
fP = derive(x, fX, x2, fX2); //calculates derivative using function
fprintf(fd, "%.2f \t %.2f\n", x, fP); //prints x and f'(x) to firstderiv.txt
}
/*loop scans in and uses derive function to calculate f''(x)*/
for (j; j <= (mNumDataPts - 2); j++) { //MainLoop2
fscanf(fd, "%lf %lf", &x, &fP); //grabs values in each row for x and f'(x)
/*loop grabs values one ahead of x and f'(x); this is for (x_i+1) and f'(x_i+1)*/
for (int m = j; m <= (j + 1); m++) { //SubLoop2
fscanf(dt, "%lf %lf", &xp2, &fXp2);
break; //leaves this loop to continue with main loop
}
f2P = derive(x, fP, xp2, fXp2); //calculates second derivative using function
fprintf(sd, "%.2f \t %.2f\n", x, f2P); //prints x and f''(x) to secondderiv.txt
}
fprintf(sd, "%i \t %i", ss, ss); //prints sentinal signal to end of secondderiv.txt
/*closes all files*/
fclose(dt);
fclose(fd);
fclose(sd);
SUCCESS; //prints message for sweet victory
return 0; //let's wrap this up
}
I appreciate any and all help! (Bonus points if you can help answer my question without providing code.)
Here are the datafiles:
File given, deriv_testdata.txt
First Derivative, firstderiv.txt
Second derivative, secondderiv.txt