【发布时间】:2014-04-07 16:03:43
【问题描述】:
所以,这是我在课堂上遇到的一个 C 编程问题。要解决这个问题,您需要知道导数的基本形式。以下是说明。
这就是我想要做的......
给定 txt 文件,我会读入一个值,然后在刚刚读取的值之前读入该值。我尝试使用两个for 循环,但这似乎没有按预期工作。我不能在这个程序中使用数组——(在我看来,这会使这个问题变得微不足道)。
目前有四个for 循环。第一对用于计算 f'(x),第二对用于计算 f''(x)。
这是我的代码。评论太多了,先见谅。
// 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
}
感谢所有帮助! (如果您可以在不提供代码的情况下帮助回答我的问题,则可以加分。)
这里是数据文件:
File given, deriv_testdata.txt
【问题讨论】:
-
这可能无法解决问题,但强烈建议检查来自
fscanf()的返回值,这应该是2是代码中的大多数(如果不是全部)情况。检查似乎是多余的,但健壮的代码使用它,这是一个很好的做法,即使在这里也是如此。
标签: c loops scanf derivative