Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Non-Linear Calculator  RSS feed

 
Andrew Tran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am currently working on an assignment that approximates the value for e^x and sin(x) using the taylor series. This program cannot use the exp() or sin() method in the Math class, nor use the Math.E constant or any if statements.

"To calculation ex, you must use a loop and calculate the sum above for the first 30 terms.  To obtain maximum numerical range and improve efficiency, you will want to progressively compute each term: don't calculate the 4th term by raising x to the 4th power and dividing by 4! Instead, determine the 4th term based on the value of the 3rd term. In other words, determine how the 4th term differs mathematically from the 3rd term, and perform only those operations.
To calculate sin(x), you must use a loop to calculate the sum above for the first 30 terms. Again, progressively compute each term."

For the e^x calculation I am pretty on point, but for the sin(x) statement i am not sure on where my calculation went wrong. Any helps would be greatly appreciated. Thank you


result.png
[Thumbnail for result.png]
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Looks like rounding errors to me ... the results are pretty much 1, 0, -1, and 0, which of course, are correct.

Henry
 
Campbell Ritchie
Marshal
Posts: 55691
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If those are only rounding errors, simply print the output with fewer places after the decimal point
Why are you returning double from your factorial method? Factorials are defined as integers and they take non negative integer arguments. You will get overflow for ints at 13! and for longs at 22! (I think), so try this instead. The last time I tried that for factorils, I got a stack space exception about 5000! but don't worry. You will get double overflow and return ∞ long before that.
 
Gary W. Lucas
Ranch Hand
Posts: 67
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've got a couple of suggestions.  First, keep in mind that the Taylor series as you've coded it converges fast when x is close to zero but more slowly as x moves away from zero.   It is possible to expand the series around other constants, but I don't think you need to look at that for this assignment...  maybe for a class in Calc, but not for this assignment :-)

Anyway, when I'm working with series summations and want to get all the accuracy I possibly can, I find that it is often better to add up the terms from smallest magnitude to largest.  If you start with the largest, then the running sum gets so large that there just isn't enough precision to capture the contribution of the small order terms.   Imagine a case where you have nine terms.  If you add up the 6 largest, the three smallest might be so small that when you add them they don't even change the sum.  But if you took the three smallest together, added them up first, and then added that to the others, they might be big enough to significantly affect the sum.

That being said, a little algebra can solve a lot of your issues.  Consider the following snippet of code.  It adds up the terms from smallest to largest, and so preserves the small-order contribution It avoids having to compute the values for factorial (which can get enormous very quickly).  And it even runs pretty fast.

For your homework assignment, you would probably want to code this logic into a loop rather than using explicit terms as I did.  As the saying goes, the results are "left as an exercise for the student."



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!