Lecture 9

# Lecture 9 - For Loop function A = for_loop(m,n for i = 1:m...

This preview shows pages 1–6. Sign up to view the full content.

For Loop function A = for_loop(m,n) for i = 1:m for j = 1:n A(i,j) = 50*exp(-0.2*i)^2*sin(0.1*j*pi); end end >> A = for_loop(8,6) A = 10.3570 19.7002 27.1150 31.8756 33.5160 31.8756 6.9425 13.2054 18.1757 21.3669 22.4664 21.3669 4.6537 8.8519 12.1836 14.3226 15.0597 14.3226 3.1195 5.9336 8.1669 9.6007 10.0948 9.6007 2.0910 3.9774 5.4744 6.4356 6.7668 6.4356 1.4017 2.6661 3.6696 4.3139 4.5359 4.3139 0.9396 1.7872 2.4598 2.8917 3.0405 2.8917 0.6298 1.1980 1.6489 1.9384 2.0381 1.9384

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
For Loop M-file for computing the factorial n! MATLAB has a built-in function factorial(n) to compute n! function fout = factor(n) % factor(n): % Computes the product of all the integers from 1 to n. x=1; for i = 1:n x = x*i; end fout = x; >> factor(12) ans = 479001600 >> factor(100) ans = 9.332621544394410e+157
While Loops while condition statements end If the statement is true, the statements are executed If the statement is always true, the loop becomes an “infinite loop The “break” statement can be used to terminate the “while” or “for” loop prematurely. Ends on the basis of a logical condition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
While Loop Compute your checking account balance function checking % Compute balance in checking account Balance = input( 'Current Checking Account Balance (\$) = ' ); Deposit = input( 'Monthly Deposit (\$) = ' ); Subtract = input( 'Monthly Subtractions (\$) = ' ); Month = 0; while Balance >= 0 Month = Month + 1; Balance = Balance + Deposit - Subtract; if Balance >= 0 fprintf( 'Month %3d Account Balance = %8.2f \n' ,Month,Balance) else fprintf( 'Month %3d Account Closed \n' ,Month) end end
While Loop >> checking Current Checking Account Balance (\$) = 8527.20 Monthly Deposit (\$) = 1025.50 Monthly Subtractions (\$) =

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 19

Lecture 9 - For Loop function A = for_loop(m,n for i = 1:m...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online