Part 1

1. ½Ð¦b MATLAB ª½±µ¿é¤J¤U¦C±¼Æ¡A¬Ý¥¦­Ìªº­È¬O¦h¤Ö¡G
1. i
2. j
3. eps
4. inf
5. nan
6. pi
7. realmax
8. realmin
2. ½Ð¨Ï¥Î lookfor «ü¥O¡A§ä¥X¨ã¦³¤U¦C¥\¯àªº MATLAB «ü¥O¡C¡]¨C¤@¶µ¥u»Ý§ä¥X¤@­Ó ¬ÛÃö«×³Ì°ªªº MATLAB «ü¥O¡C¡^
2. §ïÅÜ¯x°}ªº¤j¤p¡]¨Ò¦p±N 4¡Ñ6 ªº¯x°}§ï¦¨ 12¡Ñ2¡^
3. ±N¯x°}¥ª¥kÂ½Âà¡]Left-right flip¡^
4. ±N¯x°}¤W¤UÂ½Âà¡]Up-down flip¡^
5. §ä¥X¯x°}¨C¤@ª½¦æªº³Ì¤j­È
6. ¹ï¯x°}ªº¨C¤@ª½¦æ¶i¦æ±Æ§Ç
7. ¯x°}ªº±ÛÂà¡]Rotate¡^
8. ¤Ï¯x°}¡]Inverse matrix¡^ªº­pºâ
9. ¨D¯x°}ªº rank
10. ­pºâ¯x°}ªº reduced row echelon form
11. ­pºâ¯x°}ªº null space
13. ­pºâ¯x°}ªº QR ¤À¸Ñ¡]QR Decomposition¡^
14. ­pºâ¯x°}ªº LU ¤À¸Ñ¡]LU Decomposition¡^
16. ¹ï¦V¶q¶i¦æ§Ö³t³Å¥ß¸­Âà´«¡]Fast Fourier Transform¡^
17. ª½¨¤®y¼ÐÂà¦¨·¥®y¼Ð
18. ·¥®y¼ÐÂà¦¨ª½¨¤®y¼Ð
3. (*)When does n! explodes: Write a MATLAB script to find the minimal value of n such that n!>realmax. What is the value of n? What is the value of (n-1)! ?
4. ¼g¤@­Ó MATLAB ¤pµ{¦¡ findN01.m¡A¨D¥X³Ì¤pªº n ­È¡A¨Ï±o n! > realmax¡C½Ð°Ý n ªº­È¬O¦h¤Ö¡H¦¹®É (n-1)! ªº­È¤S¬O¦h¤Ö¡H

02-ªì±´MATLAB/findN01.mmaxN = 1000; for n=1:maxN value = prod(1:n); if value&gt;realmax break; end end fprintf('n = %d\n', n); fprintf('(n-1)! = %d\n', prod(1:n-1));

5. MATLAB ªº sqrt «ü¥O¥i¹ï¥ô¤@¼Æ­È¶i¦æ¶}¥­¤èªº¹Bºâ¡C¥Î¦¹«ü¥O¨D¥X¤U¦C¦U¼Æªº¥­¤è®Ú¡A¨ÃÅçºâ¤§¡G
1. p
2. 2*i
3. -5+12*i
¨ä¤¤ i ¬O³æ¦ìµê¼Æ¡C
6. (*)Basic function for both scalar and vector inputs: Write a MATLAB function to evaluate the following expression:
0.5*exp(x/3)-x*x*sin(x)
where x is the input and y is the output of the function. You function should be able to handle both the situations when x is a scalar or a vector. Please use the following code segment to plot the expression: x=0:0.1:10; plot(x, myFun01(x));
7. ¼g¤@­Ó MATLAB ¨ç¼Æ myFun01.m ¨Ó­pºâ¤U¦C¤èµ{¦¡¡G
y = 0.5*exp(x/3)-x*x*sin(x)
¨ä¤¤ x ¬O¨ç¼Æªº¿é¤J¡Ay ¬O¨ç¼Æªº¿é¥X¡C§Aªº¨ç¼Æ¥²¶·¯à°÷³B²z·í x ¬O¯Â¶q©Î¬O¦V¶qªº¨âºØ±¡ªp¡C¦¹¥~¡A½Ð§Q¥Î¤U­z¨â¦Cµ{¦¡½X¨Óµe¥X¦¹¨ç¼Æªº¹Ï§Î¡G x=0:0.1:10; plot(x, myFun01(x));

02-ªì±´MATLAB/myFun01.mfunction y = myFun(x) y = 0.5*exp(x/3)-x.*x.*sin(x);

1. ¼g¤@­Ó MATLAB ¨ç¼Æ piFun01.m ¨Ó­pºâ¤U¦C¯Å¼Æ¡G
f(n) = 4*(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ...)
2. ¨Ï¥Î tic ©M toc «ü¥O¨Ó´ú¶q piFun01(100000) ªº­pºâ®É¶¡¡C¦pªG§A¤£ª¾¹D¦p¦ó¨Ï¥Î³o¨â­Ó«ü¥O¡A½Ð¨Ï¥Î help tic ¤Î help toc ¨Ó¬d¥X¥¦­Ìªº¥Îªk¡C§ÚªºÂÂ¹q¸£¬O Pentium 450MHz¡A©Ò±oªº­pºâ®É¶¡¬ù¬° 2 ¬í¡C½Ð»¡©ú§Aªº¹q¸£³W®æ¥H¤Î¨ä­pºâ®É¶¡¡C

02-ªì±´MATLAB/piFun01.mfunction out = piFun(n) % piFun: Use sum of sequence to approximate pi % Roger Jang, 20030726 total=0; for i=1:n item = (-1)^(i+1)/(2*i-1); total = total+item; end out = 4*total;

8. (**) Function for computing the Fibonacci number:
1. Write a recursive function fibo.m to compute the Fibonacci sequence, which is defined as follows:
fibo(n+2)=fibo(n+1)+fibo(n)
s The initial conditions are fibo(1)=0, fibo(2)=1.
2. What is the value returned by fibo(25)?
3. Please use the commands tic and toc to measure the computation time of fibo(25). If you don't know how to use tic and toc, please try "help tic" and "help toc" in MATLAB command window. Please state the specs of your computer and the computation time of fibo(25).
4. If you have taken Discrete Mathematics, you should already known that the n-th term of a Fibonacci sequence can be expressed as
fibo(n)={[(1+a)/2]^(n-1)-[(1-a)/2]^(n-1)}/a
where a is the square root of 5. Write a non-recursive function fibo2.m to compute Fibonacci sequence according to the above expression.
5. What is the value of fibo2(25)? Is it the same as fibo(25)?
6. Compare the computation time of fibo2(25) and fibo(25).
1. ¼g¤@­Ó MATLAB ªº»¼°j¨ç¼Æ fibo.m ¨Ó­pºâ Fibonacci ¼Æ¦C¡A¨ä©w¸q¦p¤U¡G
fibo(n+2) = fibo(n+1)+fibo(n)
fibo(1) = 0, fibo(2) = 1.
2. fibo(25) ªº¦^¶Ç­È¬O¦h¤Ö¡H
3. ¨Ï¥Î tic ©M toc «ü¥O¨Ó´ú¶q fibo(25) ªº­pºâ®É¶¡¡C¦pªG§A¤£ª¾¹D¦p¦ó¨Ï¥Î³o¨â­Ó«ü¥O¡A½Ð¨Ï¥Î help tic ¤Î help toc ¨Ó¬d¥X¥¦­Ìªº¥Îªk¡C§Úªº¹q¸£¬O Pentium 2GHz¡A©Ò±oªº­pºâ®É¶¡¬ù¬° 3.35 ¬í¡C½Ð»¡©ú§Aªº¹q¸£³W®æ¥H¤Î¨ä­pºâ®É¶¡¡C
4. ¦pªG§A­×¹LÂ÷´²¼Æ¾Ç¡A´N·|ª¾¹D Fibonacci ¼Æ¦Cªº²Ä n ¶µ¥i¥Hª½±µªí¥Ü¦¨
fibo(n)={[(1+a)/2]^(n-1)-[(1-a)/2]^(n-1)}/a
¨ä¤¤ a ¬O 5 ªº¥­¤è®Ú¡C¼g§Q¥Î¤W¦¡¤@­Ó MATLAB ªº«D»¼°j¨ç¼Æ fibo2.m ¨Ó­pºâ Fibonacci ¼Æ¦C¡C
6. ½Ð­pºâ fibo2(25) ªº­pºâ®É¶¡¡A¨Ã©M fibo(25) ¤ñ¸û¡C

02-ªì±´MATLAB/fibo.mfunction out = fibo(n) % fibo: Fibonacci number % Roger Jang, 20030726 if n==1 out=0; return; elseif n==2 out=1; return; else out=fibo(n-1)+fibo(n-2); end

02-ªì±´MATLAB/fibo2.mfunction out = fibo(n) % fibo: Fibonacci number using an analytic expression % Roger Jang, 20030726 r1=(1+sqrt(5))/2; r2=(1-sqrt(5))/2; out=(r1^(n-1)-r2^(n-1))/sqrt(5);

9. (**) Find the minimum of a matrix:
1. Write a MATLAB function minxy.m which can find the minimal element in a 2-dimensional matrix:
[minValue, minIndex] = minxy(matrix)
where matrix is a 2-d matrix, minValue is the minimal element of the input matrix, and minIndex is an integer vector of length 2 indicating the indices of the minimal element. (In other words, matrix(minIndex(1), minIndex(2)) is equal to minValue.)
2. Test you program by typing the following statement in MATLAB command window:
[minValue, minIndex]=minxy(magic(20))
What are the returned values of minValue and minIndex?
1. ½Ð¼g¤@­Ó¨ç¼Æ minxy.m¡A¨ä¥\¯à¬O¥Ñ¤@­Ó¤Gºû¯x°}¤¤§ä¥X¤p¤¸¯À¡A¥Îªk¦p¤U¡G
[minValue, minIndex] = minxy(matrix)
¨ä¤¤ matrix ¬O¤@­Ó¤Gºû¯x°}¡AminValue «h¬O¨ä¤¸¯Àªº³Ì¤p­È¡A¦Ó minIndex ¬O¤@­Óªø«×¬° 2 ªº¥¿¾ã¼Æ¦V¶q¡A¥Nªí³Ì¤p­Èªº¯Á¤Þ¡C¡]´«¥y¸Ü»¡¡Amatrix(minIndex(1), minIndex(2)) ªº­È§Y¬O minValue¡C¡^

