numerical computation lecture 2: introduction to matlab programming united international college

28
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College

Upload: pauline-cannon

Post on 01-Jan-2016

252 views

Category:

Documents


3 download

TRANSCRIPT

Numerical Computation

Lecture 2: Introduction to Matlab Programming

United International College

Review

• During our Last Class we covered:– Taylor’s Theorem: Representing a function by a

series. Approximating functions by Taylor polynomial.

– Floating Point Arithmetic: Representing numbers on a finite computer. Single and Double IEEE formats. Round-off and subtractive cancelation. Machine epsilon and discrete vs continuous number line.

Today

• We will introduce the programming environment for our course:

Matlab• We will cover:– Basic Operations, Functions, Plotting– Vectors, Looping

Why Matlab?

• Programming Language is relatively easy.• You have access to it in the CS computer lab

(and in C403).• A free, similar software is available (GNU

Octave).• Most people in scientific and engineering

disciplines use Matlab.

Starting Matlab

• Start from Icon on Desktop

• Start from “Start” menu in lower left of Desktop.

Matlab Basics

• Arithmetic operations:

• Built-in Functions like sqrt, log, exp, sin etc.• Built-in constants like pi

Math Matlab

1 + 2 1 + 2

1 – 2 1 – 2

1 x 2 1 * 2

1 ⌯ 2 1 / 2

22 2^2

Golden Ratio

Golden Ratio - Numerical

• Matlab: (Note the use of parentheses!)phi = (1 + sqrt(5))/2This producesphi = 1.6180Let's see more digits.format longphiphi = 1.61803398874989

Golden Ratio – Solution to Polynomial

• Second calculation: Equating ratios in Golden rectangle gives:

• So, phi is a (positve)solution of

Golden Ratio – Solution to Polynomial

• You can use MATLAB to get the roots of a polynomial.

• MATLAB represents a polynomial by the vector of its coefficients, in descending order.p = [1 -1 -1]r=roots(p)r=

-0.61803398874989 1.61803398874989

Golden Ratio – Zeroes of function

• The number phi is also a zero of the function f(x) = 1/x – (x-1) (Verify)

• The inline function is a quick way to create functions from character strings.f = inline('1/x - (x-1)')

Golden Ratio – Zeroes of function

• A graph of f(x) over the interval 0 ≤ x ≤ 4 is obtained withezplot(f,0,4)

Golden Ratio – Zeroes of function

• To get the root of f(x) around 1 you can use the following command:phi=fzero(f,1)

• You can plot this point on the top of the ezplot graph:ezplot(f,0,4)

hold onplot(phi,0,'o')

Vectors and Looping

• Matlab was designed to be an environment for doing Matrix and Vector calculations.

• Almost all of Matlab's basic commands revolve around the use of vectors.

• A vector is defined by placing a sequence of numbers within square braces: v = [3 1]

produces: v = 3 1

Vectors and Looping

• Note that Matlab printed out a copy of the vector after you hit the enter key. If you do not want to print out the result put a semi-colon at the end of the line:

v = [3 1]; produces no output

Vectors and Looping

• Matlab can define a vector as a set of numbers with a common increment:v = [1:8]

producesv =

1 2 3 4 5 6 7 8

Vectors and Looping

• If you wish to use an increment other than 1 that you define the start number, the value of the increment, and the last number.

• For example, to define a vector that starts with 2 and ends in 4 with steps of .25 :v = [2:.25:4] produces

Vectors and Looping

• You can view individual entries in a vector. For example to view the first entry in the vector from the last slide, type:v(1)

producesans =

2

Vectors and Looping

• We can add or subtract vectors:v = [0:2:8] u = [0:-1:-4]

u+v produces ans =

0 1 2 3 4

Vectors and Looping

• We can multiply or divide vectors term by term:u.*vproducesans =

0 -2 -8 -18 -32

Vectors and Looping

• We can generate a column vector of zeroes by:zeros(5,1)producesu = 0 0

0 0 0

Fibonacci Numbers

• Consider the following program: function f = fibonacci(n)% FIBONACCI Fibonacci sequence1.2. Fibonacci Numbers 9% f = FIBONACCI(n) generates the first n Fibonacci numbers.f = zeros(n,1);f(1) = 1;f(2) = 2;for k = 3:n

f(k) = f(k-1) + f(k-2);end

Matlab Function files

• The first line defines this program as a special Matlab function M-file, not a script. The remainder of the first line says this particular function produces one output result, f, and takes one input argument, n.

function f = fibonacci(n)

Matlab Function files

• These lines are comments: % FIBONACCI Fibonacci sequence1.2. Fibonacci Numbers 9% f = FIBONACCI(n) generates the first n Fibonacci numbers.

• The next two lines initialize the first two values of ff(1) = 1;f(2) = 2;

• The next three define a loop that will iterate through the vector 3:n for k = 3:n

f(k) = f(k-1) + f(k-2);end

Matlab Function files

• To create an M-file, we go to the File menu and select New -> Function M-file. An editor window will pop up. We can type our code into this window and then click the “Run” button in the toolbar.

• We will be asked to save the file. We must save it with the same name as the function, here as “fibonacci.m”

Matlab Function files

• Then, we can use this function in the main Matlab (script) window: fibonacci(5)ans = 1 2 3 5 8

Matlab Function files• We can measure how much time it takes for this function to

complete using the Matlab commands tic and toc: tic, fibonacci(24), tocans = 1 2 3 5 (terms omitted to save space) 10946 46368 75025

Elapsed time is 0.000701 seconds.

Matlab Function files

• Class Exercise: Triangular numbers are numbers of the form n*(n+1)/2, with n a positive integer. – Write a Matlab M-file function to find the

triangular numbers up to n=20. – Try to do this in as few of lines of code as possible.