programming (matlab)ย ยท programming (matlab) chapter one: ... the matlab environment 10/2/2019...

18
Programming (Matlab) Chapter One: MATLAB Basics . 2019 โ€“ 2020 10/2/2019 1 Matlab [email protected] / 04

Upload: others

Post on 13-Oct-2020

87 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

Programming (Matlab)Chapter One:

MATLAB Basics

๐‘ซ๐’Š๐’š๐’‚๐’“ ๐‘จ.๐‘น๐’‚๐’”๐’๐’๐’2019 โ€“ 2020

10/2/2019 1Matlab [email protected]

๐‘ผ๐’๐’Š๐’—๐’†๐’“๐’”๐’Š๐’•๐’š ๐’๐’‡๐‘บ๐’‚๐’๐’‚๐’‰๐’‚๐’…๐’…๐’Š๐’๐‘ช๐’๐’๐’๐’†๐’ˆ๐’† ๐’๐’‡ ๐‘ฌ๐’…๐’–๐’„๐’‚๐’•๐’Š๐’๐’/๐‘ซ๐’†๐’‘๐’‚๐’“๐’•๐’Ž๐’†๐’๐’• ๐’๐’‡ ๐’‘๐’‰๐’š๐’”๐’Š๐’„๐’”

04

Page 2: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

1.1 Introduction โ– MATLAB logo, MATLAB Desktop window will launch: title bar, a

menu bar, a tool bar and five embedded windows, one of which is

hidden.

โ– The largest and most important window is the Command Window

on the center.

โ– The Command History Window, the Current Directory Browser and the

Workspace Browser.

โ– Command prompt (>>).

โ– If the Command Window is โ€œActive,โ€ its title bar will be dark, and

the prompt will be followed by a cursor (a blinking vertical line).

10/2/2019 Matlab 2

Page 3: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

The Matlab Environment

10/2/2019 Matlab 3

Figure 1-1 The MATLAB desktop

Page 4: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

1.2 A Matlab Calculatorโ– To begin, you can use Matlab for simple arithmetic problems.

Symbols like + (plus), - (minus), * (multiply), and / (divide) all work

as you would expect.

โ– In addition, ^ is used for exponentiation. For example, if you type:

>> 75 - 32 * 2 + 4 / 2 (Enter)

>> 2+2

>> factor(123456789),

>> sin(pi/3).

>> x + 6 = 90

>> x= 90 โ€“ 6

>> x = x +4

>> x = 34^2

>> x = 2

>> t = x + a

โ€ฆโ€ฆโ€ฆโ€ฆ.?

10/2/2019 Matlab 4

Page 5: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

1.2.1 The order of precedence

10/2/2019 Matlab 5

Operation Algebraic form Matlab Example

Addition ๐‘Ž + ๐‘ ๐‘Ž + ๐‘ 3+4

Subtraction ๐‘Ž โˆ’ ๐‘ ๐‘Ž โˆ’ ๐‘ 14 - 11

Multiplication ๐‘Ž ร— ๐‘ ๐‘Žโˆ—๐‘ 3.14

โˆ—0.85

Right division ๐‘Ž รท ๐‘ ๐‘Ž/๐‘ 48/8

Left division ๐‘ รท ๐‘Ž ๐‘Ž\๐‘ 8\56

Exponentiation ๐‘Ž๐‘

๐‘Ž^๐‘ 5

^2

53

4+9

5๐’‚๐’๐’… 43

3

4+

9

2 3

>> 5*(3/4) + 9/5 โ€ฆโ€ฆโ€ฆ.

ans =

5.5500

Page 6: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

Some Notesโ– Note: that MATLAB prints the answer and assigns the value to a

variable called ans. If you want to perform further calculations with

the answer, you can use the variable ans rather than retype the

answer.

โ– Note: Trigonometric functions in MATLAB use radians, not degrees.

โ– Note: MATLAB displays only 5 digits by default. To display more

digits, type format long (15 digits). Type format short to return to

5-digit display.

โ– Recovering from Problems: If you make an error in an input line,

MATLAB will normally print an error message.

10/2/2019 Matlab 6

Page 7: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

Notesโ– Note that MATLAB places a marker (a vertical line segment) at the

place where it thinks the error might be; however, the actual error

may have occurred earlier or later in the expression.

โ– Note The UP- and DOWN-ARROW keys allow you to scroll back

and forth through all the commands youโ€™ve typed in a MATLAB

session, and are very useful when you want to correct, modify, or

reenter a previous command.

โ– Aborting Calculations: If MATLAB gets hung up in a calculation,

or seems to be taking too long to perform an operation, you can

usually abort it by typing CTRL+C.

10/2/2019 Matlab 7

Page 8: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

Example1. Find the temperature (๐‘‡๐‘“) from the following equation, if

