scientific computing introduction to matlab programming

26
Scientific Computing Introduction to Matlab Programming

Upload: collin-caldwell

Post on 04-Jan-2016

236 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Scientific Computing Introduction to Matlab Programming

Scientific Computing

Introduction to Matlab Programming

Page 2: Scientific Computing Introduction to Matlab Programming

Today

• We will introduce the programming environment for our course:

Matlab• We will cover:– Basic Operations, Functions, Plotting– Vectors, Looping– Creating a new Function in a M-file

Page 3: Scientific Computing Introduction to Matlab Programming

Why Matlab?

• Programming Language is relatively easy.• You have access to it in the CS computer lab.• Free, similar software is available (GFreeMat,

GNU Octave).• Most people in scientific and engineering

disciplines use Matlab.

Page 4: Scientific Computing Introduction to Matlab Programming

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

Page 5: Scientific Computing Introduction to Matlab Programming

Golden Ratio

Page 6: Scientific Computing Introduction to Matlab Programming

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

Page 7: Scientific Computing Introduction to Matlab Programming

Golden Ratio – Solution to Polynomial

• Second calculation: Equating ratios in Golden rectangle gives:

• So, phi is a (positve)solution of

Page 8: Scientific Computing Introduction to Matlab Programming

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

Page 9: Scientific Computing Introduction to Matlab Programming

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)')

Page 10: Scientific Computing Introduction to Matlab Programming

Golden Ratio – Zeroes of function

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

Page 11: Scientific Computing Introduction to Matlab Programming

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')

Page 12: Scientific Computing Introduction to Matlab Programming

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

Page 13: Scientific Computing Introduction to Matlab Programming

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

Page 14: Scientific Computing Introduction to Matlab Programming

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

Page 15: Scientific Computing Introduction to Matlab Programming

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

Page 16: Scientific Computing Introduction to Matlab Programming

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

Page 17: Scientific Computing Introduction to Matlab Programming

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

Page 18: Scientific Computing Introduction to Matlab Programming

Vectors and Looping

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

0 -2 -8 -18 -32

Page 19: Scientific Computing Introduction to Matlab Programming

Vectors and Looping

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

0 0 0

Page 20: Scientific Computing Introduction to Matlab Programming

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

Page 21: Scientific Computing Introduction to Matlab Programming

Matlab Function files

• The first line defines this program as a special Matlab function M-file. 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)

Page 22: Scientific Computing Introduction to Matlab Programming

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

Page 23: Scientific Computing Introduction to Matlab Programming

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”

Page 24: Scientific Computing Introduction to Matlab Programming

Matlab Function files

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

Page 25: Scientific Computing Introduction to Matlab Programming

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.

Page 26: Scientific Computing Introduction to Matlab Programming

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.

• Friday: In Computer Lab, First Lab Project