Lecture+9++Nested+If+_+For+Loops

Lecture+9++Nested+If+_+For+Loops - Complex Decision s A...

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

View Full Document Right Arrow Icon
Complex Decision A step-by-step evaluation of a complex decision
Background image of page 1

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

View Full Document Right Arrow Icon
Nested IF Statement if (condition) statement block elseif (condition) another statement block else another statement block end Structures can be nested within each other
Background image of page 2
Else and Elseif if temperature > 100 disp(‘Too hot - equipment malfunctioning.’) elseif temperature > 75 disp(‘Normal operating range.’) elseif temperature > 60 disp(‘Temperature below desired operating range.’) else disp(‘Too Cold - turn off equipment.’) end
Background image of page 3

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

View Full Document Right Arrow Icon
Nested IF Statements nested if (if, if else, if elseif) -1 1 x 1 y if (x <= -1.0) y = 0. elseif (x <= 0.) y = 1. + x elseif (x <= 1.0) y = 1. - x else y=0. end
Background image of page 4
Repetition for i=1:m for j=1:n a(i,j)=(i+1)^2*sin(0.2*j*pi); end end Do loops
Background image of page 5

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

View Full Document Right Arrow Icon
For Loops for index = start : step : finish statements end for k = 1:length(d) if d(k) < 30 velocity(k) = 0.5 - 0.3*d(k).^2; else velocity(k) = 0.6 + 0.2*d(k)-0.01*d(k).^2 end end Ends after a specified number of repetitions -300 -250 -200 -150 -100 -50 0 50 0 50 100 150 200 v
Background image of page 6
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 7

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

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 8
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
Background image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 25

Lecture+9++Nested+If+_+For+Loops - Complex Decision s A...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online