You are on page 1of 14

Contents

Lecture 2: Applied Matlab Programming................................................................................................ 2


Creating Matrices ................................................................................................................................ 2
Use colon to refer to an entire column or row ................................................................................... 3
String manipulation............................................................................................................................. 3
Mathematical Operations with Arrays................................................................................................ 3
Presenting Results............................................................................................................................... 3
Plots .................................................................................................................................................... 3
Lines and Symbol Types in Plotting ..................................................................................................... 4
Multiple Plots per Figure Window ...................................................................................................... 4
Other Formats ..................................................................................................................................... 5
Functions ............................................................................................................................................. 5
Text output from Functions ................................................................................................................ 5
The fprintf command .......................................................................................................................... 5
The feval command............................................................................................................................. 6
Relational operators ........................................................................................................................... 6
Logical operators................................................................................................................................. 6
Logical functions ................................................................................................................................. 6
For Loop .............................................................................................................................................. 6
While Loop .......................................................................................................................................... 7
If condition .......................................................................................................................................... 7
Switch and Case .................................................................................................................................. 7
Break command .................................................................................................................................. 7
Lecture 3: Nonlinear equations, Bisection method, Fixed point iteration, Newton- Raphson method,
Secant method ........................................................................................................................................ 8
Lecture 2: Applied Matlab Programming
Creating Matrices
- ones(m,n): matrix with m rows and n columns with all elements 1
- zeros(m,n): matrix with m rows and n columns with all elements 0
- eye(n): square matrix which diagonal elements are equal to 1
Use colon to refer to an entire column or row

String manipulation

Mathematical Operations with Arrays


- A*B only if number of columns in matrix A = number if rows in matrix B
- ^ power of a matrix
- .* element- by- element multiplication
- ./ element- by- element right division
- .\ element- by- element left division
- .^ element- by- element exponentiation

Presenting Results
- All plots displayed though figure window: figure()
- Activate 3rd figure: figure(3)

Plots
- 2D plots: plot()
Lines and Symbol Types in Plotting

Multiple Plots per Figure Window


- subplot(nrows, ncols, thisPlot)
Other Formats
- format long: 14 decimals
- format bank: 2 decimals
- format short (default): 4 decimals
- if integer is entered, there are no trailing zeros

Functions
- function [out1, out2] = FUN (IN1, IN2)
- command function must be at the first line of code
- FUN is the name of the function (should equal to M- file name)
- Local variables: functions use this only while function is executing
- Global variables: share variables between functions
- Functions call other functions: function disp_my_plot

Text output from Functions


- Output to the command window: disp(simple but limited control over appearance of output)
or fprintf

The fprintf command


- fprintf(outFormat, outVariables)
- fprintf(fileHandle, outFormat, outVariables)
The feval command
- feval command evaluates value of function of functions argument

Relational operators

Logical operators

Logical functions

For Loop
for ii = 0:5
disp(ii);
a(ii+1) = ii;
end
While Loop
ii = 0;
while ii <= 5
disp(ii);
ii = ii + 1;
end
- advantage over for loop: loop counter is not going in one direction. We can have statements
within loops such as increase, decrease and reset counter.
If condition
if expression
statement
elseif expression
statement,
else
statement,
end

Switch and Case


- used when a series of paths exists for a given variable

Break command
- used to terminate the execution of for and while loop
Lecture 3: Nonlinear equations, Bisection method, Fixed point
iteration, Newton- Raphson method, Secant method
Solving nonlinear equations
- solutions in the form of roots may be obtained from functions such as quadratic equation.

Iterative Methods
- solved by initial approximation and is improved using iterative methods
- successive use of iterative methods result in approximations which approach the solution,
Iterative method is then considered to be converged
- approximate error estimate is needed to detect is method and converged sufficiently

Useful Theorems

Initial Approximations
Note: initial approximations can be found by neglecting middle term
Graphical Method

- roots occur when function f(x) crosses x- axis. It has 4 roots.

- equate both equations, bring them all to one side to equal 0 and find x (roots)
- used to obtain rough estimate of roots

Internal Halving (Bisection) Method


- requires initial range x1 < x <xr with sign change so that f(x1).f(xr) < 0
- for each iteration, halve the interval, evaluate the function at midpoint and make new
interval the half with a sign change
- ensure function is continuous or else function may converge to a discontinuity
Newton rhapson

You might also like