0 Replies - 1043 Views - Last Post: 18 February 2013 - 01:45 PM Rate Topic: -----

#1 CynicalWhiskey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-February 13

Computation Error Approximating Cos(x)

Posted 18 February 2013 - 01:45 PM

Hey, not sure if I'm posting this in the right spot, but here goes.

I wrote this program to take a degree variable from cell A2, say, "x" and spit out an approximate value for cos(x) and an exact value. I'm using VBA within Excel. However, the number I am getting for my Approximate Cos(x) is off.


Here is the relative part of my code:


'Variables
Dim Angle As Double
Dim RadianAngle As Double
Dim ApproxCos As Double
Dim ActualCos As Double
Dim TrueError As Double
Dim xrad As Double
Dim CosDegrees As Double

'Below Variables are For Output File
Dim a As Double
Dim b As Double
Dim c As Double
Dim d As Double
Dim e As Double
Dim f As Double

'Conversion for degrees to radians
Angle = Cells(2, "a").Value
RadianAngle = Angle * (4 * Atn(1)) / 180

'Output Radians to cell B2
Cells(2, "b").Value = FormatNumber(RadianAngle, 4)

'Approximate Cos(x)
xrad = RadianAngle
ApproxCos = 1 - xrad ^ 2 / 2! + xrad ^ 4 / 4! - xrad ^ 6 / 6! + xrad ^ 8 / 8!
Cells(2, "c").Value = FormatNumber(ApproxCos, 4)

'Conversion for Actual Cos(x)
ActualCos = Cos(xrad)
Cells(2, "d").Value = FormatNumber(ActualCos, 4)

'Output True Error
TrueError = Abs(ActualCos - ApproxCos)
Cells(2, "e").Value = FormatNumber(TrueError, 4)

'Output to File Where a = Angle in Degree, b = Radian angle, c = Approximated Cos(x), d = Actual Cos(x) and e = True Error
'Define Variables "a, b, c, d, e and f"

a = Cells(2, "a").Value
b = Cells(2, "b").Value
c = Cells(2, "c").Value
d = Cells(2, "d").Value
e = Cells(2, "e").Value
f = Cells(2, "f").Value



Is This A Good Question/Topic? 0
  • +

Page 1 of 1