270 likes | 499 Views
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. Why Matlab?.
E N D
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
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.
Matlab Basics Arithmetic operations: Built-in Functions like sqrt, log, exp, sin etc. Built-in constants like pi
Golden Ratio - Numerical Matlab: (Note the use of parentheses!) phi = (1 + sqrt(5))/2 This produces phi = 1.6180 Let's see more digits. format long phi phi = 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 with ezplot(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 on plot(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] produces v = 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) produces ans = 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.*v produces ans = 0 -2 -8 -18 -32
Vectors and Looping We can generate a column vector of zeroes by: zeros(5,1) produces u = 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. 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 f f(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), toc ans = 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. Friday: In Computer Lab, First Lab Project