How to write a Taylor series as a function - java

I have to write a Taylor series until the 16th element that calculates sin and compare the values returned values with Math.sin. Well , everything works fine until the last time when instead of 0.00000 i get 0.006941.Where is my error and if somebody have an idea how to write this in a more professional way I would be very happy.
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
NumberFormat formatter = new DecimalFormat("#0.000000");
double val[] = {0, Math.PI / 3, Math.PI / 4, Math.PI / 6, Math.PI / 2, Math.PI};
for (int i = 0; i < val.length; i++) {
System.out.println("With Taylor method: " + formatter.format(Taylor(val[i])));
System.out.println("With Math.sin method: " + formatter.format(Math.sin(val[i])));
}
}
public static double Taylor ( double val){
ArrayList<Double> memory = new ArrayList<Double>();
double row = val;
for (int i = 0, s = 3; i < 16; i++, s = s + 2) {
double mth = Math.pow(val, s);
double result = mth / factorial(s);
memory.add(result);
}
for (int i = 0; i < 16; i++) {
if (i % 2 == 0) {
double d = memory.get(i);
row = row - d;
} else {
double d = memory.get(i);
row = row + d;
}
}
return row;
}
public static long factorial ( double n){
long fact = 1;
for (int i = 2; i <= n; i++) {
fact = fact * i;
}
return fact;
}
}

Your math is correct, but your factorials are overflowing once you get to calculating 21!. I printed out the factorials calculated.
factorial(3) = 6
factorial(5) = 120
factorial(7) = 5040
factorial(9) = 362880
factorial(11) = 39916800
factorial(13) = 6227020800
factorial(15) = 1307674368000
factorial(17) = 355687428096000
factorial(19) = 121645100408832000
factorial(21) = -4249290049419214848 // Overflow starting here!
factorial(23) = 8128291617894825984
factorial(25) = 7034535277573963776
factorial(27) = -5483646897237262336
factorial(29) = -7055958792655077376
factorial(31) = 4999213071378415616
factorial(33) = 3400198294675128320
It appears that your raising val to ever higher powers isn't significant enough to make a difference with the overflow until you get to the highest value in your array, Math.PI itself. There the error due to overflow is significant.
Instead, calculate each term using the last term as a starting point. If you have the last value you entered into memory, then just multiply val * val into that value and then divide the next two numbers in sequence for the factorial part.
That's because memory.get(i) is equal to memory.get(i - 1) * (val * val) / ((s - 1) * s). This also makes your calculation more efficient. It avoids the multiplication repetition when calculating the numerator (power part) and the denominator (the factorial calculation). This will also avoid the overflow which results from how you calculated the denominator separately.
My implementation of this idea substitutes this for the first for loop:
double mth = val;
for (int i = 0, s = 3; i < 16; i++, s = s + 2) {
mth = mth * val * val;
mth = mth / ((s - 1) * s);
memory.add(mth);
}
and places
double row = val;
between the for loops, to ensure that the first term is the initial sum as you had it before. Then you don't even need the factorial method.
This this I get 0.000000 for Math.PI.

Related

Why won't this program print?

The problem is to determine the number of hours required before the second method becomes more beneficial than the first. I've been looking at this all day and don't know why I can't get it to print.
public class BestMethod{
public static void main(String[] args){
double earnings1 = 10.00;
double earnings2 = 0.10;
double totalHours1 = 0;
double totalHours2 = 0;
double x = 0;
double y = 0;
for (int i = 1; i <= 10; i++)
{
totalHours1++;
x = totalHours1 * earnings1;
totalHours2++;
earnings2 *= 1;
y = (earnings2 * 1) * 2 + earnings2;
}
if (y > x){
System.out.println ("It will take the second method " + totalHours2 + " hours before it becomes more beneficial than the first method ");
}
}
}
Based on your code, the value of earnings1 and earnings2 are remaining the same.
After the first iteration (i = 1) how values are changing (use pen and paper)
totalHours1 = 1.0, totalHours2 = 1.0, x = 10.0 and y = 0.30000000000000004
After completing the loop (i = 10)
totalHours1 = 10.0, totalHours2 = 10.0, x = 100.0 and y = 0.30000000000000004
So, the value of y is less than the value of x and the condition is going to false.
So, (in case if you want to) print the value use this in your program if (y < x) or you have to change the mathematical (calculation) method.