Hint
½ÐºÉ¶q¨Ï¥Î min «ü¥O¡C

02-ªì±´MATLAB/minxy.mfunction [minValue, minIndex] = minxy(matrix) %MINXY Minimum of a 2D matrix % Usage: [minValue, minIndex] = minxy(A) % minValue: the minimum of the matrix A % minIndex: the 2D index of minValue in A % Roger Jang, 20010219 [columnMin, columnMinIndex] = min(matrix); [minValue, tmp] = min(columnMin); minIndex = [columnMinIndex(tmp) tmp];

Part 2

1. What is the MATLAB command to display the version of the MATLAB you are using?
2. What is the MATLAB command to test the speed of your computer when executing MATLAB?
4. What is the MATLAB command to display the search path?
5. What is the MATLAB command to find the path of a command?
6. How do you add a folder to the search path of MATLAB?
7. How do you delete a folder from the search path of MATLAB?
8. What is the default data type of variables in MATLAB?
9. How do you create comments in a MATLAB program?
2. Create a magic matrix: Create a 5-by-5 magic matrix by hand. (You don't need to explain how you derive it.)
3. Vector indexing: Given two row vectors x and y of the same length, write a one-line MATLAB statement to achieve the following goals:
1. Change element 3 by multiplying it by 5.
2. Append an element of value 5.
3. Delete element 2.
4. Concatenate x and y.
5. Stack x and y.
4. Matrix indexing: Given a 5-by-5 matrix matrix A, write a one-line MATLAB statement to achieve the following goals:
1. Delete row 2 of A.
2. Delete rows 2 and 4 of A.
3. Delete column 3 of A.
4. Delete columns 3 and 5 of A.
5. Change row 4 of A by multiplying it by 5.
6. Change columns 1 and 4 of A by multiplying it by 3.
7. Switch columns 1 and 3 of A.
8. Switch columns 1 and 3, and rows 3 and 5, of A.
9. Replace column 3 by row 4.
10. Replace rows 2 and 4 by columns 1 and 5.
11. Assign a logical variable Y to verify sum of each column of A is equal to 65.
12. Assign a logical variable Y to verify sum of each row of A is equal to 65.
13. Assign a logical variable Y to verify the diagonal sum of A is equal to 65.
14. Assign a logical variable Y to verify the anti-diagonal sum of A is equal to 65.
15. Assign B to be columns 4, 2, and 5 of A.
16. Assign B to be the second to fourth elements of column 4 of A.
17. Assign B to be the concatenation of the third to fifth elements of column 4, and the first to fourth elements of column 5, of A.
18. Assign B to be the diagonal vector of A, without using "diag" command.
19. Assign B to be diag(A, -1), without using "diag" command. (This is, [23 6 19 2] for A=magic(5).)
20. Assign B to be diag(A, 1), without using "diag" command. (This is, [24 7 20 3] for A=magic(5).)
21. Put A at the upper left corner of a zero matrix of 7 by 7.
22. Put 5:14 into the first two columns of A.
23. Put 5:14 into the last two columns of A.
5. Simple indexing: What is the value of the variable d after executing the following script: a = [10 20;30 40]; b = [10 30;30 30]; c = a>b; d = a(c);
6. Simple math on matrices: Given a matrix A, write a one-line MATLAB statement to achieve the following goals:
1. Find the max. of each row.
2. Find the min. of each column.
3. Sort each row.
4. Sort each column in descending order.
5. Find the max. of min. of each column.
6. Find the max. of min. of each row.
7. Rotation and reflection: Given a n-vertex polygon denoted as a complex vector of n elements z=[z1, z2, ..., zn]. Use a few MATLAB statements to compute the coordinates of the polygon after the following operations.
1. Rotate p/6 with respect to the origin.
2. Rotate p/6 with respect to its center (which is the mean of all the vertices).
3. Reflection along a line q=p/3.
4. Flip the polygon along a straight of slope 1/2 which passes the polygon's center.
5. What is the rotation matrix? What is the reflection matrix?
8. Ackermann's function: ½Ð¨Ï¥Î MATLAB ¨Ó¼g¤@­Ó»¼°j¨ç¼Æ¥H­pºâ¤U¦C Ackerman's function¡G
A(m, n) =
 n+1, if m=0 A(m-1,1), if n= A(m-1, A(m,n-1)), otherwise

(English version) Write a function myAdd.m which can add two matrices with different dimensions, with the following usage:

where a and b are given 2D matrices, with perhaps different dimensions. Hence, it is not always possible to perform the common matrix addition. As an extension, myAdd can extend the given matrices to have the same dimensions (with minimum padded zeros), then add them to create the final output. For instance, when a = [1 2 3; 4 5 6] and b = [5; 2], myAdd(a, b) should return [6 2 3; 6 5 6]. Moreover, the function do not support scalar expansion, so myAdd([1, 1], 2¡^returns [3, 1] instead of [3, 3].
output = myMultiply(a, b)
11. Derivation of the Euler's number: This exercise is about the derivation of the Euler's number which is the base of the natural logarithm. The Euler's number is defined as follows: $$e = \lim_{n\rightarrow \infty} \left(1+\frac{1}{n}\right)^n$$
1. Give the formula of $(x+y)^n$ based on the binomial theorem.
2. Prove that e can be reformulated as follows: $$e = \frac{1}{0!} + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + \cdots$$
12. Approximation of ln(1+r): This exercise is about the approximation of $ln(1+r)$.
1. Give the formula of the Taylor series expansion of a general well-behaved function $f(x)$ at $x=a$.
2. Use the expansion to explain why $ln(1+r)$ can be approximated by $r$ as $r$ is close to zero.
13. Computing the Euler's number (part 1): Let's assume the Euler's number e = 2.71828182845904523536.
1. Write a function to approximate the Euler's number by the following formula:
fun1(n) = (1+1/n)n
2. Plot the function as n goes from 1 to 1000.
3. Write a script to determine the smallest value of n such that |fun1(n) - e| < 10-5 ?
14. Computing the Euler's number (part 2): Let's assume the Euler's number e = 2.71828182845904523536.
1. Write a function to approximate the Euler's number by the following formula:
fun2(n) = 1/0! + 1/1! + 1/2! + 1/3! + ... + 1/n!
2. Plot the function as n goes from 1 to 100.
3. Write a script to determine the smallest value of n such that |fun2(n) - e| < 10-5 ?
15. Rule of 70:
1. Please explain what "Rule of 70" (also known as "Rule of 72" or "Rule of 69") is.
2. Please derive the exact formula for the above rule, assuming $ln(1+r) = r$ when $r$ is close to zero.
16. Computing for rule of 70: Please refer to the wiki page of Rule of 72 for this and all the following exercises.
1. Write a MATLAB program to plot the first table in the above wiki page, where x-axis is the rate from 0% to 70%, and y-axis is the no. of years for doubling the investment. The plot should have 6 curves, corresponding to columns 2 to 7 of the table.
2. Write a MATLAB program to plot the deviation from the actual years of doubling the investment.
17. Comparison of compounding at various frequencies: Please write a MATLAB script to generate the comparison plot shown here. (Hint: use "stairs" to plot a function in a stairwise manner.)
21. ¶R©Ð§ë¸êºëºâ: ½Ð¼g¤@­ÓMATLAB¸}¥»¡A¶i¦æ¤U¦C¹Bºâ¡C¡]Hint: You can use the functions "loan" and "saving" in the Utility Toolbox.¡^

