所以,这是我在上课时遇到的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
答案 0 :(得分:1)
建议简化scan()
循环。
扫描第一对。在循环中(从索引1开始)扫描下一对,计算导数并打印。使用第二对作为下一组的第一对。
static const char *format2double = "%lf%lf"; // space not needed in "%lf %lf"
if (fscanf(dt, format2double, &x, &fX) != 2) Handle_Error();
for (i = 1; i < mNumDataPts; i++) {
if (fscanf(dt, format2double, &x2, &fX2) != 2) Handle_Error();
fP = derive(x, fX, x2, fX2);
fprintf(fd, "%.2f \t %.2f\n", x, fP); //prints x and f'(x) to firstderiv.txt
x = x2;
fX = fX2;
}
答案 1 :(得分:0)
计算第一次微分f'(x)。我认为你应该将你的Sub循环更改为
for (int k = i+1 ; k <= (i + 1); k++) //change k=i+1
这将是找到双重微分f''(x)的相同解决方案。将您的子循环更改为
for (int m = j+1; m <= (j + 1); m++); //change m=j+1
* 但是在这个循环中你写了continue;
就在for()
循环结束之前,我认为这是不合逻辑的,因为即使你删除它,该循环将如何继续开始下一次迭代。我想你可能想在那一行写break;
(正如你在找到f'(x)时在早期的子循环中写的那样)