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

Module 01

Matlab is a software for numerical computation and graphics. It is designed for matrix operations like solving systems of linear equations and computing eigenvalues. It can also perform graphical functions and be extended through user-written programs. While it can solve nonlinear problems through additional toolboxes, Matlab produces approximate solutions rather than exact symbolic computations like Maple or Mathematica.

Uploaded by

Dinesh Pant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Module 01

Matlab is a software for numerical computation and graphics. It is designed for matrix operations like solving systems of linear equations and computing eigenvalues. It can also perform graphical functions and be extended through user-written programs. While it can solve nonlinear problems through additional toolboxes, Matlab produces approximate solutions rather than exact symbolic computations like Maple or Mathematica.

Uploaded by

Dinesh Pant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

ENGINEERING PROBLEM

SO
SOLVING
G WITH MATLAB
A A
AND MAPLE
MODULE 1
STARTING MATLAB
2017
2. Introduction
Matlab (Matrix laboratory) is an interactive software system for
numerical computations and graphics.

As the name suggests, Matlab is especially designed for matrix


p
computations: solvingg systems
y of linear equations,
q , computing
p g
eigenvalues and eigenvectors, factoring matrices, and so forth.

IIn addition,
ddi i i has
it h a variety
i off graphical
hi l capabilities,
bili i and
d can be
b
extended through programs written in its own programming
language.
language

Many such programs come with the system; a number of these


extend Matlab's capabilities to nonlinear problems, such as the
solution of initial value problems for ordinary differential equations.

2
MATLAB
Matlab is designed to solve problems numerically, that is, in finite-
precision arithmetic.
arithmetic Therefore it produces approximate rather than
exact solutions, and should not be confused with a symbolic
computation
p system
y ((SCS)) such as Mathematica or Maple.
p

It should be understood that this does not make Matlab better or


worse than an SCS; it is a tool designed for different tasks and is
therefore not directly comparable.

3. MATLAB Windows
Default Windows Other Windows
Command Window Help Window
C
Current Di
Directory Window
Wi d Edi Window
Editor Wi d
Workspace Window Figure Window
Command History Window
3
MATLAB
4
MATLAB
4. Working in the Command Window
Clickk in
Cli i the
th Command
C d Window
Wi d t make
to k it active.
ti Wh a window
When i d
becomes active, its title bar darkens. It is also likely that your cursor
will change from outline form to solid or from light to dark,
dark or it may
simply appear. Now you can begin entering commands.
• Several Commands can be types in the same line.
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.
• It is not possible to go back to a previous line in the command
window, make a correction and re
re-execute
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 executed. The down-arrow keyy (()) can be used to move down
the previously typed commands.
5
MATLAB
• To enter a statement that is too long to be typed in one line, use
three periods, … , followed by Enter.
• You can suppress output to the screen by adding a semicolon (;)
after the statement.
statement
• When the symbol % is typed in the beginning of a line, the line is
designated as a comment.
comment

clc Command
The clc command clears the Command Window. You can also place
cursor on the Command window and press the right button of the
mouse.
Aborting
To interrupt a running program press simultaneously the Ctrl-c keys.

6
MATLAB
5. Saving and Reloading Work
To save yyour current workspace
p select Save Workspace
p as… from
the File menu. Chose a name for your file, e.g. filename.mat and
next click on Save. Remember that the file you just created must be
l
located
d in
i MATLAB's
MATLAB' searchh path.h
To load contents of the file named filename into MATLAB's
workspace
k type load
l d filename
fil i the
in h Command
C d Window.
Wi d Y can
You
also select open from the File menu.
To save the
h workk performed
f d during
d i session,
i you can use command
d
diary ‘filename.txt’.

6. Arithmetic Operations of Scalars


Y can use MATLAB to
You t do
d arithmetic
ith ti as you would
ld do
d with
ith a
calculator. You can use + to add, - to subtract, * to multiply, / or \ to
divide, and ^ to exponentiation. 7
MATLAB
Order of Precedence

