Lecture 01
Lecture 01
Department of Mathematics
University of Kelaniya
Introduction to MATLAB
Learning Outcomes:
On completion of this unit, the student should be able to write computer
programs to solve structured engineering and mathematical problems using
appropriate numerical techniques in software environment.
Course Contents:
Introduction to software: software as a calculator, Basic Mathematical
functions, Vectors, Accessing elements of arrays and array manipulation, Scripts
and function files, plotting simple functions.
Programming: Loops and conditional statements, specific commands, error
checking and displaying.
Applications: Roots of nonlinear equations using and Graphical Methods,
Bisection Method, False Position Method, Fixed-point Method, Newton
Raphson Method, Secant Method.
Recommended
Texts:
Event Code
484-677-350
Enter this code at
app.gosoapbox.com
4
Starting MATLAB
MATLAB Windows
Online Help
Interrupting Calculations
Ending a Session
5
Introduction
Starting MATLAB
MATLAB mobile
❖ The following link can be used to obtain the MATLAB mobile version
https://play.google.com/store/apps/details?id=com.mathworks.matlabm
obile&hl=en
❖ First download the app and install. Then create a MathWorks account
using your email.
Octave
9
Introduction
The MATLAB Environment
❖ The MATLAB window (is called the MATLAB main desktop) looks:
Command Window
The user enter commands and obtain results in the command window (that is, input
and output appear). Each new line on the command window starts with the symbol
>> (prompt) which denotes where new input can be entered.
Current Folder
This window allows to access the project folders and files.
Workspace
The loaded variables (including created) of the current MATLAB session
are displayed.
Command History
The command history contains the history of the commands entered in
the command window.
Introduction
Help facilities
❖ The user can use help facilities to find out information about syntax and
functionality and see working examples. There are three ways the user can
get help in MATLAB:
text based help, graphical help interface and web based help.
Web-based help
Help in MATLAB can also be obtained from the web. This appears in the
graphical help interface by typing in the command prompt:
For example: >> web http://www.mathworks.com
❖ The command help can also be used to find out information about a
specific function.
For example: >> help cos
Introduction
✓ Feel free to experiment with variants of the examples we present; the best
way to find out how MATLAB responds to a command is to try it.
1. expressions in brackets: ( )
2. powers: ^
3. multiplication and division: * , /
4. addition and subtraction: + , -
❖ After starting MATLAB, the command window will open with the
command prompt being displayed
>>
Let's suppose you want to calculate the expression, 1 + 4 ×3. You type it at the
prompt command (>>) as follows,
>>1+4*3
ans =
13
❖ Noticed that if you do not specify an output variable, MATLAB uses a default
variable ans , short for answer, to store the results of the current calculation.
Note that the variable ans is created (or overwritten, if it is already existed).
To avoid this, you may assign a value to a variable(will be discussed later) or
output argument name.
>>x=1+4*3
x =
13
Try Parentheses can be used to override the
>> 2*pi priorities
ans = >> y = (-5) ^ 2
6.2832 y =
25
Also, scalar real variables can be included:
>> y = pi/4; Calculations can also involve complex
>> y ^ 2.45 quantities. Here are some examples that
ans = use the values of 𝑥 (2 + 4𝑖)
0.5533
>> 3 * x
Since exponentiation has higher priority then
ans =
negation, the following result would be
6.0000 + 12.0000i
obtained:
>> y = -5 ^ 2 >> 1 / x
y = ans =
-25 0.1000 - 0.2000i
Thus, 5 is first squared and then negated. >> x ^ 2
ans =
-12.0000 + 16.0000i
Arithmetic Operator &
Their Precedence
WorkSheet # 01(HW)
Making corrections
To make corrections, we can, of course retype the expressions. But if the expression is
lengthy, we make more mistakes by typing a second time. A previously typed command
can be recalled with the up-arrow key in the MATALB desktop version. When the
command is displayed at the command prompt, it can be modified if needed and
executed. Useful Keyboard Shortcuts
• ↑ – Go back one command
• ↓ – Go forward one command
• TAB – Shows a list of similar commands
In the app, using history icon , select the previous command/s , then copy to clipboard
and then paste, so you can edit.
Example:
Quitting MATLAB
28
Some other commonly used
Built-in functions
29
Try …
30
❖ Observe that MATLAB assigns a new value to ans with each
calculation.
31
Naming variables in MATLAB
❖ MATLAB is case-sensitive which means that upper and lower case letters
are considered different. (‘GeeksForGeeks’ and ‘geeksforgeeks’ are
considered separate variable names)
32
33
To do more complex
calculations, you can
assign computed
values to variables
of your choosing.
For example,
34
Mary goes to the office supply store and buys four erasers at 25 cents
each, six memo pads at 52 cents each, and two rolls of tape at 99 cents.
How many items did Mary buy, and how much did they cost?
?
When MATLAB
performs a
calculation, it
does so using the
values that it
knows at the time
the requested
command is
evaluated.
35
Try assigning u = pi, v = pi, and w
= sym(pi), and then type whos to see
how the different data types are
described.
36
✓ MATLAB uses double-precision floating point
arithmetic, which is accurate to approximately 15
digits; however, MATLAB displays only 5 digits by
default.
✓ To display more digits, type format long.
display.
37
Using MATLAB’s Symbolic MathToolbox, you can carry out algebraic or
symbolic calculations such as factoring polynomials or solving algebraic
equations.
Type help symbolic to make sure that the Symbolic Math Toolbox is
installed on your system.
38
❖ Symbolic variables can further be
combined into equations (‘f’). If we
want to evaluate these functions, then
use a substitute function (‘subs’) to
replace the variables with values.
39
❑ The command expand told MATLAB
to multiply out the expression, and factor
forced MATLAB to restore it to factored
form.
40
More….:
41