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

Lecture 01

Uploaded by

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

Lecture 01

Uploaded by

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

AMAT 21552

Scientific computing using


appropriate software I

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

MATLAB IS AN INTEGRATED TECHNICAL COMPUTING


ENVIRONMENT THAT COMBINES NUMERIC COMPUTATION,
ADVANCED GRAPHICS AND VISUALIZATION, AND A HIGH LEVEL
PROGRAMMING LANGUAGE.
WWW.MATHWORKS.COM/PRODUCTS/MATLAB
Introduction
MATLAB is the short form of MATrix LABoratory which is a high-
performance language for technical computing. It integrates computation,
visualization, and programming in an easy-to-use environment where problems
and solutions are expressed in familiar mathematical notation. Typical uses
include:
✓ Math and computation
✓ Algorithm development
✓ Modeling, simulation, and prototyping
✓ Data analysis, exploration, and visualization
✓ Scientific and engineering graphics
✓ Application development, including Graphical User Interface building

Starting MATLAB

❖ You may have an icon set up on your desktop that enables


you to start MATLAB with a simple double-click.
Introduction

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

Scientific Programming Language


•Powerful mathematics-oriented syntax with built-in 2D/3D plotting and visualization
tools.
•Free software, runs on GNU/Linux, macOS, BSD, and Microsoft Windows.
•Drop-in compatible with many Matlab scripts.
•Octave is free software licensed under the GNU General Public License (GPL).

Free MATLAB Trail

9
Introduction
The MATLAB Environment
❖ The MATLAB window (is called the MATLAB main desktop) looks:

❖ MATLAB uses three primary windows:


• Command window: Used to enter commands and data.
• Graphics window: Used to display plots and graphs.
• Edit window: Used to create and edit M-files.

❖ The MATLAB main desktop has many areas (e.g. Title


bar, Menu bar, etc.) and windows.
Introduction
❖ The following windows are important parts of the MATLAB main desktop.

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.

Text based help


Text based help can be obtained from
the command prompt by typing
help. The help topics appear as in
Figure.
Introduction

Graphical interface help


This is a separate window and it is launched from the menu bar,
Help Matlab help, the main desktop toolbar by pressing the question mark
or by typing helpbrowser in the command prompt.

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

❖ The command “lookfor” can be used to search for a specific keyword.


Introduction

✓ We will start learning how to use MATLAB to do mathematics.

✓ Try the commands in a MATLAB Command Window as you go along.

✓ 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.

Input and output

❖ You input commands to MATLAB in the MATLAB Command Window.


MATLAB returns output in two ways: Typically, text or numerical output
is returned in the same Command Window, but graphical output appears in
a separate graphics window.
Introduction

To generate this screen


on your computer,
first type 1/2 + 1/3.
Then type
ezplot(’xˆ3 - x’).

➢ While MATLAB is working, it may display a “wait” symbol.


➢ Echo printing is a characteristic of MATLAB. It can be suppressed by
terminating the command line with the semicolon (;) character.
Arithmetic Operator &
Their Precedence

❖ The most common arithmetic operations in MATLAB are

❖ This is just like on a calculator, in spreadsheets (Excel) and most


programming languages.
Arithmetic Operator &
Their Precedence
In MATLAB, and many other programming languages, operations are
performed in the following order:

1. expressions in brackets: ( )
2. powers: ^
3. multiplication and division: * , /
4. addition and subtraction: + , -

Operations of the same precedence, for example multiplication and division,


are evaluated from left to right.

MATLAB evaluates arithmetic expressions precisely by the rules for order


of precedence. Use brackets as needed to get the correct expression or
make the expression clear!
Using MATLAB as a calculator

❖ After starting MATLAB, the command window will open with the
command prompt being displayed
>>

❖ The calculator mode of MATLAB operates in a sequential fashion as


you type in commands line by line. For each command, you get a result.

❖ Thus, you can think of it as operating like a very fancy calculator.

❖ For example, if you type in


>> 55 - 16
MATLAB will display the result
ans =
39
Using MATLAB as a calculator

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)

1. >> 1+2*3 13. >> 2*-3


2. >> (1+2)*3 14. >> 2*(-3)
3. >> 4/2+1 15. >> 2^-3
4. >> 1+4/2 16. >> 2^(-3)
5. >> 4/(2+1) 17. >>3ˆ2 - (5 + 4)/2 + 6*3
6. >> 8^2/3
7. >> 8^(2/3) Self-test Exercise
8. >> 12/2/3 ➢ Write a MATLAB command to enter a
9. >> 12/(2*3) fraction with 17.1 + 20.3 in the numerator
10. >> 4^3^2 and 36.5 + 41.8 in the denominator.
11. >> 4^(3^2) ➢ Note that the answer is NOT "17.1 +
12. >> (4^3)^2 20.3/36.5 + 41.8". Why not?
➢ Answer: (17.1 + 20.3) / (36.5 + 41.8)
You must use brackets, otherwise the
division is done before either addition.
Error messages
➢ If we enter an expression incorrectly, MATLAB will return an error message. For
example, in the following, we left out the multiplication sign, *, in the following
Expression

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

To end your MATLAB session, type quit in the Command Window, or


select File Exit MATLAB in the desktop main menu.
Getting started
➢ Built-in Functions
➢ MATLAB Variables
➢ Overwriting variable
➢ Floating point numbers
MATLAB Bulit-in Functions

MATLAB and its Toolboxes have a rich collection of built-in


functions.

28
Some other commonly used
Built-in functions

29
Try …

30
❖ Observe that MATLAB assigns a new value to ans with each
calculation.

❖ The variable ans can be used to extend the calculations rather


than retype the answer.

31
Naming variables in MATLAB

❖ A variable name must begin with a letter which may be followed by


letters, digits, or underscores (no other special character allowed).

❖ MATLAB is case-sensitive which means that upper and lower case letters
are considered different. (‘GeeksForGeeks’ and ‘geeksforgeeks’ are
considered separate variable names)

❖ Avoid giving variables the same name as that of existing MATLAB


functions (run the "iskeyword" to obtain the complete list).

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.

 ‘sym’ can also be used to represent


numbers in their rational form.

 The command whos shows


information about all defined
variables, but it does not show the
values of the variables.

 To see the value of a variable, simply


type the name of the variable and
press ENTER or RETURN.

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.

✓ Then all subsequent numerical output

will have 15 digits displayed.


✓ Type format short to return to 5-digit

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.

 To perform symbolic computations, you must use syms to declare the


variables you plan to use to be symbolic variables. Consider the following
series of commands:

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.

❖ ‘sym’ can also be used to represent


numbers in their rational form.

39
❑ The command expand told MATLAB
to multiply out the expression, and factor
forced MATLAB to restore it to factored
form.

❑ MATLAB has a command called


simplify, which you can sometimes
use to express a formula as simply as
possible.

❑ MATLAB has a more robust command,


called simple, that sometimes does a
better job than simplify.

40
More….:

41

You might also like