First Parentheses. For nested parentheses, inner most ate


executed first.
Second Exponentiation
Third Multiplication, Division (equal precedence)
Fourth Addition and Subtraction

Using MATLAB as a Calculator


This can be done by typing a mathematical expression in the
Command Window and pressing Enter.

8
MATLAB
7. Display Formats
FORMAT Set S t output
t t format.
f t FORMAT with ith no inputs
i t sets t the
th
output format to the default appropriate for the class of the variable.
For float variables,
variables the default is FORMAT SHORT.
SHORT

FORMAT SHORT Scaled fixed point format with 5 digits.


digits
FORMAT LONG Scaled fixed point format with 15 digits
for double and 7 digits for single.
single
FORMAT SHORT E Floating point format with 5 digits.
FORMAT LONG E Floating point format with 15 digits for
double and 7 digits for single.
FORMAT SHORT G Best of fixed or floating point format
with 5 digits.

9
MATLAB
FORMAT LONG G Best of fixed or floating point format
with
ith 15 digits
di it for
f double
d bl and
d 7 digits
di it for
f
single.
FORMAT SHORT E
Engineering
i i fformatt that
th t has
h att least
l t 5
ENG digits and a power that is a multiple of
three
FORMAT LONG Engineering format that has exactly 16
ENG significant digits and a power that is a
multiple of three.
FORMAT HEX Hexadecimal format.
format
FORMAT + The symbols +, - and blank are printed
for positive,
positive negative and zero elements.
elements
Imaginary parts are ignored.
FORMAT BANK Fixed format for dollars and cents.
cents
10
MATLAB
FORMAT RAT Approximation by ratio of small integers.
N b with
Numbers ith a large
l numerator
t or large
l
denominator are replaced by *.
FORMAT S
Suppresses extra
t line-feeds.
li f d
COMPACT
FORMAT LOOSE P t the
Puts th extra
t line-feeds
li f d back
b k in.
i

8 Elementary Mathematical Functions


8.
sqrt: square root function
sin, cos, tan: trigonometric functions
sind,, cosd,, tand: trigonometric
g functions when angle
g is in degree
g

sinh, cosh, tanh: hyperbolic functions

11
MATLAB
asin, acos, atan: inverse trigonometric functions
asind acosd,
asind, acosd atand: inverse trigonometric functions for degree
asinh, acosh, atanh: inverse hyperbolic functions
exp, log, log10: exponential functions
real,, imag,
g, conj:
j real,, imaginary
g y pparts of a complex
p number,, and the
complex conjugate number
abs, angle: absolute value of a complex number,
number and the phase angle
of a complex number
complex(x,y):
complex(x y): Generates a complex number with a real component
x and an imaginary component y.
round: rounding to the nearest integer
floor: rounding to the smallest integer
ceil: rounding to the largest integer
12
MATLAB
fix: rounding to the smallest integer if positive and to the largest
integer if negative
sign: 1 if positive, 0 if zero, and –1, if negative
mod rem: remainders upon division
mod,
factorial: factorial of an integer number
factor: factorize an integer number into prime numbers
gcd(x y) : Finds the greatest common denominator of x and y.
gcd(x,y) y
lcm(x,y) : Finds the least common multiple of x and y.

primes(x) : Finds all the prime numbers less than x.


