of 29

• date post

31-Dec-2015
• Category

## Documents

• view

218

2

Embed Size (px)

### Transcript of •¸€¼–¹³• 2008, Applied Mathematics NDHU1 ïƒ...

2008, Applied Mathematics NDHU1Looping for loop while loopBreak and returnRecursion

Lecture 2 Iteration12008, Applied Mathematics NDHU2for-loop

Source codes

Evaluaten=input('input an integer:');v=0;for i=1:n v=v+4*i^3+3*i^2+2*i+1;endfprintf('The answer is %d\n',v); 2008, Applied Mathematics NDHU3For-Looping

for i=1:n v=v+4*i^3+3*i^2+2*i+1;end Iteratively executing same body instructions Increasing or decreasing control index2008, Applied Mathematics NDHU4Increasing indexv=v+4*i^3+3*i^2+2*i+1FT i =1

i=i+1for i=1:n v=v+4*i^3+3*i^2+2*i+1;end2008, Applied Mathematics NDHU5Decreasing indexv=v+4*i^3+3*i^2+2*i+1FT i =n

i=i-1for i=n:-1:1 v=v+4*i^3+3*i^2+2*i+1;end2008, Applied Mathematics NDHU6Representation of polynomials

representation

2008, Applied Mathematics NDHU7roots

roots([1 -2])ans =

22008, Applied Mathematics NDHU8Addition of two polynomials

2008, Applied Mathematics NDHU9

2008, Applied Mathematics NDHU10add_polySource codes m=length(a);n=length(b); c=zeros(1,max(m,n)); size_c=length(c); for i=1:m c(size_c-i+1)=c(size_c-i+1)+a(m-i+1); end for i=1:n c(size_c-i+1)=c(size_c-i+1)+b(n-i+1); end ii=min(find(c~=0)); c=c(ii:size_c);

2008, Applied Mathematics NDHU11add_poly>> a=[-1 2 3]; b=[1 2];add_poly(a,b)ans = -1 3 5

>> a=[-1 2 3]; b=[1 -2 3];>> add_poly(a,b)ans = 6

2008, Applied Mathematics NDHU12Nested loopLoops within loopsEx. source codes : multiplication of two matrices2008, Applied Mathematics NDHU13Nested loop [na ma]=size(A); [nb mb]=size(B); for i=1:na for j=1:mb C(i,j)=A(i,:)*B(:,j); endendouterinner The inner loop body is executed na x mb times Nested loops are usually time consuming

2008, Applied Mathematics NDHU14for i=1:nafor j=1:nbC(i,j)=A(i,:)*B(:,j);

endNested loop2008, Applied Mathematics NDHU15Speeding-upEliminate nested loopsRewrite loops or nested loops to equivalent loop-free codesEx. C=A*B2008, Applied Mathematics NDHU16while-loopstatementsfalsetruen >1while n>1 s=[mod(n,2) s]; n=(n-mod(n,2))/2;endBody statements are executed repeatedly until the halting conditionholds 2008, Applied Mathematics NDHU17while-loopHalting condition: a logical expressionLoop bodywhile n>1 s=[mod(n,2) s]; n=(n-mod(n,2))/2;end2008, Applied Mathematics NDHU18dec2binfunction s=dec2bin(n)% n: a positive integer% s: a vector for binary representation of n s=[]; while n>1 s=[mod(n,2) s]; n=(n-mod(n,2))/2; end if n==1 s=[n s]; endreturnSource codes2008, Applied Mathematics NDHU19Decimal to binaryFind binary representation of a decimal numberInput: a decimal numberOutput: binary representation2008, Applied Mathematics NDHU20dec2bin>> dec2bin(12)

ans =

1 1 0 0>> ceil(log2(12))ans =42008, Applied Mathematics NDHU21dec2bin>> dec2bin(88)ans = 1 0 1 1 0 0 0>> ceil(log2(88))ans = 72008, Applied Mathematics NDHU22Recursive function

2008, Applied Mathematics NDHU23Fibonacci number

2008, Applied Mathematics NDHU24Fibonacci number

A tiling with squares whose sides are successive Fibonacci numbers in length 2008, Applied Mathematics NDHU25fibfunction s=fib(n)%% n: a nonnegative integer% fib(1) and fib(0) returns 1 and 0% fib(n) equals fib(n-1)+fib(n-2) for n>1 if n