查找多项式的 y、导数和积分值的程序(已更新)
Program that finds y, derivative, and integral vals of a polynomial (UPDATED)
制作一个应该计算多项式 y 值导数和积分的程序...现在在格式化 for 循环时遇到问题,因此它循环遍历我想要的所有 x 值(由用户确定)。
很确定数学函数现在也不是完全正确的,但是一旦我让这个循环工作,我可以稍后再去处理它们哈哈
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);
int main()
{
double a, b, c;
double xi, xf, xc, x=0;
double fx=0, fD=0, A=0;
printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
explain();
printf("\n\nEnter a value for a: ");
scanf("%lg", &a);
printf("Enter a value for b: ");
scanf("%lg", &b);
printf("Enter a value for c: ");
scanf("%lg", &c);
printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);
printf("\n\nEnter your initial x-value: ");
scanf("%lg", &xi);
printf("Enter your final x-value: ");
scanf("%lg", &xf);
printf("Enter what you would like to increment by: ");
scanf("%lg", &xc);
printf("| x | f(x) | f'(x) | A |\n"); //printing table
printf("----------------------------------------\n");
for(int i=xi; i<xf; i++) {
math(a, b, c, x, xi, &fx, &fD, &A);
printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
x = x + xc;
}
return;
}
void explain() {
printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
*fx = (a*(x*x)) + (b*x) + c; //finding y values
*fD = (2*a) + b; //finding derivative values
*A = ((a/3)*pow(x,3) + (a/2)*pow(x,2) + c*x) - ((a/3)*pow(xi,3) + (a/2)*pow(xi,2) + c*xi); //finding integral values
return;
}
这是输出的屏幕截图,如您所见,table 仅打印到 1,而不是所需的 5(由输入指示)。我需要更改 for 循环中的内容以使其正确输出,但我不确定该怎么做
我发现你的代码有问题:
1> 你的推导函数不正确。应该是 2*a*x + b
2> 我将 for 循环更改为 while 循环,步骤是从标准输入输入的 xc
这是我的解决方案和你的最新答案:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);
int main()
{
double a, b, c;
double xi, xf, xc, x = 0;
double fx = 0, fD = 0, A = 0;
printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
explain();
printf("\n\nEnter a value for a: ");
scanf("%lg", &a);
printf("Enter a value for b: ");
scanf("%lg", &b);
printf("Enter a value for c: ");
scanf("%lg", &c);
printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);
printf("\n\nEnter your initial x-value: ");
scanf("%lg", &xi);
printf("Enter your final x-value: ");
scanf("%lg", &xf);
printf("Enter what you would like to increment by: ");
scanf("%lg", &xc);
printf("| x | f(x) | f'(x) | A |\n"); //printing table
printf("----------------------------------------\n");
x = xi;
double nextX;
while (x <= xf) {
nextX = x + xc;
math(a, b, c, x, nextX, &fx, &fD, &A);
printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
x = x + xc;
}
return 0;
}
void explain() {
printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
*fx = (a*(x*x)) + (b*x) + c; //finding y values
*fD = (2 * a * x) + b; //finding derivative values
*A = ((a / 3)*pow(x, 3) + (a / 2)*pow(x, 2) + c * x) - ((a / 3)*pow(xi, 3) + (a / 2)*pow(xi, 2) + c * xi); //finding integral values
return;
}
制作一个应该计算多项式 y 值导数和积分的程序...现在在格式化 for 循环时遇到问题,因此它循环遍历我想要的所有 x 值(由用户确定)。
很确定数学函数现在也不是完全正确的,但是一旦我让这个循环工作,我可以稍后再去处理它们哈哈
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);
int main()
{
double a, b, c;
double xi, xf, xc, x=0;
double fx=0, fD=0, A=0;
printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
explain();
printf("\n\nEnter a value for a: ");
scanf("%lg", &a);
printf("Enter a value for b: ");
scanf("%lg", &b);
printf("Enter a value for c: ");
scanf("%lg", &c);
printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);
printf("\n\nEnter your initial x-value: ");
scanf("%lg", &xi);
printf("Enter your final x-value: ");
scanf("%lg", &xf);
printf("Enter what you would like to increment by: ");
scanf("%lg", &xc);
printf("| x | f(x) | f'(x) | A |\n"); //printing table
printf("----------------------------------------\n");
for(int i=xi; i<xf; i++) {
math(a, b, c, x, xi, &fx, &fD, &A);
printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
x = x + xc;
}
return;
}
void explain() {
printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
*fx = (a*(x*x)) + (b*x) + c; //finding y values
*fD = (2*a) + b; //finding derivative values
*A = ((a/3)*pow(x,3) + (a/2)*pow(x,2) + c*x) - ((a/3)*pow(xi,3) + (a/2)*pow(xi,2) + c*xi); //finding integral values
return;
}
这是输出的屏幕截图,如您所见,table 仅打印到 1,而不是所需的 5(由输入指示)。我需要更改 for 循环中的内容以使其正确输出,但我不确定该怎么做
我发现你的代码有问题:
1> 你的推导函数不正确。应该是 2*a*x + b
2> 我将 for 循环更改为 while 循环,步骤是从标准输入输入的 xc
这是我的解决方案和你的最新答案:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);
int main()
{
double a, b, c;
double xi, xf, xc, x = 0;
double fx = 0, fD = 0, A = 0;
printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
explain();
printf("\n\nEnter a value for a: ");
scanf("%lg", &a);
printf("Enter a value for b: ");
scanf("%lg", &b);
printf("Enter a value for c: ");
scanf("%lg", &c);
printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);
printf("\n\nEnter your initial x-value: ");
scanf("%lg", &xi);
printf("Enter your final x-value: ");
scanf("%lg", &xf);
printf("Enter what you would like to increment by: ");
scanf("%lg", &xc);
printf("| x | f(x) | f'(x) | A |\n"); //printing table
printf("----------------------------------------\n");
x = xi;
double nextX;
while (x <= xf) {
nextX = x + xc;
math(a, b, c, x, nextX, &fx, &fD, &A);
printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
x = x + xc;
}
return 0;
}
void explain() {
printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
*fx = (a*(x*x)) + (b*x) + c; //finding y values
*fD = (2 * a * x) + b; //finding derivative values
*A = ((a / 3)*pow(x, 3) + (a / 2)*pow(x, 2) + c * x) - ((a / 3)*pow(xi, 3) + (a / 2)*pow(xi, 2) + c * xi); //finding integral values
return;
}