Lab1 Introduction to Matlab I

Basic Operations

Date Due: 5pm Aug, 28, 2008

Goal

To get started with Matlab and perform some basic operations.

What is Matlab

Matlab is a software package for numerical computation and visualization. In this course, we will use Matlab as the tool for modeling of environmental systems. Matlab provides a user friendly environment that you can solve many kinds of problems related to scientific computation and modeling.

Launch Matlab

Go to the directory J:\isis\pc-pkg\matlab\bin\win32\, and double click MATLAB.exe to launch the program. A Matlab interface will appear as shown in the following:

.

Fig 1. The MATLAB interface

The big window to the right is the main Command Window where you will issue commands asking the software to perform certain operations. The smaller window to the left on the top showing the contents of your current working directory (the place where you Matlab will read and save files), and the one at the bottom showing you the historyof the commands you issued in the Command Window. The ‘Start’ button at the bottom to the left shows you the other functions provided by MATLAB, for instance, toolbox, etc.

On the top of the windows, there are a series of menus, which we will learn how to use them later on in the class. To the right on the tool bar, you can set your working directory in the space next to “Current Directory:” Please type your current directory as “J:\isis\html\courses\2008fall\geog\410\001\students\youronyen\ (may not be ready to use today, Sorry!)”. After you type in the directory, click the button next to it (with three dots at the bottom), and this will make the directory you entered as the current directory. You should be about to see the contents under the directory in the smaller window on the top to the left.

BasicOperations

Tip: Matlab is case sensitive. Please be careful when you type the commands.

The prompt in Matlab is “>”. Right next to that prompt

> 2+2

You will get:

ans =

4

Here “ans” is the default output variable. If you do

> 3*5

ans =

15

> 2^3

ans =

8

> 10/2

ans =

5

You see that “ans” only stores the result of the most recent operation. You lost the output in “ans” in the earlier operations. You can keep the output from an earlier operation by assigningan variable to store it and use it later on . For example, you can do

> x=2+2

x =

4

> y=3*5

y =

15

> z=2^3

z =

8

> Z=10/2

Now we can recall the value for each of the variable by just type the name of the variable and hit the enter key.

> x

x =

4

> y

y =

15

> z

z =

8

> Z

Z =

5

Note, the upper case “Z” and lower case “z” have different values as Matlab is case sensitive.

We can call the variables all at the same time by type the variable names in one line and separate them by “,”.

> x, y, z, Z

x =

4

y =

15

z =

8

Z =

5

In addition to these basic operations, we can also call the internal functions to use, such as the trigonometry functions, logarithmic, exponential functions etc. A function is use as “functionname()”, where the input value to the function is given inside the parentheses.

> sqrt(4)

ans =

2

“sqrt()” is a function that takes the square root of the input variable.

Note, when you use trigonometric functions, such as sine and cosine, the input is an angle measured in radiance. If you know the angle measured in degrees, you can do it as

> sin(30*pi/180)

ans =

0.5000

Where pi (π) =3.1416 (built in constant). The above express converts the angle in degrees to radiances first and then evaluated by the sine function. Similarly you can do

> cos(30*pi/180)

ans =

0.8660

> tan(30*pi/180)

ans =

0.5774

For exponential functions, we use exp(x) to calculate the xth power to e. e =2.718.

> exp(1)

ans =

2.7183

> exp(2)

ans =

7.3891

For logarithms, the natural logarithmis log(x) in MATLAB, and (log ten of x) is log10(x);

> log(2.718)

ans =

0.9999

> log(1)

ans =

0

> log10(1)

ans =

0

> log10(10)

ans =

1

We know that ln(1) and lg(1) are both 0.

Exercises

  1. Arithmetic operations:
  • where r =2.0, and π is pi in Matlab.
  1. Exponential and logarithms: The mathematical functions and are calculated with Matlab functions asexp(x), log(x), and log10(x);
  1. Trigonometry: The basic Matlab trig functions are sin(), cos(), tan(), cot(), and The argument of these functions must be in radians.
  • sin(60o)
  • cos(30o)
  • cos(π)
  • .

Lab report

Please copy and paste from your Matlab command window the commands you issued to solve each of the problem above to the MS Word processor. Save it as youronyen-lob1.doc at your student directory(J:\isis\html\courses\2008fall\geog\410\001\students\youronyen (not available now, but should be available soon). Please do not print your lab report. You should not modify your lab report after the deadline, otherwise it will make your lab report showing late.

1