Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

3_MATLAB_Introduction

MATLAB is a technical computing language primarily used for math computations, modeling, data analysis, and algorithm development, with a focus on matrix operations. The environment includes key components like the Command Window, Workspace, and Editor, and supports various arithmetic and built-in functions. This document serves as an introduction to MATLAB's basics, including variable management, arithmetic operations, and essential commands.

Uploaded by

Sungaleli Yuen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

3_MATLAB_Introduction

MATLAB is a technical computing language primarily used for math computations, modeling, data analysis, and algorithm development, with a focus on matrix operations. The environment includes key components like the Command Window, Workspace, and Editor, and supports various arithmetic and built-in functions. This document serves as an introduction to MATLAB's basics, including variable management, arithmetic operations, and essential commands.

Uploaded by

Sungaleli Yuen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

MATLAB

GET STARTED WITH MATLAB


Introduction
 MATLAB is a powerful language for technical computing. The
name MAT-LAB stands for MATrix LABoratory, because its
basic data element is a matrix (array).
 MATLAB can be used for math computations, modeling and
simulations, data analysis and processing, visualization and
graphics, and algorithm development
 MATLAB is widely used in universities and colleges in
introductory and advanced courses in mathematics, science,
and especially engineering
Introduction

 In industry the software is used in research, development, and


design. The standard MATLAB program has tools (functions)
that can be used to solve common problems

 In addition, MATLAB has optional toolboxes that are collections


of specialized programs designed to solve specific types of
problems. Examples include toolboxes for signal processing,
symbolic calculations, and control systems.
Introduction

 MATLAB is a huge program, and therefore it is impossible to


cover all of it in one semester

 This course focuses primarily on the foundations of MATLAB

 The assumption is that once these foundations are well


understood, the student will be able to learn advanced topics
easily by using the information in he Help menu
MATLAB Environment
 The MATLAB environment consists of the following main parts
1. Command window
2. Command history
3. Workspace
4. Current folder
5. Editor
6. Help
7. Layout
8. Simulink
9. Add -Ons
MATLAB Environment
8 7 9 6
5

3
4
1
2
Command Window
 The Command Window is the main window in MATLAB. Use the
