0% found this document useful (0 votes)
55 views

Chapter 1 - Matlab

Here are the solutions to the problems: 1) The number of elements in y is 21. The value of the third element is -7. 2) The number of elements is 41. 1) The 10th element is -0.9498. 2) The new array is [-0.9 -0.85 -0.8 -0.75 -0.7]. 3) The sum of the first three elements of the new array is -2.55.

Uploaded by

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

Chapter 1 - Matlab

Here are the solutions to the problems: 1) The number of elements in y is 21. The value of the third element is -7. 2) The number of elements is 41. 1) The 10th element is -0.9498. 2) The new array is [-0.9 -0.85 -0.8 -0.75 -0.7]. 3) The sum of the first three elements of the new array is -2.55.

Uploaded by

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

Introduction to Matlab

Huynh Kha Tu, Ph.D


To start Matlab on a MS Windows system:
 Double-click on the Matlab icon.
Matlab Desktop
In Command window
The Current Directory:

 a file manager window  used to access files.

 open a file by double-clicking on the file name with


the extension .m (Matlab Editor)
Workspace window:

 Displays the variables created in the Command


window.

 Open Array Editor by double-clicking on a variable


name.
Command History window:
 Show all the previous keystrokes entered in the
Command window.
 Used for keeping track what are typed.
 Can click on a keystroke and drag it to the
Command Window or the Editor.
 Double-click on a keystrokeexecute it in
the Command Window.
Enter: Executes the command, retype if there is a
mistake.
Backspace key, Delete key, the left- / right
arrow keys (← →): edit the commands.
up- /down-arrow (↑↓): scroll back/forward
through the commands (once to see the
previous entry, twice to see the entry before
that, and so on)
Session (interactive session)
The interaction between the user and Matlab
Ex:
entry response

By default, four decimal places are used in the result

Can use the command “format” to change the default

In Matlab, e represents exponentiation to a power of 10.


Variable: a symbol used to contain a value and is
assigned the answer by “ans”.
“ans” can be used for further calculations.
Use variables to write mathematical expression
→ assign the result to a variable of your own choosing

Matlab ignores spaces


When we do not specify a variable name for a
result, Matlab uses the symbol “ans” as a
temporary variable containing the most recent
answer.
Argument: is the value operated on by the function
which is enclosed by the parentheses after the
function’s name.
There are hundreds of functions available.

function‟s name

argument
A scalar is a single number.
A scalar variable is a variable that contains a single
number.
Scalar arithmetic operations
Symbol Operation Matlab form
^ Exponentiation: ab a^b
* Multiplication: ab a*b
/ Right division: a/b a/b
\ Left division: a\b a\b
+ Addition: a+b a+b
- Subtraction: a-b a-b
Order of precedence

Precedence Operation
First Parentheses, evaluated starting with the innermost pair.
Second Exponentiation, evaluated from left to right.
Third Multiplication and division with equal precedence,
Evaluated from left to right.
Fourth Addition and subtraction with equal precedence,
evaluated from left to right.

Parentheses can be used to alter this order.


Practice:
Use Matlab to calculate

 
5
a.
3
6 7  3
2 4
4 7  145
48.255  93
b.
53  14 2
27 2 319 4 / 5
 6014 
3
c.  a. 225.6717
b. 7.7189
4 5 c. 202.4120
Example:
The volume of a sphere is given by V=4r3/3,
where r is the radius. Use Matlab to compute
the radius of a sphere having a volume 30
percent greater than that of a sphere of radius
0.3m.
Commands for managing the work session

Command Description
clc Clears the Command Window.
clear Removes all variables from memory.
clear var1, var 2 Removes the variables var1 and var2 from memory
exist („name‟) Determines if a file or variable exists having the name
“name”.
quit Stop Matlab.
who Lists the variables currently in memory.
whos Lists the current variables and sizes, and indicates if
they have imaginary parts.
Commands for managing the work session

Command Description
: Colon; generates an array having regularly spaced
elements.
, Comma; separates elements of an array.
; Semicolon; suppress screen printing; also denotes a
new row in an array.
… Ellipsis, continues a line.
Use the arrow, tab, and control keys to recall, edit,
and reuse functions and variables you typed earlier.
↑: smart recall,
Tab: reduce the amount of typing,
Enter: display the value of the variable, or continue editing
to create a new executable line.
Esc: clear the entire line
Ctrl+k: simultaneously to delete (kill) to the end of
the line
Special variables and constants

Command Description
ans Temporary variable containing the most recent answer.
eps Specifies the accuracy of floating point precision.
i,j The imaginary unit
inf Infinity
NaN Indicates an undefined numerical result
pi The number 
Function: with arguments enclosed in the
parentheses.
Command/statement: argument is not needed/ or not
enclosed in the parentheses.
Ex: plot (x);
clear;
clear x;
addition

multiplication

subtraction

division

complex conjugate
In the Command window, some menus are
often displayed:
 File
 Edit
 Debug
 Parallel (new version)
 Desktop
 Window
 Help
The toolbar is below the Menu bar, provides button
as shortcut to some of the feature on the menus.
 New
 M-file
 Open File
 Cut
 Copy
 Paste
 Undo
 Redo
Introduction to Matlab

Computing with Matlab


Computational capabilities of Matlab
Some features used to solve engineering
problems
Handling collections of numbers (arrays) is one
of computational capabilities of Matlab.
A numerical array is an ordered collection of
number.
The elements of the array must be separated
By commas or spaces.
Use “length” function to determine how many
values are in the array.
Computing with Matlab
Overview on Arrays

Example:
A polynomial in Matlab can be described by an
array whose elements are the polynomial’s
coefficients, starting with the coefficient of
highest power of x.
Ex: 4x3 – 8x2 + 7x -5 can be represented by
Problem 1
Suppose x takes on the values x=1, 1.2, 1.4, ..., 5. Use
MATLAB to compute the array y that results from the
function y=7Sin(4x). Use Matlab to determine how many
elements are in the array y, and the value of the third
element in the array.
Problem 2
Use Matlab to determine how many elements are in the
array [sin(-pi/2):0.05:Cos(0)]. Use Matlab to:
1. Determine the 10th element.
2. Create a new array taking the values from the 4th to 8th
element.
3. Calculate sum of the first three elements of the new
array.
Problem 2
Use Matlab to determine how many elements
are in the array [sin(-pi/2):0.05:Cos(0)]. Use
Matlab to:
1. Determine the 10th element.
2. Create a new array taking the values from the 4th to
8th element.
3. Calculate sum of the first three elements of the new
array.

You might also like