Sie sind auf Seite 1von 22

Lecture 1: Introduction To MATLAB

Prof. Dr. Salina A.


Samad
Mr. Iskandar Yahya
Information
2 Lectures (Thursday the 14th and 21st FEB)
Email: iskandar@vlsi.eng.ukm.my

Reserved computers with MATLAB application


at the Microcomputer Lab:
Monday – Thursday (2pm – 5pm)
Friday (3pm – 5pm)

MATLAB 7.5 (MATLAB R2007b)


History of MATLAB
Ancestral software to MATLAB
Fortran subroutines for solving linear (LINPACK)
and eigenvalue (EISPACK) problems
Developed primarily by Cleve Moler in the
1970’s

Later, when teaching courses in mathematics,


Moler wanted his students to be able to use
LINPACK and EISPACK without requiring
knowledge of Fortran

MATLAB developed as an interactive system


History of MATLAB
MATLAB gained popularity primarily through
word of mouth because it was not officially
distributed
In the 1980’s, MATLAB was rewritten in C with
more functionality (such as plotting routines)

The Mathworks, Inc. was created in 1984


The Mathworks is now responsible for
development, sale, and support for MATLAB
The Mathworks is located in Natick,
Massachusetts MA, USA.
MATLAB Environment
When you open MATLAB, you will see this
window:

Variable lists
/Current
Directory Main
typing/command
window

Command
history
MATLAB Environment
MATLAB can function as normal calculator:

Can define variables in MATLAB:


>> a = 5;
>> b = 3; Can be any character or strings of
>> c = 8; character as long as it is not separated
>> x = a + b/c by “space”.
x= **But we cannot use names that have
5.3750 already been used by MATLAB as the
predefined functions and variables.
MATLAB Environment
To see the variables currently stored: WHO
>> who
Your variables are:
A a b c x

Detailed variable information: WHOS


>> whos
Name Size Bytes
Class

A 1x1 8 double
array
a 1x1 8 double
array
b 1x1 8 double
array
c 1x1 8 double
array
MATLAB Environment
To clear variables: CLEAR
>> clear a b c >> clear
>> who >> who
Your variables are: >>
A x

Saving and loading variables: SAVE/LOAD


>> save >> save test
Saving to: matlab.mat >> load test
>> load
Loading from: matlab.mat

MATLAB is Case-Sensitive. Be careful when


typing.
MATLAB Environment
Most powerful command in MATLAB: HELP
Try typing “help” - list of all help topics
You can get help of specific functions and
commands that you like
Try “help variables”, and also “help save
variables” etc.
All about MATLAB is accessible by clicking
START and then choose MATLAB Help.

Effectively, you can leave this lecture now


and learn everything from MATLAB Help.
On the MATLAB Help window, run demos.
MATLAB Environment
Built-in math functions:
MATLAB Environment
Predefined Variables:
“Ans”, “sin()” and “pi” are examples of
predefined variables. Do Not define a variable
with these names, or try to change them.
What other predefined variables are there?

Handle Complex numbers:


>> n = 3+4i;
>> m = 1+i;
>> mul = m*n
mul =
-1.0000 + 7.0000i
>> real(mul)
ans =
-1
MATLAB Environment
Matrices and Vectors:
Create matrices by using square brackets,
separate rows by semicolon, each element
separated
>> by2 coma
Matrix_A = [1 3 ; 4 5 6or
; space:
>> Matrix_B =
7 8 9] [9,8,7;6,5,4;3,2,1]
Matrix_A = Matrix_B =
1 2 3 9 8 7
4 5 6 6 5 4
7 8 9 3 2 1

Inverse a matrix using “inv(A)”, where “A” is


your matrix. Similarly, we can get the
Determinant by using “det(A)”.

Find out what other predefined functions are


MATLAB Environment
Strings:
Character strings are enclosed in single quote –
>> Y = 'MATLAB is very powerful‘
Y=
MATLAB is very powerful
To use quotes, use double single quotes for a
single quote, and one double quote for a double
quote (What?!) E.g:
>> word = 'did''t‘ >> word = 'did"t‘
word = word =
did't did"t

String indexing is used to refer to a specific


“letter” in the string:
MATLAB Environment
Example:
>> me = 'Iskandar‘
me =
Iskandar

>>me(1)
ans =
I

>>me(5)
ans =
n

>>me(8)
ans =
r
MATLAB Environment
Other string funtions:
length - Return the number of character
strings
strcmp - Compares two strings
str2num - Converts a string to a numerical
value, i.e. converts ‘123’ to 123.
strrep - Replaces characters in a string with
different characters (very useful in editing
programs and debugging)
upper - Converts a string to uppercase
lower - Converts a string to lowercase
MATLAB Environment
Input and Output statement:
Use semicolon at the end of your line to
suppress the display- matlab will carry out the
calculation without displaying the result. Try
this
 fprintf – Use this command to generate a text
>> fprintf('This is a test. \n')
line:
This is a test.

Thefprintf('
>> “\n” isThis
included
\n is \n ato\nprompt
test. \n ')the display to start a
This
new line, e.g.:
is
a
test.
MATLAB
Environment
fprintf – to display the value of variables, we
can include a “%g” format character:
>> A = 3.45
A=
3.4500

>> fprintf('The value of A is %g. \n',


A)
The value of A is 3.45.
Information from user can be asked using
input function:
>> num = input('Specify a number. \n');
Specify a number.
2
>> fprintf('The number u entered is %g. \n',
num);
The number u entered is 2.
MATLAB
Environment
Plotting in MATLAB Intro:
Use plot function. Example of plotting then
function y=|x|sin(x). Use linspace function to
generate domain variable x, i.e.
linspace(x1,x2,n).
This function generates n equally spaced values
from x1 to x2 and stores them in x as a one-
dimensional array with one row:
>> x = linspace(0,5,11)
x=
Columns 1 through 9
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000
3.5000 4.0000
Columns 10 through 11
4.5000 5.0000
MATLAB
Environment
Plotting in MATLAB Intro:
Use plot as plot(x,y). This function plots the
values stored in array y versus the array of x.
The two arrays must have the same number of
values:
>> x=linspace(-100,100,5000);
>> y=abs(x).*sin(x);
>> plot(x,y)

• The abs
function returns
the absolute
values of x.
•This is the
generated plot:
MATLAB
Environment
Plotting in MATLAB Intro:
We can also plot multiple traces on a single
plot:
>>
x=linspace(0,2*pi,100)
;
>> y1=sin(x);
>> y2=cos(x);
>> y3=sin(x).*sin(x);
>> plot(x,y1,x,y2,x,y3)
MATLAB
Environment
Plotting in MATLAB Intro:
We use xlable, ylable, title and legend
tocomplete the plot:
>> xlabel('x');
>> ylabel('y');
>> title('Various
trigonometric
Function');
>> legend('sin(x)',
'cos(x)',
'sin(x)*sin(x)');
• This example is
just a tiny fraction
of MATLAB’s full
range plotting
capabilities. We
shall see that later.
MATLAB
Environment
Predefined Functions in this chapter:

sin abs fprintf log help save


load sqrt real imag angle inv
rand det lentgh strcmp str2num upper
lower input plot linspace xlabel ylabel
title legend lookfor which roots

Das könnte Ihnen auch gefallen