Программа, которая находит y, производную и интегральные значения полинома (ОБНОВЛЕНО) - PullRequest
0 голосов
/ 04 февраля 2019

Создание программы, которая должна вычислять полиномы 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;
}

Screenshot of output

Вот снимок экрана с выводом, поскольку вы можете видеть, что таблица печатает только до 1 вместо 5, как требуется (указано входными данными).Мне нужно изменить то, что в цикле for, чтобы заставить его правильно выводить, но я не уверен, что делать

1 Ответ

0 голосов
/ 04 февраля 2019

Я видел проблему с вашим кодом:

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;
}
...