you know ๐‘‡๐‘ = 40: ๐‘ป๐’‡=๐Ÿ—

๐Ÿ“๐‘ป๐’„ + ๐Ÿ‘๐Ÿ

Answer: >> ๐‘‡๐‘= 40;

>> ๐‘‡๐‘“ = 9/5 โˆ— ๐‘‡๐ถ + 32 ๐‘‡๐‘“ = 104

2. Calculate ๐’› = ๐’†๐’™๐’‘ โˆ’๐’‚ ๐’”๐’Š๐’ ๐’™ + ๐Ÿ๐ŸŽ ๐’š for ๐’‚ = ๐Ÿ“, ๐’™= ๐Ÿ, ๐’š = ๐Ÿ–.

Answer:

>> a=5; x=2; y=8;

>> z=exp(-a)*sin(x)+10*sqrt(y)

z=10/2/2019 Matlab 8

Page 9: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

1.3 Helphelp general >>help factor >>more on >>more off

Help Browser: While help in the Command Window is useful for

getting quick information on a particular command, more extensive

documentation is available via the MATLAB Help Browser.

โ– Different way of invoke, one is following:

>>doc sin

โ€ข Lookfor command searches the first line of every MATLAB help file

for a specified string (use lookfor -all to search all lines)

>>lookfor factor

โ€ข You can type demo(or selecting it in help browser) to try some of

MATLABโ€™s online demonstrations.

โ€ข Methods to exit MATLAB: type quit at the prompt, click on (ร—),

close icon, Alt+F4.

10/2/2019 Matlab 9

Page 10: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

1.4 Symbolic Computation โ– 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.

โ– The command expand told MATLAB to multiply out the

expression.

โ– 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. For example,

10/2/2019 Matlab 10

Page 11: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

1.4 Symbolic Computation โ– When you work with symbolic expressions you often need

to substitute (using subs) a numerical value, or even

another symbolic expression, for one (or more) of the

original variables in the expression.

For example:

>> d = 1, syms u v >> w = uห†2 - vห†2 >> subs(w, u, 2)

>> subs(w, v, d) >> subs(w, v, u + v) >> subs(w,[u v],[4 3])

โ–Example: Replace a with 4 from this ๐‘ง = ๐‘Ž + ๐‘,

โ–Ans: >> syms a b; >> subs(a+b,a,4); >> ๐‘Ž๐‘›๐‘ : ๐‘ง = 4 + ๐‘;

โ– Note when you enter multiple commands on a single line separated

by commas, MATLAB evaluates each command and displays the

output on separate lines.

10/2/2019 Matlab 11

Page 12: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

1.4 Symbolic Computation โ–ช Exact Arithmetic: MATLAB uses floating-point

arithmetic for its calculations.

โ–ช You can do exact arithmetic with symbolic expressions.

>> cos(pi/2) % really cos(ฯ€/2)=0 ans = 6.1232e-17

โ–ช The inaccuracy is due to MATLAB gives an

approximation to ฯ€ accurate to about 15 digits, not its exact

value.

โ–ช If you donโ€™t specify the number of digits, the default

setting is 32.

10/2/2019 Matlab 12

Page 13: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

1.4 Symbolic Computation Note One should be wary of using vpa on an expression that

MATLAB must evaluate before applying variable-precision

arithmetic.

3ห†45 gives a floating-point approximation

vpa(3ห†45) gives an answer that is correct only in its first 16 digits

vpa('3ห†45') gives the exact answer.

10/2/2019 Matlab 13

Page 14: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

Matlab Functions

10/2/2019 Matlab 14

Page 15: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

Punctuation Marks

10/2/2019 Matlab 15

Page 16: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

Examples

1. Evaluate the symbolic expression ๐‘“ = 2๐‘ฅ2 โˆ’ 3๐‘ฅ + 1 at

the point x = 1/3.

Ans: >> syms x; >> f = 2*x^2 - 3*x + 1; >>subs(f, 1/3); ๐‘“ = 2/9.

2. substitute the value x = 3 in the symbolic expression of ๐’‡ = ๐’™๐Ÿ๐’š+ ๐Ÿ“๐’™ ๐’š.

Ans: >> syms x y; >> f = x^2*y+5*x*sqrt(y); >> subs(f,x,3);

ans; 9*y+15*y^1/2.

10/2/2019 Matlab 16

Page 17: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

Examples3. Try to write the following in command window:

10/2/2019 Matlab 17

Page 18: Programming (Matlab)ย ยท Programming (Matlab) Chapter One: ... The Matlab Environment 10/2/2019 Matlab 3 Figure 1-1 The MATLAB desktop. 1.2 A Matlab Calculator ... MATLAB has a command

Examples4.

10/2/2019 Matlab 18