Search code examples
ctaylor-series

Taylors Theorem C


I've got simple code for Taylor's Theorem for cosh() function.

I'm trying to catch a mistake - the result is sometimes close the real answer.

How to do it correctly?

When my start is 0, end is 5, and subdivides is 5 it gave good results, but when I put 5 as start and 10 as end, the result is farther away from the expected value.

#include <stdio.h>
#include <math.h>

int poww( float number, int a )
{
    float result = 1.0;
    int i;
    if( a != 0 );
    {
        for( i = 0; i < a; i++ ) {
            result = result * number;
        }
    }
    return result;
}

int factorial(int n)
{
    switch (n) {
    case 0:
        return 1;
        break;
    default:
        return n * factorial(n-1);
    }
}

void main()
{
    puts("Enter start: ");
    float start;
    scanf("%f", &start);

    puts("\nEnter end: ");
    float end;
    scanf("%f", &end);

    puts("\nSubintervals:");
    int subinterval;
    scanf("%d", &subinterval);

    float h = (end - start) / (float)subinterval;
    printf("h is : %3.2f \n", h);

    double x, result, temp;
    int n;

    for( x = start; x <= end; x += h) {
        result = 0;
        for(n = 0 ;  ; n++) {
            temp = poww(x, 2 * n) / (factorial( 2 * n ) * 1.0);
            if(temp < 0.00001) {
                break;
            } else {
                result = result + temp;
                printf("X = %f temp = %f, result = %f\n", x, temp, result);
            }
        }
        printf("X = %f, result = %3.2f, cosH = %3.2f\n\n", x, result, cosh(x) );
    }
    puts("Press any key...");
    getchar();
}

PROBLEM SOLVE:

function returns an integer instead of double, also I changed every float to double.


Solution

  • Change all float types to double and use double as the return type for the factorial() and poww() functions, too. It's the last two that are most important in this case.

    Also, the return type on main() should be int, not void.

    [I just finished removing the dead if statement in poww(), and noticed that the function only "speeds up" a pow() computation. If you're worried about performance, worry about computing a factorial and a power on every term, rather than multiplying the previous term by x^2 and dividing by (2*n)*(2*n-1).]

    I get good results between 4 and 10 on this minor fix of your code:

    #include <stdio.h>
    #include <math.h>
    
    double poww( float number, int a )
    {
        float result = 1.0;
        int i;
        for( i = 0; i < a; i++ )
        {
            result = result * number;
        }
        return result;
    }
    
    double factorial(int n)
    {
         switch (n)
          {
                case 0: return 1;
                        break;
                default: return n * factorial(n-1);
          }
    }
    
    int main(){
    
        puts("Enter start: ");
        float start;
        scanf("%f", &start);
    
        puts("\nEnter end: ");
        float end;
        scanf("%f", &end);
    
        puts("\nSubintervals:");
        int subinterval;
        scanf("%d", &subinterval);
    
        float h = (end - start) / (float)subinterval;
        printf("h is : %3.2f \n", h);
    
        double x, result, temp;
        int n;
    
        for( x = start; x <= end; x += h){
            result = 0;
            for(n = 0 ;  ; n++){
                temp = poww(x, 2 * n) / (factorial( 2 * n ) * 1.0);
                if(temp < 0.00001){
                    break; }
                else{
                    result = result + temp;
                    printf("X = %f temp = %f, result = %f\n", x, temp, result);
                }
            }
                    printf("X = %f, result = %3.2f, cosH = %3.2f\n\n", x, result, cosh(x) );
        }
    
        puts("Press any key...");
        getchar();
        return 0;
    }