isprime(x)) : Checks to see if x is a prime number.
isprime( n mber If it is,
is the
function returns 1; if not, it returns 0.

13
MATLAB
9. Variables and Assignments
A variable is a name made of a letter or a combination of several
letters (and digits) that is assigned a numerical value. Once a
variable is assigned
g a numerical value,, it an be used in mathematical
expressions, in functions, and in any MATLAB statements and
commands.
Variable names can contain letters, digits and the underscore
character.
Variable names must begin with a letter.
MATLAB is case sensitive; it distinguishes between uppercase and
lowercase letters. For example AA, Aa, aA and aa are four different
variables.
Avoid using the names of a built-in-function for a variable.
You can check to see if a variable name is allowed by using the
isvarname command. 14
MATLAB
MATLAB reserves a list of keywords for use by the program, which
you cannot assign as variable names.
names The iskeyword command
causes MATLAB to list these reserved names.

>> iskeyword

ans =

'break'
'case'
case
'catch'
'classdef'
' ti '
'continue'
'else'
'elseif'
'end'
'for'
'function‘
……..
……….. 15
MATLAB
10. Predefined Variables
A number of frequently used variables are already defined when
MATLAB is started.
ans A variable that has the value of the last expression that
was not assigned to a specific variables.
pi
p The number .
eps The smallest difference between two numbers (2-52).
inf Used for infinity.
infinity
i, j Defined as (-1).
NaN Not a number
date returns the current date in a “string format.”
clock Returns a six-member array [year month day hour
minute second].
Fix(clock) result in a format that is easier to read 16
MATLAB
11. Managing Variables
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 size together with information
about their bytes and class.

12. Errors in Input


If you make an error in an input line,line MATLAB will beep and print
an error message. Note that MATLAB places a marker (a vertical line
segment)
g ) at the pplace where it thinks the error might
g be;; however,, the
actual error may have occurred earlier or later in the expression. 17
MATLAB
13. Computational Limitations
The variables stored in a computer can assume a wide range of
values. On the majority of computers, the range extends from about
10-308 to 10308, which should be enough g to accommodate most
computations. MATLAB includes functions to identify the largest
real numbers and the largest integers the program can process.

realmax Returns the largest possible floating-point number


used in MATLAB
realmin Returns the smallest possible floating-point number
used in MATLAB
intmax Returns the largest possible integer used in
MATLAB
intmin Returns the smallest possible integer used in
MATLAB
18
MATLAB
14. Algebra and Trigonometry
Using
U i MATLAB’ Symbolic
MATLAB’s S b li Math
M th Toolbox,
T lb you can carry outt
algebraic and trigonometric or symbolic calculations such as
factoring polynomials , simplification or solving algebraic
equations.
To perform symbolic computations,
computations you must use syms to declare
the variables you plan to use to be symbolic variables.

E
Example
l
>> syms x y >> sin(pi)
>> (x+y)*(x-2*y) >> sin(sym(pi))
>> expand(ans) >> sym('1/3')+sym('5/8')
>> factor(ans) >> factor(ans)
p y ((
>> simplify ((x^3-y^3)/(x-y))
y ) ( y)) >> simplify(((sin(x))^2+(cos(x))^2))
p y((( ( )) ( ( )) ))
19
MATLAB
MATLAB has a more robust command, called simple, that
sometimes does a better job than simplify.
simplify

15. Solving Equations


You can solve equations involving variables with solve .
>> solve('x^2-3*x+2')
solve('x^2 3*x+2')
>> solve('x^3-3*x^2*y+2*y','y')

>> solve('x^2-3*x-5')

To get numerical solutions, type double(ans), or vpa(ans) to display


more digits.
>> solve('x*cos(y)-2*sin(y)','x')
>> solve('exp(x)*y-x','y')
( p( ) y , y )
20
MATLAB
Simultaneous Equations
>> sol=solve('x+2*y-3','3*x-5*y+6')
l l (' +2* 3' '3* 5* +6')
>> sol.x
>> sol.y
>> [[x,y]=solve('x+2*y-3','3*x-5*y+6')
,y] ( y , y )

fzero
You can numerically find the solutions shown on the graph with
fzero, which looks for a zero of a given function near a specified
value of x.
>> fzero('exp(-2*x)-sin(x)',0.5)
e o( e p( ) s ( ) ,0.5)

21
MATLAB
16. User Defined Function
Y can use inline
You i li tot define
d fi your own functions.
f ti
>> f=inline('x^3+2*x^2-5*x+6','x')
>> f(2)

22
MATLAB

You might also like