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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
While Loop >> checking Current Checking Account Balance ($) = 8527.20 Monthly Deposit ($) = 1025.50 Monthly Subtractions ($) =
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online