-1

I'm getting the "-1.#IND00" as a solution to this Fourier series.

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

int main()
{
    int n, x=50, L=100, q1=2;
    float pi = 3.141592;
    float flux1=0;
    double flux2=0;

        for(n=0;n<=50;n++)
        {
            flux1=q1*(2/(pi*n))*(cos(n*pi)-1)*(sin((n*pi*(x+L))/(2*L)));
            flux2+=flux1;
        }
        flux2+=q1;
        printf("%f\n", flux2);

    return 0;
}

flux2 is coming out as "-1.#IND00" I can't work out why, since each term in the sum, (flux1), comes out as a rational number and the sum seems to converge.

Can you guys help?

  • 1
    Why are you using `float` mixed with `double`? Unless you want to satisfy an interface, there is very little reason to use a `float` on a modern machine. – Weather Vane Oct 10 '15 at 17:38
  • [This old answer](http://stackoverflow.com/a/347940/440558) maybe can provide some clues. – Some programmer dude Oct 10 '15 at 17:41
  • *"since each term in the sum, (flux1), comes out as a rational number"* simply isn't true. `flux1` goes bad on the very first iteration, even after replacing all `int` and `float` with `double` (except `n`), adding `.0` to all literal integer numbers, improving the value of `pi`, and swapping the sequence of `sin((n*pi*...` to `sin((pi*n*...`. – Weather Vane Oct 10 '15 at 17:50

1 Answers1

2

This computation is in error on the first iteration when n == 0

2.0/(pi*n)

It's a "divide by zero" error.

On subsequent iterations, the computation is good, but the sum flux2 has already been corrupted by the bad value of the first flux1 and never recovers.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56