GEOSC 444
Matlab Application for Geoscience

Lesson 1: Two little surprises that can trip you up

PrintPrint

Syntax introduced:

log log10 sin sind pi

One of the rare counterintuitive (to me anyway) commands is log. When I see "log" written I always assume base 10, whereas "ln" means natural log (base e). But that's not how MATLAB expresses it. In MATLAB,log means natural log. If you want base 10, use log10.

Surprise #2 is that the default for trig functions (sine, cosine, tangent, etc.) is radians. To convert to degrees instead, multiply your angle by pi/180 or else use sind. "pi" is a "special variable" in MATLAB. By default you can just type the word to use it. The "d" in sind to make MATLAB use degrees also works for other trig functions, like, for example, cosd and tand.

Here's a tip: If you are ever working with a programming language, spreadsheet program, or calculator and you aren't sure whether you are working in degrees or radians, just try calculating the trig function of an angle that has an obvious well-known answer, such as the sine of 90 degrees:

>> sin(90)
ans =
   0.8940
>> sin(90*pi/180)
ans =
   1
>> sind(90)
ans =
   1
The first time we tried it, we didn't get 1 as the answer so this means we know we must not be working in degrees. Therefore, either multiply 90 by pi/180 and take the sine of that quantity, or else use sind. We get back the answer 1, so now we know what we are doing.

Check yourself!