P1253297484vuAKa

1 / 31
About This Presentation
Title:

P1253297484vuAKa

Description:

Instead of saving a function into a separate ... You've agreed to buy a new car for $18,500. The car dealer is offering two ... A(i, :) returns the ith row ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 32
Provided by: mm152

less

Transcript and Presenter's Notes

Title: P1253297484vuAKa


1
MATLAB
2
Defining a Function in an M-file
Consider the function
fun1.m
function answer fun1(x) answer
xexp(x2)-exp(x2)-sin(x3)
  • Requirements The name of the file containing
    function fun1 must be fun1.m
  • To call function use the syntax
    outVarfun1(inputVar)

3
Using inline()
Instead of saving a function into a separate
M-file, we can use the function inline() to put
definition of the function into the file
containing other statements too
test.m
finline('xexp(x2)-exp(x2)-sin(x3)') f(0.3)
4
M-File for Interest Calculations
Problem Youve agreed to buy a new car for
18,500. The car dealer is offering two financing
options (1) 2.9 interest over 4 years or (2)
8.9 interest over 4 years, with a factory rebate
of 1,500. Which one is the better deal?
Solution Let P be the monthly payment of a loan
of A dollars, having a monthly interest rate of
R, paid off in M months. Then P is given by the
following formula
Giving a total amount paid of
5
interest.m
A18500 amount of loan M124 number
of months FR1500 factory rebate first
financing offer R(2.9/100)/12 monthly
interest rate PA(R(1R)M/((1R)M-1))
payment T1PM total car
cost second financing offer R(8.9/100)/12
monthly interest rate P(A-FR)(R(1R)M/((1
R)M-1)) payment T2PM total car
cost difference DT2-T1
6
Array Operations
gtgt c12345 1 2 3 4 5 gtgt bc 1 2 3 4
5 gtgt g1 2 3 4 5 6 1 2 3 4 5 6 gtgt ones(3) 1
1 1 1 1 1 1 1 1 gtgt zeros(2,3) 0 0 0 0 0 0
Transpose is found using single quote
7
Array Operations
gtgt xzeros(10,1) x 0 0 0
0 0 0 0 0 0 0 gtgt
x(5)80 x 0 0 0 0 80
0 0 0 0 0
gtgt xzeros(10,1) x 0 0 0
0 0 0 0 0 0 0 gtgt
x(4)7 x(9)100 x 0 0 0
7 0 0 0 0 100 0 gtgt
yx(xgt0) y 7 100
8
Reverse Direction
gtgt y8 4 7 1 16 78 y 8 4 7
1 16 78 gtgt y(3-11) ans 7 4
8
These are the third, second, and first elements
of the vector y in reverse order. 3-11 says
start with three, count down by 1, and stop at 1
gtgt y(5-21) ans 16 7 8
5-21 says start with five, count down by 2,
and stop at 1
9
Matrices
Some Functions on Matrices
gtgt A1 2 3 4 5 6 7 8 9 A 1 2
3 4 5 6 7 8 9
det(A) inv(A) rank(A) trace(A)
Sub-matrices of a Matrix
A(i,j) returns the element at entry (i, j) A(,
j) returns the jth column A(i, ) returns
the ith row A(, jk) returns the submatrix of A
consisting of the columns j, j1, , k A(ik,
jh) returns the submatrix of A consisting of the
elements in the rows i to k and in the column j
to h
Transpose of a Matrix
gtgt BA' B 1 4 7 2 5
8 3 6 9
10
Solving Linear Systems and LU Factorization
To solve Axb use xinv(A)b or xA\b
gtgt A2 3 4 1 A 2 3 4
1 gtgt b79 b 7 9 gtgt xA\b x
2 1
11
LU Decomposition
L, U lu(A) where ALU L, U, P
lu(A) where PALU
gtgt A1 2 3 4 5 6 7 8 0 A 1 2
3 4 5 6 7 8 0 gtgt L,
Ulu(A) L 0.1429 1.0000 0
0.5714 0.5000 1.0000 1.0000 0
0 U 7.0000 8.0000 0
0 0.8571 3.0000 0 0
4.5000
12
Cholesky Factorization
chol(A) gives an upper triangular matrix which
is the Cholesky factor of A. An error message
is given if A is not positive definite
13
Generating Equally Spaced Points
linspace(a,b) returns a vector with 100
equally spaced values in interval a, b
linspace(a, b, n) returns a vector with n
elements in the interval a, b
Examples
gtgt xlinspace(0, 2, 5) x 0 0.5000
1.0000 1.5000 2.0000
14
Plotting Functions
Use fplot or plot commands
poly1.m
p'3.x22.x-4' define polynomial fplot(p,
-2, 2) plot p grid draw
grids title('The polynomial p') add title
When the program file poly1.m is executed, the
following figure will be displayed
15
Plotting Functions
gtgt fplot(_at_(x)tan(x),sin(x),cos(x), 2pi-1 1
-1 1)
16
Plotting Functions
gtgt fplot(_at_(x)2x3-2,3x22x-4, -2 2)
17
Finding a Root of a Function Given a Starting
Value
fzero(fcn, x0) returns one of the zeros of the
function defined by the string fcn. The
command requires an initial value x0. The
relative error of the approximation is eps
fzero(fcn, x0 , tolerance)
Examples
gtgt x-100.0110 gtgt f'atan(x)' gtgt
rootfzero(f,1.6) root 1.5040e-020
18
Finding a Root of a Function Given a Starting
Value
Find the zeros of the following function in the
interval 0, 4
Determine the positive root in a given range
gtgt x00 1.3 gtgt xfzero(inline('x10-1'),
x0) x 1
Determine the negative root in a given range
gtgt x0-1.3 0 gtgt xfzero(inline('x10-1'),
x0) x -1
Determine a root with an initial guess
gtgt x00 gtgt xfzero(inline('x10-1'), x0) x
-1
19
Finding all Roots of a Function
Finding the roots of
gtgt p3 2 -4 gtgt theRootsroots(p) theRoots
-1.5352 0.8685
Finding the roots of
gtgt roots(1 0 0 0 0 0 0 0 0 0 0 0 -2) ans
-1.0595 -0.9175 0.5297i -0.9175 -
0.5297i -0.5297 0.9175i -0.5297 - 0.9175i
0.0000 1.0595i 0.0000 - 1.0595i 0.5297
0.9175i 0.5297 - 0.9175i 1.0595
0.9175 0.5297i 0.9175 - 0.5297i
20
Norms and Condition
norm(x) gives norm(x, inf) gives
norm(x, p) gives cond(A) gives the condition
number of A in Euclidean norm
21
Conditional Statements if
if logicalEpression statements end
Example Write an if statement to make a 20
discount if cost is greater than 5
myif.m
x10 cost x25 if xgt5
cost(1-20/100)cost end sprintf('sd', 'The
Cost After Discount ', cost)
gtgt myif ans The Cost After Discount 200
22
The operator Checking for Equality
Example Remove the first column of A if all the
elements of the first column are zero. if A(,
1) 0 A A(1m 2n) end
23
Conditional Statements if-elseif-else
if logicalEpression1 statements1 elseif
logicalEpression2 statements2 elseif
logicalEpressionN statementsN else stateme
nts end
24
for Loop
for variableexpression statements end
note expression can be an array
Example 1 for n110 x(n) sin(npi/10)
end
gtgt data3 9 45 6 7 16 -1 5 for ndata x
n(1)-n(2) end x -4 x -7 x
46 x 1
Example 2 data3 9 45 6 7 16 -1 5 for
ndata x n(1)-n(2) end
25
Nested for Loops
for variable1expression1 statements1 for
variable2expression2 statements2 end end
Example
for n15 for m5-11
A(n,m)n2m2 end end disp(A)
2 5 10 17 26 5 8 13 20
29 10 13 18 25 34 17 20 25
32 41 26 29 34 41 50
26
while Loop
while logicalExpression statements end
Example
number0 epsilon1 while (1epsilon)gt1
epsilonepsilon/2 numbernumber1 end disp(
number) disp(2epsilon)
This example computes the smallest number that
can be added to 1 such that the result is greater
than 1 using finite precision. MATLAB uses 16
digits, so we would expect this number to be near
10-16
53 2.2204e-016
27
The break statement
x1 for i11000 xx/2 if (x1 lt 1)
break end end disp(i) disp(x2)
53 2.2204e-016
28
Example
Let x0 be any integer. Suppose that the following
rule is defined to
if xk is even
if xk is odd
If you stop generating values when xk1, does the
sequence diverge to infinity or converge to 1?
What is needed is a while loop that stops when
xk1, and an if-else-end construction to
implement the foregoing rule.

29
Example(cont.)
xzeros(600,1) x(1)round(abs(input('Enter a
number '))) k1 while(x(k)gt1) (klt600)
if rem(x(k),2)0 x(k) is even
x(k1)x(k)/2 else x(k) is odd
x(k1)3x(k)1 end kk1 increment
sequence counter end xx(xgt0) keep the
generated values and display them
30
Derivatives
diff(fexpr) computes the first derivative
symbolically of fexpr with respect to the
default symbolic variable diff(fexpr, s)
computes the first derivative symbolically of
fexpr with respect to the symbolic variable
s diff(fexpr, n, s) computes the nth
derivative symbolically of fexpr with respect
to the symbolic variable s int(fexpr) computes
the indefinite integral of fexpr w.r.to the
symbolic variable int(fexpr, s)
int(fexpr, a, b, s) compute the definite
integral of fexpr over the interval a, b
with respect to s
31
Find the first and second derivatives of the
following function
gtgt f'(x3x)' f (x3x) gtgt diff(f) ans
3x21
gtgt diff(f,2) ans 6x
Write a Comment
User Comments (0)