Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of MATLAB
  • Discussion

  • Study Guides

  • Practice Questions

  • Download Lecture Slides

  • Table of Contents

  • Related Books

Lecture Comments (2)

0 answers

Post by Elvis Wodi on February 16, 2015

why did the we get one as the result

0 answers

Post by Mohamed Kaba on August 11, 2014

Instead of using nested "ifs" could we use "&" in the first "if", and then use "else" at the end. For example:

if (a~=0) & (d<0)
   disp('complex roots')

Decisions, Part 2

  • An if construct can contain further ifs and so on, this action is called nesting, and is different than elseif ladder.
  • The switch statement executes certain statements based on the value of a variable or expression.

Decisions, Part 2

The electricity accounts of residents in a very small town are calculated as follows:
♦  500 units or less are used the cost is 2 cents per unit;
♦  more than 500, but not more than 1000 units are used, the cost is $10 for the first 500 units, and then 5 cents for every unit in excess of 500;
♦  if more than 1000 units are used, the cost is $35 or the first 1000 units plus 10 cents for every unit in excess of 1000;
♦  in addition, a basic service fee of $5 is charged, no matter how much electricity is used.

Write a program which enters the following five consumptions into a vector, and uses a for loop to calculate and display the total charge for each one: 200, 500, 700, 1000, 1500.
cons = [200 500 700 1000 1500];
for i = 1:numel(cons)
    if cons(i)  < = 500
     cost(i) = cons(i)*0.02 + 5;
    elseif cons(i)  < = 1000
     cost(i) = 10 + (cons(i) - 500)*0.05 + 5;
     cost(i) = 35 + (cons(i) - 1000)*0.10 + 5;
disp('cost = ')

$9, $15, $25, $40, $90
If you invest $1000 for one year at an interest rate of 12 percent, the return is $1120 at the end of the year. But if interest is compounded at the rate of 1 percent monthly (i.e. 1/12 of the annual rate), you get slightly more interest because it is compounded. Write a program which uses a for loop to compute the balance after a year of compounding interest in this way. The answer should be $1126.83. Evaluate the formula for this result separately as a check.
money = 1000;
for month = 1:12
    money = money*1.01;

check: 1000*1.0112

*These practice questions are only helpful when you work on them offline on a piece of paper and then use the solution steps function to check your answer.


Decisions, Part 2

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Multiple Ifs Versus Elseif 0:09
  • Nested Ifs 2:24
    • Example Nested If
  • Vectorizing Ifs 4:45
  • Switch 5:12