正在加载图片...
Chapter 1. Introduction to MATLAB FIBONACCI Fibonacci sequence f FIBoNACcI (n) generates the first n Fibonacci numbers The name of the function is in uppercase because historically MatlaB was case insensitive and ran on terminals with only a single font. The use of capital letters may be confusing to some first-time MATLAB users, but the convention persists. It is important to repeat the input and output arguments in these comments because he first line is not displayed when you ask for help on the function he next line f zeros(n, 1) by-1 matrix all zeros and assigns it to f. In mAtlAB matrix with only one column is a column vector and a matrix with only one row is t two lir f(2)=2 provide the initial conditions The last three lines are the for statement that does all the work for k= 3: n (k-1)+f(k-2) We like to use three spaces to indent the body of for and if statements, but other people prefer two or four spaces, or a tab. You can also put the entire construction on one line if you provide a comma after the first clause This particular function looks a lot like functions in other programming lan- guages. It produces a vector, but it does not use any of the MATLaB vector or matrix operations. We will see some of these operations soon. Here is another Fibonacci function, fibnum m. Its output is simply the nth Fibonacci number function f fibnum(n) FIBNUM Fibonacci number i FIBNUM(n) generates the nth Fibonacci number if n<=1 else f fibnum(n-1)+fibnum(n-2) end The statement fibnum(12) produces10 Chapter 1. Introduction to MATLAB FIBONACCI Fibonacci sequence f = FIBONACCI(n) generates the first n Fibonacci numbers. The name of the function is in uppercase because historically Matlab was case insensitive and ran on terminals with only a single font. The use of capital letters may be confusing to some first-time Matlab users, but the convention persists. It is important to repeat the input and output arguments in these comments because the first line is not displayed when you ask for help on the function. The next line f = zeros(n,1); creates an n-by-1 matrix containing all zeros and assigns it to f. In Matlab, a matrix with only one column is a column vector and a matrix with only one row is a row vector. The next two lines, f(1) = 1; f(2) = 2; provide the initial conditions. The last three lines are the for statement that does all the work. for k = 3:n f(k) = f(k-1) + f(k-2); end We like to use three spaces to indent the body of for and if statements, but other people prefer two or four spaces, or a tab. You can also put the entire construction on one line if you provide a comma after the first clause. This particular function looks a lot like functions in other programming lan￾guages. It produces a vector, but it does not use any of the Matlab vector or matrix operations. We will see some of these operations soon. Here is another Fibonacci function, fibnum.m. Its output is simply the nth Fibonacci number. function f = fibnum(n) % FIBNUM Fibonacci number. % FIBNUM(n) generates the nth Fibonacci number. if n <= 1 f = 1; else f = fibnum(n-1) + fibnum(n-2); end The statement fibnum(12) produces
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有