1

I am trying to build a program that calculates Taylor series for sin(x). With x bigger than 0 and less than pi/2 (I tried to write with latex but didn't work(??)) . Approximation of sin(x) is around the point x0 = 0. e is the max error (or inaccuracy... I don't know how it's called). Error(or inaccuracy) is calculated with the formula in the method I call r. So r must always be less than e. Finally, n is the number of iterations. Below is my program, it works with no errors but i get inaccurate results and i don't know what i am doing wrong(maybe we should take into consideration the propability of using a formula with mistakes).

import java.lang.Math;
import java.util.Scanner;

public class hw74 { 
    public static long factorial(int number) {
        long result = 1;

        for (int factor = 2; factor <= number; factor++) {
            result *= factor;
        }

        return result;
    }

    static double p(double x, double x0, int n) {
        double PT = Math.pow(-1, n) * (Math.pow(x-x0, 2*n+1)/factorial(2*n+1));
        return PT;
    }

    static double r(double x, int n) {
        double rn = Math.pow(x, n+1)/factorial(n+1);
        return Math.abs(rn);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double x0 = 0;
        System.out.println("Give x. ");
        double x = sc.nextDouble();
        System.out.println("Give E. ");
        double e = sc.nextDouble();
        System.out.println("Give N. ");
        int n = sc.nextInt();
        int i = 0;
        //System.out.println(e<r(x, i) && i<n);
        //System.out.println(r(x, i));
        while((r(x, i)<e) && i<n) {
            i++;
            System.out.println("N is : "+i);
            System.out.println("P is : "+p(x,x0,n));
            System.out.println("E is : "+r(x,n));
        }
    }   
}

For example: for n=5, e=1 and x=pi/5 (0.62831853071), the results should be : sin(pi/5) = 0.5877852522... For n=1 r=0.1974, n=2 r=0.0413, n=3 r=0.0065, n=4 r=8.16*10^(-4), n=5 r=8.54*10^(-5)

hnefatl
  • 5,860
  • 2
  • 27
  • 49
  • Writing the formula with a call to factorial, as you did, is not recommended. It's inaccurate and inefficient. – duffymo Jan 14 '18 at 23:40
  • @duffymo So i should calculate the factorial of the number inside the same method? However, this causes no problems so far... –  Jan 15 '18 at 09:52
  • I didn't say "problem" - inaccurate and inefficient. – duffymo Jan 15 '18 at 10:23

2 Answers2

1

Double is floating point type and this way not accurate. Use BigDecimal instead. See this answer for more information.

haba713
  • 2,465
  • 1
  • 24
  • 45
  • 2
    The inaccuracy problem in my program is way bigger than decimals, so it's not about doubles or floats. Thanks anyway. –  Jan 14 '18 at 17:20
0

We approximate the Taylor series (a sum) with a Taylor polynomial. The method p in your program calculates just a single term in this polynomial, so you just need to sum the sequence from 0 to N, for example:

static double sum(double x, double x0, int n) {
    double sum = 0;
    for (int i = 0; i <= n; ++i) {
        sum += p(x, x0, i);
    }
    return sum;
}

In other words, that's the part notated by the symbol ∑.

edit: Looking at your logic in main, it could be that you're already trying to do something like this, but I'm not sure. I think you probably need to revisit that too.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • Yeah that's the main part my code i was missing...however, i still need to do something with my r method because what is printed from what r returns is not what i expected. I think i'll find it(it's not so important anyway). Thanks –  Jan 14 '18 at 17:57
  • KhanAcademy has some videos on this if you need a refresher. https://www.khanacademy.org/math/ap-calculus-bc/bc-series/bc-taylor-series/v/lagrange-error-bound-for-sine-function – Radiodef Jan 14 '18 at 18:03
  • About your "edit"...yes i made the loop but i wasn't keeping the sum, so i guess the problem could be solved in 2 lines by creating the sum variable, adding in each iteration p to the sum, and by replacing while with a for. –  Jan 14 '18 at 20:56