This action might not be possible to undo. Are you sure you want to continue?
Prof. Dr. Salina A. Samad Mr. Iskandar Yahya
2 Lectures (Thursday the 14th and 21st FEB) Email: email@example.com 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.
When you open MATLAB, you will see this
Variable lists /Current Directory
Main typing/command window
MATLAB can function as normal calculator:
Can define variables in MATLAB:
>> a = 5; >> b = 3; >> c = 8; >> x = a + b/c x= 5.3750 Can be any character or strings of character as long as it is not separated by “space”. **But we cannot use names that have already been used by MATLAB as the predefined functions and variables.
To see the variables currently stored: WHO
>> who Your variables are: A a b c x
Detailed variable information: WHOS
>> whos Name Size Class A array a array b array c array 1x1 1x1 1x1 1x1 Bytes 8 double 8 double 8 double 8 double
To clear variables: CLEAR
>> clear a b c >> who Your variables are: A x >> clear >> who >>
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
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.
Built-in math functions:
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
Matrices and Vectors: Create matrices by using square brackets, separate rows by semicolon, each element separated by 3 ; 4 5 ; >> Matrix_A = [1 2 coma6or space: Matrix_B = >>
7 8 9] Matrix_A = 1 2 3 4 5 6 7 8 9 [9,8,7;6,5,4;3,2,1] Matrix_B = 9 8 7 6 5 4 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
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 = 'did"t‘ word = did"t
String indexing is used to refer to a specific
“letter” in the string:
>> me = 'Iskandar‘ me = Iskandar >>me(1) ans = I >>me(5) ans = n >>me(8) ans = r
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
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: is a test. This
>> fprintf('is included a \n test. \n ') The “\n” This \n is \n to prompt the
display to start a
This line, e.g.: new is a test.
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
>> 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.
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 onedimensional array with one row: >> x = linspace(0,5,11)
x= Columns 1 through 9 0 0.5000 1.0000 1.5000 3.5000 4.0000 Columns 10 through 11 4.5000 5.0000 2.0000 2.5000 3.0000
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:
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)
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.
Predefined Functions in this chapter:
sin load rand lower title abs sqrt det input legend fprintf real lentgh plot lookfor log imag strcmp linspace which help angle str2num xlabel roots save inv upper ylabel