Command Window to enter variables and to run functions and M-files
scripts (more about m-files later
Command Window
Notes for working in the Command Window:-

 To type a command, the cursor must be placed next to the


command prompt ( >> )

 Once a command is typed and the Enter key is pressed, the


command is executed.

 Several commands can be typed in the same line. This is done by


typing a comma between the commands. When the Enter key is
pressed, the commands are executed in order from left to right.
Cont.. .
 It is not possible to go back to a previous line that is displayed in
the Command Window, make a correction, and then re-execute
the command
 A previously typed command can be recalled to the command
prompt with the up-arrow key (↑)
 When the command is displayed at the command prompt, it can
be modified if needed and then executed. The down-arrow key
(↓) can be used to move down the list of previously typed
commands
Cont.. .

 If a command is too long to fit in one line, it can be


continued to the next line by typing three periods … (called
an ellipsis) and pressing the Enter key

 The continuation of the command is then typed in the new


line.

 The command can continue line after line up to a total of


4,096 characters.
Keep in Mind !!
o The semicolon ( ; )
If a semicolon ( ; ) is typed at the end of a command, the output
of the command is not displayed. This is useful when the result is
obvious or known, or when the output is very large
o Typing %
When the symbol % (percent) is typed at the beginning of a line,
the line is designated as a comment, But the % character typed
after a command (in the same line) this has no effect on the
execution of the command
Cont.. .
o The clc command

The clc command (type clc and press Enter), once the clc
command is executed clears the Command Window and a clear
window is displayed
Command History
 Statements you enter in the Command Window are logged in the
Command History

 From the Command History, you can


view and search for previously run
statements, as well as copy and
execute selected statements

 You can also create an M-file from


selected statements
Workspace
 The Workspace window list all your variables used as long you
have MATLAB opened
You could also use the following
command
 >> who: this command displays the
variables currently in the workspace
 >> whos: this command displays the
variables currently in the workspace
and information about their size and
other information
 >> clear: this command clears all the
variables in your workplace.
Current Folder

The “Current Folder” window lists all m-files,


etc. available in the current directory
Editor

 The Editor is used to create scripts and


m-files. Click the “New Script” button
in the Toolbar

 For training purpose use “ New Live


Script” no need of saving and editing is
easy
MATLAB BASICS
Basic Operations
⁃ Variables are defined with the assignment operator, “=”. MATLAB is
dynamically typed, meaning that variables can be assigned without
declaring their type, and that their type can change

⁃ Values can come from constants, from computation involving values of


other variables, or from the output of a function

Note!

 MATLAB is case sensitive! The variables x and X are not the same

 Unlike many other languages, where the semicolon is used to terminate


commands, in MATLAB the semicolon serves to suppress the output of
the line that it concludes
Arithmetic Operations with Scalars
The symbols of arithmetic operations are
Operation Symbol Example

Addition + 5+3
Subtraction – 5–3
Multiplication * 5*3
Right division / 5/3
Left division \ 5\3=3/5
Exponentiation ^
Order of Precedence
MATLAB executes the calculations according to the order of
precedence displayed below. This order is the same as used in most
calculators

Precedence Mathematical Operation


First Parentheses. For nested parentheses, the
innermost are executed first (brackets)
Second Exponentiation
Third Multiplication, division (equal precedence)
Fourth Addition and subtraction
Display Formats
The user can control the format in which MATLAB displays output
on the screen.
Command Description
format short Short fixed point format with 4 digits after the
decimal point
format long Long fixed point format with 15 digits after the
decimal point for double values and 7 digits after the
decimal point for single values
format short e Short scientific notation with 4 digits after the
decimal point
format long e Long scientific notation with 15 digits after the
decimal point for double values and 7 digits after the
decimal point for single values
Cont.. .
Command Description
format short g Short fixed format or scientific notation, whichever is
more compact, with a total of 5 digits
format long g Long fixed format or scientific notation, whichever is
more compact, with a total of 15 digits for double
values and 7 digits for single values
format bank Currency format with 2 digits after the decimal point
format compact Suppress excess blank lines to show more output
format loose Add blank lines to make output more readable
Cont.. .
Example:

 >> format short, pi, single(pi) %displays both double and single
pi with 5 digits as 3.1416

while

 >> format long, pi, single(pi) % displays pi as


3.141592653589793 and single(pi) as 3.1415927.
Elementary Math Built-In Functions
 In addition to basic arithmetic operations, expressions in
MATLAB can include functions. MATLAB have a very large
library of built-in functions. A function has a name and an
argument in parentheses (in brackets)

- When the function is used, the argument can be a number, a


variable that has been assigned a numerical value, or a
computable expression that can be made up of numbers
and/or variables
Elementary math functions
Function Description
sqrt(x) Square root
nthroot (x,n) Real nth root of a real number x. (if x is negative
n must be an odd integer.)
exp (x)
abs (x) Absolute value
angle (x) Phase angle
log (x) Natural logarithm base e logarithm (ln)
log10 (x) Base 10 logarithm
conj (x) Complex conjugate
factorial (x) The factorial function x! ( x must be a positive
integer)
Trigonometric math functions
Function Description
sin (x) Sine of angle x (x in radians)
sind (x) Sine of angle x ( x in degrees)
cos (x) cosine of angle x (x in radians)
cosd (x) cosine of angle x ( x in degrees)
tan (x) tangent of angle x (x in radians)
tand (x) tangent of angle x ( x in degrees)
cot (x) cotangent of angle x (x in radians)
cotd (x) cotangent of angle x ( x in degrees)
Elementary Math Built-In Functions

 The inverse trigonometric functions are;

- asin(x), acos(x),atan(x), acot(x) for the angle in radians and

- asind(x), acosd(x), atand(x), acotd(x) for the angle in degrees

 The hyperbolic trigonometric functions are sinh(x), cosh(x),


tanh(x), and coth(x)
Rounding functions

Function Description

round (x) Round to the nearest integer

fix (x) Round toward zero

ceil (x) Round toward infinity

floor (x) Round toward minus infinity

rem (x,y) Returns the reminder after x is divided by y

sign (x)
Rules about Variable names
A variable can be named according to the following rules:-

- Must begin with a letter

- Can be up to 63 characters long

- Can contain letters, digits, and the underscore character

- Cannot contain punctuation characters (e.g., period, comma,


semicolon)

- MATLAB is case-sensitive: it distinguishes between


uppercase and lowercase letters. For example, AA, Aa, aA,
and aa are the names of four different variables
Cont.. .

- No spaces are allowed between characters (use the


underscore where a space is desired)

- Avoid using the name of a built-in function for a variable


(i.e. cos, sin, exp, sqrt, etc.). Once a function name is used to
for a variable name, the function cannot be used.
Useful commands for managing variables
 The following are commands that can be used to eliminate
variables or to obtain information about variables that have been
created
Command Outcome
clear Removes all variables from the memory
clear x y z Removes only variables x, y and z from the memory
who Displays a list of the variables currently in the
memory
whos Displays a list of the variables currently in the
memory and their sizes together with information
about their bytes and class
Built-in constants
 MATLAB have several built-in constants. Some of them are
explained here

Name Description
pi

inf
NaN Not a Number
i, j Used for complex numbers

You might also like