How does a calculator calculate square roots and stuff like sin(10)? Does it do it mathamaticaly?
Printable View
How does a calculator calculate square roots and stuff like sin(10)? Does it do it mathamaticaly?
How else can you do math? :confused:Quote:
Originally posted by Flip
Does it do it mathamaticaly?
You can calculate a cube root mathematically (what I think you mean by that is explain one math 'function' by combining others?), did you know that?
The Calculator uses approximation functions, in our second semester calculus class we derived approximations for cos(x),sin(x) and e^x
the approximation functions are just regular polynomials, if I remember correctly.
Really! if they have an approximation of sinx to the 9th digit of an polynomial for all angles, i'd love to see that polynomial!!!
anywayz i am not sure how a calculator does that, but it is possible to calculate sinx by using its taylor expansion.
I think that's what he was referring to, but don't quote me on it.
like for example, how does the calculator calculate the square root of three? Is there a pencil-and-paper method that the calculator follows?
newton's square root approximation method would work. instead of sqrt(3) it can try to find a number x such that 3-x*x<0.00000001
Calculators and computers user Taylor series. They have infinite terms but if the variable lies in a specific range of values they converge and usually they do very rapidly. For example, for the sinus you have:
sin(x) = x - x3/3! + x5/5! - x7/7! + ...
so the nth term is: (-1)n+1x(2n-1)/(2n-1)!
This works for |x|<=? (I'm not sure if it's 1 or Pi/2, you'll have to check this out on a calculus book or insist to me that I do it myself as soon as I have one handy). If x is outside this range, then you can use a trigonometric formula such as:
sin(a + b)=sin(a)cos(b) + sin(b)cos(a) where a and b meet the requirement (if they don't, keep splitting them and use the formula recursively)
it works for all x's but as x grows the number of terms in the series that we have to use to remain close to the actual sin value goes to infinity
If you're using a program, to do the 3rd root is the same as
num ^ (1/3)
4th
num ^ (1/4)
etc.
It doesn't actually work for all x, for instance, if x = Pi the series does not converge. Though it works only for 0<=x<=Pi/2, it can still be used for other values of x outside this range, by resourcing to "tricks" i.e. properties such asQuote:
Originally posted by Bjwbell
it works for all x's but as x grows the number of terms in the series that we have to use to remain close to the actual sin value goes to infinity
sin(-x) = -sin(x), or sin(x + Pi/2) = cos(x), etc.
Just very few terms are really necessary for a high accuracy, only bear in mind x must be radiants, not degrees. For example, for 90 deg. you have approx. x = Pi/2 = 1.5708 and the result using just 5 terms is 1.000003543, not so different from the actual value 1
it does converge for x=Pi
try doing 10 taylor terms and evaluating it, it come out to 0.00679 which is pretty close to zero
Yup...! I'll definitely have to take a new look into my calculus books...Quote:
Originally posted by Bjwbell
it does converge for x=Pi
try doing 10 taylor terms and evaluating it, it come out to 0.00679 which is pretty close to zero
Btw here's an applet to show you folks how accurate those series can get...
http://math.furman.edu/~dcs/java/taylor.html
The website "http://" doesn't really have much on it...
Repost the link or edit your post
ADDED FROM LAST POST: Yeah, im a retard. Just copy and paste. I am so observant...
The URL is displayed in the text. You can just copy and paste it if the liks doesn't work accidentally.
There is a way to calculate square roots by hand, and it's kind of like a complicated form of division. Each time, two digits are dropped instead of one, and the previous quotient is multiplied by 20, while the new quotient is added to it. (Something like that)
There is at least one thread in this forum that discusses the Newton method for solving all sorts of problems. Square root is a special case, which works as follows.
Suppose you wanted the Square root of 100 and did not know that 10 was the answer. Take a wild guess, say 20.
Then 100/20 = 5, and answer is between 5 & 20.
Take average: (20 + 5) / 2 = 12.5, which is a lot better than 20.
100/12.5 = 8
Average: (12.5 + 8) / 2 = 10.25, we are getting closer.
If you keep it up, you get the following.
20
12.5
10.25
10.003 048 781
10.000 000 465
10.000 000 000
Note that when two successive values are almost equal, you know you are very close, even if you did not know what the final answer should be. If you started with a first guess of 100, it would only take a few more iterations to get an accurate result. A modern PC or calculator is so fast that a few extra iterations are not noticable. The first guess is not important.
The methods used by calculators & computers use some simple algorithm for the first guess. The (number + 1)/2 is not a terrible first guess. It is a compromise catering to original numbers less than one and greater than one. Trying half or 10% of the number is better for big numbers, but worse for numbers less than one.
Trig functions use power series as mentioned in a previous post. For an angle in radians (Pi * degrees / 180), Sine(x) is approximated by the following.
x - x3/3! + x5/5! - x7/7! . . .
3! = 3*2*1
5! = 5*4*3*2*1
et cetera.
Tricks are used to avoid calculating for large values of x, which require many terms of the polynomial. Some tricks are as follows.
For angles between 45 & 90, compute cosine of (90-angle).
Compute both Sine & Cosine of 1/2 or 1/4 of the angle and use formula for twice the angle.
To calculate square roots we can iterate the following equation:
x = x/2 + k/2x
Solving the above U get x 2 = k
In VB
VB Code:
Public Function SquareRoot(thenumber As Double) as Double Dim theVals() As Double ReDim theVals(1 To thenumber) theVals(1) = (1 / 2) + (thenumber / 2) For i = 2 To thenumber theVals(i) = (theVals(i - 1) / 2) + (thenumber / (2 * theVals(i - 1))) If theVals(i) = theVals(i - 1) Then SquareRoot = theVals(i) Exit Function End If Next i End Function
KayJay: Why use an array? If you do not have a lot of memory, you might have a problem finding the square root of a really large number like one billion.
Iterating until an exact match is overkill. If two successive values match in the first 6-7 digits, you have about 12 or more correct digits.
Can you Redim an array with a Double, which might not be an integer? Does this program compile?
BTW: Can your program find the square root of two or three? It seems to me that you would only iterate once or twice.
Very true. It does not need an array, but this was not a final optimized routine. that said i'll optimize it. Yes to find out square roots of three and two are a problem with this. But we can use a "STEP" statement in the Loop to increment by 0.01 or 0.001 or whatever. An yes again the array cannot be dimensioned for as a double. But as U rightly pointed an array is not required at all. And yes this code will compile. :) And using the Round() function to test the results will make life easier.
http://www.vbforums.com/showthread.p...ber+squareroot