¡]¦b¸Ñ¤W­z°ÝÃD®É¡A§Ú­Ì¤£¦Ò¼{³q³f¿±µÈµ¥¨ä¥¦¦]¯À¡A¨Ã°²³]»È¦æªº¦s´Ú§Q²v¥u¦³1.38%¡C¡^

Hint: My result for your reference...

¥H¤U­pºâ¥H¸U¤¸¬°³æ¦ì Monthly payment = 2.21839 Without rent income: Final value of the house = 2019.37 Final value of the monthly payments = 612.737 Final value of the down payment = 131.764 Final earning = 2019.37-612.737-131.764 = 1274.87 Initial earning = 967.54 With rent income: Final value of the monthly payments = 336.529 Final earning = 2019.37-336.529-131.764 = 1551.08 Initial earning = 1177.16
22. Function for computing IRR: Write a function to compute the IRR (Internal Rate of Return) of a given investment plan. A basic example for yearly compounding and yearly cash flow is shown next:

02-ªì±´MATLAB/irrFind.mfunction r=irrFind(cashFlowVec, x0) r=fzero(@npvCompute, x0); function npv=npvCompute(x) n=length(cashFlowVec); npv=sum(cashFlowVec./((1+x).^(0:n-1))); % Yearly compounding end end

The new function has the following usage:
irr=myIrrFind(cashFlowVec, x0, 'timeUnit4cashFlow', 'timeUnit4compounding');
where
• irr: Internal rate of return
• cashFlowVec: Cash flow vector
• x0: Initial guess of IRR, default to 0
• timeUnit4cashFlow: Unit in month for cash flow, with the following possible values:
• 1: month
• 3: quarter
• 6: half year
• 12: year (default)
• timeUnit4compounding: Time unit compounding, with the following possible values:
• 'month' (default) for monthly compounding
• 'year' for yearly compounding (which is a valid option only when timeUnit4cashFlow is 12)
Note that if timeUnit4cashFlow is not 12 and timeUnit4compounding is 'year', the function should return nan.
MATLABµ{¦¡³]­p¡G¤Jªù½g