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

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

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

Complex Decision A step-by-step evaluation of a complex decision

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

View Full Document
Nested IF Statement if (condition) statement block elseif (condition) another statement block else another statement block end Structures can be nested within each other
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

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

View Full Document
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
Repetition for i=1:m for j=1:n a(i,j)=(i+1)^2*sin(0.2*j*pi); end end Do loops

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

View Full Document
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
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

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

View Full Document
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
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

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 / 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
Ask a homework question - tutors are online