Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Sine Cosine and Tan in Excel. 3

Status
Not open for further replies.

Roadbridge

Civil/Environmental
Apr 20, 2005
116
Can any one give me guidance in using Sine/Cosine/Tan in excel.
Example.

1000*Cosq and so on.

How do you write it in excel format?
 
Replies continue below

Recommended for you

- Place the value that you want to take the sin, cos, or tan, in cell A1.

- In cell B1 type "=1000 * sin(A1)". For cos, use cos(A1). For tan, use tan(A1).
 
Also, my excel uses radians (maybe there is a way to set the default to degrees?)

To convert degrees to radians, multiply by Pi/180

For example to find the cos of cell G20 which contains an angle in degrees, use the following:

=+COS(PI()/180*G20)

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
You can also use degrees() and radians() to do the unit conversions, so sin(radians(90)) returns 1

TTFN
 
There are a couple of worksheet functions that deal with the degrees - radians issue.

=RADIANS(C2) equals 4.71238898 radians when C2 = 270 degrees (3pi/2 radians)

=DEGREES(C6) equals 179.999848 degrees when C6 = 3.14159 radians


Norm
 
It would just be a lot easier if the darn programmers of excel would let you default to degrees like the calculators do. This is always the pain in the tukus when dealing with trig functions in excel - or any spreadsheet that I've seen thus far.
[cheers]
 
I thought it would be extremely easy to write your own functions in VBA to use degrees as input. However, it looks like the BASIC trigonometric functions (sin, cos, tan) are missing in VBA for Excel, even though some of the more esoteric functions (sinh, cosh, etc) are supported!

I can’t get the following VBA Macro to work – it generates a "#VALUE!" error:

Public Function sindeg(theta_degrees)
Pi=Excel.WorksheetFunction.Atan2(1,1)*4
sindeg=Excel.WorksheetFunction.Sin(theta_degrees/180*Pi)
End Function

However, the following macro (with just the addition of one character, to convert “sin” to “sinh”) runs fine. (It’s just a shame that it generates the wrong answer!)

Public Function sindeg(theta_degrees)
Pi=Excel.WorksheetFunction.Atan2(1,1)*4
sindeg=Excel.WorksheetFunction.Sinh(theta_degrees/180*Pi)
End Function

Does anyone know a way around this apparent fundamental limitation of VBA?

 
sin works fine in VBA on my puter

Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
GregLocock,

What version of Excel are you running?

I am beginning to suspect the omission of the basic trig functions (sin etc) in VBA is a new "enhancement" in the 2003 version. I have found plenty of references to using trig functions in VBA in older versions, but the examples won't run on my 2003 version.

For example, Microsoft on-line help defines "SIN" in the 1997 VBA reference here:


However, the 2003 on-line reference provides a list of mathematical functions available in VBA - see here:


The hyperbolic functions (SINH, COSH, etc) are listed, as are the inverse trig functions (ASIN, ACOS, etc), but the basic trig functions (sin, cos, tan) are conspicuous by their absence. Sure enough, I can run a VBA function which uses SINH, but when I try to use SIN, it generates an error message.

Surely I am missing something obvious?!?
 
'97. Good catch.

Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
I have Excel 2003

In the worksheet, =sin(90) yields a correct answer

In VBA, Range("A1").Value = Sin(90) puts the correct value in cell A1

Perhaps you have to load the analysis pack in Excel?
 
OK – I think I have worked out the answer to my own question. (Thanks to GregLocock and IFRs!)

It seems there is a group of core mathematical functions which are built into the core of VBA. These functions include Sin, Cos, Tan, etc, and the correct syntax is the normal method of calling such a mathematical function:

sindeg = Sin(theta_degrees / 180 * Pi)

(Note that the syntax for the VBA arc-tan function is Atn(x) NOT Atan(x), which is the Excel syntax.)

Then, you can access MOST (but not all) Excel mathematical functions using the following syntax:

answer = Excel.WorksheetFunction.Sinh(x)

A list of Excel functions which are available in this way can be found by opening Visual Basic help, and then search down through the contents for:

Microsoft Excel Visual Basic Reference
Programming Concepts
Events, Worksheet Functions & Shapes
Using Microsoft Excel Worksheet functions in Visual Basic

and then click on the link to “List of Worksheet Functions Available to Visual Basic”. These available functions include the hyperbolic functions (Sinh, Cosh, etc), Acos, Asin, Atan2 (but NOT Atan!), and so on.

Note that it seems that in general, if any function is built into the core of VBA, the Excel version is NOT available in this way. That is:

sindeg = Sin(theta_degrees / 180 * Pi)

will work, but

sindeg = Excel.WorksheetFunction.Sin(theta_degrees / 180 * Pi)

will generate an error.

Sorry if this is obvious to others, but it caused me a LOT of grief working out where I went wrong.

For the record, the following VBA code runs fine. (Note – this is just a demonstration – I KNOW it’s not the most efficient way to achieve my objectives! Note also, I have used the excel Function Pi() to create a local variable called Pi. This is probably very poor programming form, but is allowable here, because VBA does not seem to recognise Pi as a reserved keyword, unless it is in the Excel.WorksheetFunction.Pi() syntax.)

Public Function sindeg(theta_degrees)
Pi = Excel.WorksheetFunction.Pi()
sindeg = Sin(theta_degrees / 180 * Pi)
End Function
 
To avoid rounding errors, I have resorted to using degrees for input and result output, and letting Excel do all the intermediate calcs in 'native' radians mode.



Bung
Life is non-linear...
 
Great info Julian. I was about to give you a star and I noticed an ironic twist....

The only post with a star in this thread is a post which was a passing comment by it's nature no constructive info intended or included

I had to pause to point out the irony before I deliver my star which will destroy the evidence.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
Here is some more info:

Code:
Excel Trig Functions

SIN(number)
SIN(30) equals -0.98803, the sine of 30 radians
SIN(RADIANS(30)) equals 0.5, the sine of 30°
Example: =SIN(RADIANS(30))

COS(number)
COS(1.5) equals 0.07074, the cosine of 1.5 radians
COS(RADIANS(1.5)) equals 0.99966, the sine of 1.5°
Example: =COS(RADIANS(1.5))

TAN(number)
TAN(2) equals -2.18504, the tangent of 2 radians
TAN(RADIANS(2)) equals 0.03492, the tangent of 2°
Example: =TAN(RADIANS(2))

ASIN(number)
ASIN(0.5) equals 0.523599 radians
DEGREES(ASIN(0.5)) equals 30°, the arcsine of 0.5
Example: =DEGREES(ASIN(0.5))

ACOS(number)
ACOS(-0.5) equals 2.09440 radians
DEGREES(ACOS(-0.5)) equals 120°, the arccosine of -0.5
Example: =DEGREES(ACOS(-0.5))

ATAN(number)
ATAN(1) equals 0.785398 radians
DEGREES(ATAN(1)) equals 45°, the arctangent of 1
Example: =DEGREES(ATAN(1))

Flores
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor