You are on page 1of 5

MECH374 Lab Class 1: Introduction

1. How to start:
a) Click the icon on the desktop;
b) Start->run->Matlab

2. Matlab Environment
a) Command window

b) Graphics window
c) Edit window

3. Exercises:
Do these exercises in the command window:
a) >>2+4
ans=
6
>>ans+5
ans=
11

Notice that MATLAB has automatically assigned the answer to a variable, ans. Thus, you
could use ans in a subsequent calculation.

b)
>>a=4
a=
4
>>b=6;
>>c=8,d=0;
c=
8

You can type several commands on the same line by separating them with commas or
semicolons. If you separate them with commas, they will be displayed, and if you use the
semicolon, they will not.

c)
>>a=8;A=12
Enter
>>a
Then, enter
>>A

MATLAB treats names in a case-sensitive manner-that is , the name a is not the same as the
name A.

d) Complex values
>>x=1+5*i
>>y=2+9*j
MATLAB allows the symbol j to be used to represent the unit imaginary number for input.
However, it always uses an i for display.
e) Predefined variables
>>pi
>>format long
>>pi
>>format short
>>pi
Some of the predefined variables

f) Vectors and Matrices


Row vector:
>>a=[1 2 3 4 5]

Column vector:
>>b=[2;4;6;8;10]
>>b=[2;
4;
6;
8;
10]
>>b=[2 4 6 8 10]

Matrices:
>>A=[1 2 3;4 5 6;7 8 9]
>>A=[1 2 3
456
7 8 9]

ONES and ZEROS functions:


>>E=zeros(2,3)
>>u=ones(1,3)

g) Colon Operator
>>t=1:5
>>t=1:0.5:3
>>t=10:-1:5
The colon can also be used as a wildcard to select the individual rows and columns of
a matrix.
>>A(2,:)
We can also use the colon notation to selectively extract a series of elements from
within an array.
>>t(2:5)

h) Linspace
The linspace function generates a row vector of equally spaced points.

linspace(x1,x2,n)

It generates n points between x1 and x2. If n is omitted, the function automatically


generates 100 points.
>> linspace(0,1,6)

i) Logspace
The logspace function generates a row vector that is logarithmically equally spaced.

logspace(x1,x2,n)

It generates n logarithmically equally spaced points between 10 x1 and 10 x 2 .


>> linspace(-1,2,4)
j)
Basic Mathematical Operations
Addition + 2+3
Subtraction - 4-2
Multiplication * 2*3
Division / or \ 2/3 or 3\2
Exponentiation ^ 2^3

h) Graphics
>>t =[0:2:20]
>>g=9.81;m=68.1;cd=0.25;
>>v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t)
>>plot(t,v)

You might also like