Interduction to MATLAB Manal Alotaibi Mathematics department College
Interduction to MATLAB • Manal Alotaibi Mathematics department College of science King saud university
Content q. What is MATLAB? q. The MATLAB desktop invironment. q. MATLAB as calculator. q. Variables. q. Algorithm q. Interduction to m-file function. MATLAB
Outline q What is MATLAB? q The MATLAB desktop invironment. q MATLAB as calculator. q Variables. q Algorithm q Interduction to m-file function. MATLAB
• MATLAB is a mathematical and graphical software package with numerical, graphical and programming capabilities. • it has built-in functions to perform many operations. MATLAB
Outline q What is MATLAB? q The MATLAB desktop invironment. q MATLAB as calculator. q Variables. q Algorithm q Interduction to m-file function. MATLAB
MATLAB
Command window: Type your instructions here and press ENTER to execute them. MATLAB
Example: Declare a column matrix with values 1, 2 and 3. MATLAB
Command history: a list of instructions executed by MATLAB is shown here. MATLAB
Workspace: shows a list of variables created by MATLAB. As you can see, the value of ‘aaa’ is shown. MATLAB
Another way to create a variable Is to press this button. MATLAB
MATLAB will prompt you to enter the variable name. MATLAB
As you can see, the variable name has been changed to bbb. MATLAB
2) Or by double clicking on bbb. To assign a value to bbb, you can do it in two ways: 1) Using the command window. MATLAB
To clear all variables from memory and close all figures, use the clear, close all command. MATLAB
As you can see, all workspace variables are deleted when you execute this command. MATLAB
To clear the command window, use the clc (clear console) command. MATLAB
As you can see, all console entries are deleted when you execute this command. MATLAB
If you want to see help, you can type help at the command window. MATLAB
Outline q What is MATLAB? q The MATLAB desktop invironment. q MATLAB as calculator. q Variables. q Algorithm q Interduction to m-file function. MATLAB
Operators addition + a+b subtraction-a-b multiplication*a*b Division(left) / a/b Division(right) ab (b a) power^a^b MATLAB • • •
Operator precedence rules 1. 2. 3. 4. 5. () parentheses. ^ power. - negation. */ all multiplication and division. + - addition and subtraction. MATLAB
Examples >> 6*5 ans = 30 • >> 17/5 ans = 3. 4000 >> ans = 3. 4000 MATLAB >> x=6*5 x= 30 >> y=17/5; >> y y= 3. 4000 >> z=x+y z= 33. 4000 >> who Your variables are: x y z
Some functions in MATLAB • • exp(x) sin(x) asin(x) log 10(x) sqrt(x) abs(x) sum(x) MATLAB ex sin-1 x ln x log x |x| • • pi i, j Na. N Inf p imaginary unit Not-a-Number
Example >>y=x^0. 5; >>y=x^(1/2); >>y=sqrt(x); MATLAB
Outline q. What is MATLAB? q. The MATLAB desktop invironment. q. MATLAB as calculator. q. Variables. q. Algorithm q. Interduction to m-file. MATLAB
Variables Creating Variables: – Names • Can be any string of upper and lower case letters along with numbers and underscores but it must begin with a letter • Reserved names are IF, WHILE, ELSE, END, SUM, etc. • Names are case sensitive – Value • This is the data the is associated to the variable; the data is accessed by using the name. – Variables have the type of the last thing assigned to them • Re-assignment is done silently – there are no warnings if you overwrite a variable with something of a different type. MATLAB
Outline q. What is MATLAB? q. The MATLAB desktop invironment. q. MATLAB as calculator. q. Variables. q. Algorithm q. Interduction to m-file MATLAB
Definition of algorithm • An algorithm is the sequence of steps needed to solve a problem. MATLAB
Outline q. What is MATLAB? q. The MATLAB desktop invironment. q. MATLAB as calculator. q. Variables. q. Algorithm q. Interduction to m-file. MATLAB
To create an m-file, 1) type edit at the command window, or 2) Press this button. MATLAB
The previous command will display the editor window. The editor creates an m-file that can be used to write your MATLAB programs. MATLAB
To execute a program, press the RUN button. MATLAB
End the first part
- Slides: 34