Why do I get "NaN" as Outprint`?

I want to calculat Pi with Java. With S. Ramanujan formel.
Heres my code:
public class PiV5 {
public static void main(String[] args) {
int a = 4;
double pi = 0;
int b = 3;
int fakult = 3;
int x = 3;
long y = 1;
for (int i =1; i <= 50; i++) {
int n = i*4;
long fakultaet = 1;
long fakultae2 = 1;
int bh = i;
for (int g=1; g<=n; g++) {fakultaet = fakultaet * g;}
for (int l=1; l<=bh; l++) {fakultae2 = fakultae2 * l;}
pi = ((fakultaet * (1103 + (26390*i)))/Math.pow(fakultae2, 4) * Math.pow(396, 4*i));
};
System.out.println("Pi nach ein paar Rechnungen: " + (Math.sqrt(8)/9801)*pi);
}
}
Thanks for ur help, if you could help me
As Andreas mentioned in the comments this calculation results in a numeric overflow, because the values getting to large even for long data types.
The maximum steps you can do with your algorithm right now is 5, because 20! = 2432902008176640000, 21! = -4249290049419214848 which is caused by the numeric overflow.
But even then you have a little error in your code, because you forgot to sum up the values in the loop:
pi += ((fakultaet * (1103 + (26390 * i))) / Math.pow(fakultae2, 4) * Math.pow(396, 4 * i));
To get a better accuracy also use double values for the constant values:
pi += ((fakultaet * (1103d + (26390d * i))) / Math.pow(fakultae2, 4) * Math.pow(396, 4 * i));
Using that with 5 iterations will result in the following:
Pi nach ein paar Rechnungen: 4.0513767058512194E63
This is not quite a good result for PI.
To improve it and get better accuracy you could use BigDecimal class.

finding the sum of the sreies, using java

the question is to find the sum of this series
series
i used this code to solve it , but im not quite sure the logic is correct.
the noofterms is how many terms are going to be added
and x is the number that will be assigned to the variable.
does the logic seem correct?
public static double sumOfSeries(double x, int noofterms){
double evennumbers=1;
double oddnumbers=1;
double result=1;
// since the power of x starts from 1 , we start i from 1 and increment by 2
for (int i=1; i<noofterms; i+=2 ){
// we reset starting numbers so we start from them everytime
evennumbers = 1;
oddnumbers = 1;
// everytime the number increases by 2 when it is smaller than i+1
// ex when its equal to 2 , j = 3 , j+1 = 4 so it increments by 2
// when its 4 , j = 5 , j+ 1 = 6 , it increments
for (int j=2; j<=i+1; j+=2){
// multiply by increments of 2
evennumbers= evennumbers * j;
}
// it starts from 1 and increments by 2 so it goes like 1,3,5
for (int z=1; z<=i; z+=2){
oddnumbers = oddnumbers * z;
}
result*=((Math.pow(x, (double)i)) / (double)i) + (oddnumbers/evennumbers);
}
return result;
}
You can do it better. Note that numerators and denominators form two sequences, so you can keep previous terms to efficiently make computations, this will look like this :
long even = 1;
long odd = 1;
double result = x;
for(long i = 1; i < noofterms; i++)
{
even *= 2 * i;
odd *= 2 * i - 1;
double oper = Math.pow(x, (double)(2 * i + 1)) / (double)(2 * i + 1);
result += (double)even / (double)odd * oper;
}
You can improve by using logarithms because even and odd will grow very fast and will lead to overflows :
double even = 0.0;
double odd = 0.0;
double result = x;
double logx = Math.log(x);
for(long i = 1; i < noofterms; i++)
{
even += Math.log((double)(2 * i));
odd += Math.log((double)(2 * i - 1));
double oper = logx * (2 * i + 1) - Math.log((double)(2 * i + 1));
result += Math.exp(even - odd + oper);
}
EDIT: only one sequence could also be computed : p *= (double)(2*i)/(2*i-1). Then the log trick is not useful.

Maximum value of int breaking my for loop?

I was attempting to solve this morning's Codeforces problem Div 2C: http://codeforces.com/contest/716/problem/C
This problem has the potential to loop up to 100,000 times so the parameter here can be up to 100,000. Loop seems to break when passing in 100,000 (and possibly earlier) and i is declared as an int:
public void solve(int a) {
double x = 2;
double y = 0;
double n = 0;
double target = 0;
double lcm = 0;
for (int i = 1; i <= a; i++) {
lcm = (i + 1) * i;
y = ((lcm * lcm) - x) / i;
n = (y * i) + x;
if (Math.sqrt(n) % (i + 1) == 0) {
x = Math.sqrt(n);
String answer = String.format("%.0f", y);
System.out.println("this is i: " + i);
System.out.println(answer);
}
}
}
Here is the relevant output:
this is i: 46337
99495281029892
this is i: 46338
99501722706961
this is i: 46340
99514606895203
this is i: 65535
32769
Doing a quick search on Stack overflow shows that the number 65535 is associated with a 16-bit unsigned int, but java uses 32bit ints. Changing the type to double works, as does simply looping 100,000 times and printing without the code logic. I understand that 100,000^2 IS above the maximum int limit, but this value is never stored as an int in my code. What's going on here?
The following line generates an out of bounds int before converting the result to double:
lcm = (i + 1) * i;
The above is essentially the same as:
lcm = (double)((i + 1) * i);
or
int temp = (i + 1) * i;
lcm = (double) temp;
Instead try (first converting to double and then taking what is similar to a square):
lcm = (i + 1.0) * i;

Converting a float into a string fraction representation

In Java, I am trying to find a way to convert a float number into a fraction string. For example:
float num = 1.33333;
String numStr = Convert(num); // Should return "1 1/3"
float num2 = 1.333;
String numStr2 = Convert(num2); // Should also return "1 1/3"
float num3 = 0.5;
String numStr3 = Convert(num3); // Should return "1/2"
float num4 = 2.25;
String numStr4 = Convert(num4); // Should return "2 1/4"
Any ideas how to do this in Java?
The simplest approach might be to use trial and error.
public static String toFraction(double d, int factor) {
StringBuilder sb = new StringBuilder();
if (d < 0) {
sb.append('-');
d = -d;
}
long l = (long) d;
if (l != 0) sb.append(l);
d -= l;
double error = Math.abs(d);
int bestDenominator = 1;
for(int i=2;i<=factor;i++) {
double error2 = Math.abs(d - (double) Math.round(d * i) / i);
if (error2 < error) {
error = error2;
bestDenominator = i;
}
}
if (bestDenominator > 1)
sb.append(' ').append(Math.round(d * bestDenominator)).append('/') .append(bestDenominator);
return sb.toString();
}
public static void main(String... args) {
System.out.println(toFraction(1.3333, 1000));
System.out.println(toFraction(1.1428, 1000));
for(int i=1;i<100000000;i*=10) {
System.out.println("PI "+i+": "+toFraction(3.1415926535897932385, i));
}
}
prints
1 1/3
1 1/7
PI 1: 3
PI 10: 3 1/7
PI 100: 3 14/99
PI 1000: 3 16/113
PI 10000: 3 16/113
PI 100000: 3 14093/99532
PI 1000000: 3 140914/995207
PI 10000000: 3 244252/1725033
Look into chain fractions. This allows you to determine denominator and fraction within a given accuracy.
For Pi you can get 22/7 or 355/113 depending on when you choose to stop.
This might be of help:
http://www.merriampark.com/fractions.htm
Otherwise you'd need some way of telling Convert() how far out you want to take things. Maybe a maximum reduced demoninator or something like that. That way you'll get "1 1/3" for both of the first two examples you have above rather than "1 33333/100000" for the first and "1 333/1000" for the second.
Extract the fractional part of the number (for example, ((int) 0.5 + 1) - 0.5, then divide one by the result (1 / 0.5). You'll get the denominator of the fraction. Then cast the float to an int, and you'll get the integer part. Then concatenate both.
It's just a simple solution, and will work only if the numerator of the fraction is 1.
double n = 1.2f;
int denominator = 1 / (Math.abs(n - (int) n - 0.0001)); //- 0.0001 so the division doesn't get affected by the float point aproximated representation
int units = (int) n;
int numerator = units * denominator + 1;
System.out.println("" + numerator + "/" + denominator); //6/5
System.out.println("" + units + " 1/" + denominator); //1 1/5
Assume you have "0.1234567", then count how many numbers after the decimal point (which is 7). then multiply the number with 10 ^ 7, now you have "1234567".
divide 1234567 over 10 ^ 7. Then, simplify the fraction using the GCD of the two numbers.
0.1234567 * 10000000 = 1234567
=> 1234567 / 10000000
=> System.out.println(1234567 / gcd(1234567,10000000) + "/" + 10000000/gcd(1234567,10000000));
Modified the FOR loop to break the loop, when the best denominator is already identified.
if (error2 == 0) break;
public static String toFraction(double d, int factor) {
StringBuilder sb = new StringBuilder();
if (d < 0) {
sb.append('-');
d = -d;
}
long l = (long) d;
if (l != 0) sb.append(l);
d -= l;
double error = Math.abs(d);
int bestDenominator = 1;
for(int i=2;i<=factor;i++) {
double error2 = Math.abs(d - (double) Math.round(d * i) / i);
if (error2 < error) {
error = error2;
bestDenominator = i;
if (error2 == 0) break;
}
}
if (bestDenominator > 1)
sb.append(' ').append(Math.round(d * bestDenominator)).append('/') .append(bestDenominator);
return sb.toString();
}
public static void main(String... args) {
System.out.println(toFraction(1.3333, 1000));
System.out.println(toFraction(1.1428, 1000));
for(int i=1;i<100000000;i*=10) {
System.out.println("PI "+i+": "+toFraction(3.1415926535897932385, i));
}
}

Categories

Resources