0

My task is to implement the cos(x) function withou using Math. library and with the taylor polynom, my code looks like this:


public class Cosinus {
    public static void main(String[] args) {

        /*if(args.length == 0){
            System.out.println("ERROR: Geben Sie ein Argument für x ein!");
            return;
        }*/
        double x = 5;

        double summand1     = (x*x) / 2;
        double summand2     = (x*x*x*x) / (2*3*4);
        double summand3     = (x*x*x*x*x*x) / (2*3*4*5*6);
        double summand4     = (x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8);
        double summand5     = (x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10);
        double summand6     = (x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12);
        double summand7     = (x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14);
        //double summand8     = (x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16);
        //double summand9     = (x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18);
        //double summand10    = (x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
        

        double cosinusFunktion = (((((((1 - summand1) + summand2) - summand3) + summand4) - summand5) + summand6) - summand7);
        
        System.out.println(cosinusFunktion);
    }
}

For x = 1, 2, 3, and 4 Y is between 1 and -1 but with x = 5 it goes too -4 and if the x are even getting bigger this continues too 1287918274.

I cant solve this task but tthe task says it is enough to implement this funktion iwth the taylor polynom and the first 11 summand. I tried this too, but then even with x = 1 the bounds are broken. How can i solve this, so x = 42.5 is in bound of -1 and 1?

Tried more summands to make the result more excact, but the bounds get broken even more. tried implement the periodicity of x-2*PI, but I dont know where to put it and results get messed up eeven more.

ntimo
  • 1
  • see [My program for calculating pi using Chudnovsky in C++ precision problem](https://stackoverflow.com/a/73258767/2521214) and look how the iteration is computed so the subresult is as small as possible ... so do this in for loop and compute `(x^i)/(i!)` from the previous iteration ... for example like this [Taylor sin() in C++](https://stackoverflow.com/a/71908936/2521214) – Spektre Nov 03 '22 at 08:01

2 Answers2

3

you are getting an integer overflow for the factorial in the summand7 line

as a simple fix you can change the line to:

double summand7     = (x*x*x*x*x*x*x*x*x*x*x*x*x*x) / ((double) 2*3*4*5*6*7*8*9*10*11*12*13*14);
SR3142
  • 550
  • 3
  • 9
  • damn that really helped, how did you know there would be an overflow? – ntimo Nov 02 '22 at 20:31
  • 1
    this is one of those things you learn through experience and spending hours trying to figure out what's wrong with your code – SR3142 Nov 02 '22 at 20:55
  • yes thats right, i was able to solve it, now i can calculate at least until x = 100 but thats enough for my assignment and I learned a lot, thanks guys – ntimo Nov 02 '22 at 22:49
1

The Taylor expansion will always blow up for larger inputs. However, since:

sin(x) = sin(x + n*2*pi)  // for any integer n

You can simply pre-process you input with a modulus function to prevent your output from blowing up.


I can't test compile right now, but if memory serves, you would add one of the following lines prior to computing your first summand:

x = x%(Math.PI*2)

Or, if you can't use Math

x = x%((double)3.14159265358979323846*2)
Pursuit
  • 12,285
  • 1
  • 25
  • 41
  • thanks for the help! so for cos(x) = cos(x-n*2*pi) ? – ntimo Nov 02 '22 at 20:01
  • @ntimo for `sin` and `cos` is the same ... as `n` goes from `-inf` to `+inf` the sign does not matter so the parameter of `cos(x)` should be `cos(fmod(x,2*PI))` ... (not sure if `%` in java is float or int just in case its int `fmod` is safer you can do also `cos(x - (floor(x/2*PI)*2*PI))` in case you do not have `fmod`...) – Spektre Nov 04 '22 at 07:14