Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 27.12.2017, 14:15   #1
vufesey
Новичок
Джуниор
 
Регистрация: 27.12.2017
Сообщений: 1
По умолчанию Вычисление функций с помощью рядов

Вещественная функция может быть представлена с помощью ряда Тейлора, радиус сходимости которого определяется положением особых точек функции. Любой конечный отрезок ряда Тейлора является полиномом, поэтому может быть вычислен с использованирем операций сложения и умножения.

Данная программа вычисляет функцию log(1-x).

Код:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// this function is calculating the power of x
double powX(double x, int i)
{
    switch(i)
    {
        case 0:
            return 1;
        case 1:
            return x;
        default:
            return x * powX(x, i-1);
    }
}

// this function is calculating the value of a member of the serie
double membValCalc(double x, int i)
{
    return -1 * (powX(x, i) / i);
}

int main()
{
    /* acc = accuracy; result = aproximated value; et_result = contain library value;
       aprDiff = is the difference between library's value and the result of this program's work */
    double x, acc, result, membVal, et_result, aprDiff, achievedAcc;
    int i, j = 0, n; // n - is the maximal member of serie in the calculation
    printf("This program will calculate function ln(1-x).\n");
    printf("Please, enter x value between -1.0 and 1.0 as 'double' type: ");
    scanf("%lf", &x);
    // Area of definition of this function; if x not belong to this area, the program will be closed
    if((x < -0.9999999)||(x > 0.9999999))
    {
        printf("Error: x must be the value between -1 and 1.\n");
        return -1;
    }
    printf("Please, enter n value as 'integer' type: ");
    scanf("%d", &n);
    printf("Enter accuracy of calculation as 'double' type: ");
    scanf("%lf", &acc);
    if(acc < 0)
    {
        printf("Error: accuracy is negative number.");
        return -1;
    }

    result = 0.0;
    // This for loop is calculating each member of the serie and increase the result variable by this value
    for(i = 1; i <= n; i++)
    {
        membVal = membValCalc(x, i);
        j = i;
        if((membVal > 0) && (membVal <= acc))
        {
            j = i - 1;
            break;
        }
        else if((membVal < 0) && ((membVal * -1) <= acc))
        {
            j = i - 1;
            break;
        }
        else
        {
            result += membVal;
        }
    }
    et_result = log(1-x);
    if((result >= 0) && (et_result >= 0))
    {
        aprDiff = result - et_result;
    }
    else if((result < 0) && (et_result < 0))
    {
        aprDiff = (result * -1) - (et_result * -1);
    }
    else
    {
        aprDiff = result + et_result;
    }
    achievedAcc = membValCalc(x, j);

    if(aprDiff < 0) {aprDiff *= -1;}
    if(achievedAcc < 0) {achievedAcc *= -1;}
    printf("Total quantity of calculated members of the serie: %d\n", j);
    printf("The achieved accuracy of calculation is %f\n", achievedAcc);
    printf("The result log(1-x) is: %lf\n", result);
    printf("Using the Math.h library we could get the result log = %lf\n", et_result);
    printf("The difference between calculated approximation and library's value: %lf", aprDiff);

    return 0;
}
Помогите пожалуйста переделать вычисления в коде под формулу exp(x^2).
vufesey вне форума Ответить с цитированием
Старый 27.12.2017, 14:35   #2
p51x
Старожил
 
Регистрация: 15.02.2010
Сообщений: 15,724
По умолчанию

Ну так откройте вики https://ru.wikipedia.org/wiki/%D0%AD...BD%D1%82%D0%B0 и выпишите формулу в код. Даже ж думать не надо...
p51x вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Решить задачу с использованием функций. Ввод-вывод значений массивов осуществить с помощью функций. Владимир20178 Помощь студентам 2 29.06.2017 05:05
вычисление математических функций C++ Анастасия_96 Помощь студентам 1 12.12.2016 15:29
Вычисление рядов. Ряд Тейлора. Akinak Помощь студентам 0 29.12.2011 00:12
Вычисление рядов Rebel666 Паскаль, Turbo Pascal, PascalABC.NET 5 26.11.2011 18:27
Вычисление рядов заданной точностью Neutron37 Паскаль, Turbo Pascal, PascalABC.NET 0 29.09.2010 20:14