Start learning today, and be successful in your academic & professional career. Start Today!

Loading video...

Enter your Sign on user name and password.

Start learning today, and be successful in your academic & professional career. Start Today!

Loading video...

- 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.

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.

♦ 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;

else

cost(i) = 35 + (cons(i) - 1000)*0.10 + 5;

end

end

disp('cost = ')

disp(cost)

$9, $15, $25, $40, $90

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;

else

cost(i) = 35 + (cons(i) - 1000)*0.10 + 5;

end

end

disp('cost = ')

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;

end

disp(money)

check: 1000*1.01^{12}

for month = 1:12

money = money*1.01;

end

disp(money)

check: 1000*1.01

*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.

Answer

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

I. Introduction | ||
---|---|---|

MATLAB Essentials, Part 1 | 8:36 | |

MATLAB Essentials, Part 2 | 7:39 | |

II. MATLAB Fundamentals | ||

Introduction to Programs | 6:42 | |

Arrays & Matrices | 7:06 | |

Operator, Expression & Statements | 13:26 | |

Repeating With For | 14:21 | |

Decisions, Part 1 | 14:35 | |

Decisions, Part 2 | 8:53 | |

Last Notes on MATLAB Introduction | 9:47 | |

III. Logical Vectors | ||

Logical Vectors, Part 1 | 11:23 | |

Logical Vectors, Part 2 | 11:40 | |

IV. Program Design | ||

Program Design & Algorithm Development | 19:36 | |

V. Graphics | ||

Graphics, Part 1 | 9:14 | |

Graphics, Part 2 | 17:20 | |

Graphics, Part 3 | 18:37 | |

VI. Loops | ||

Loops | 10:38 | |

While | 17:01 | |

VII. Project | ||

Projectile Problem | 16:33 | |

VIII. Function M-Files | ||

Function M-Files, Part 1 | 12:55 | |

Function M-Files, Part 2 | 14:14 | |

Function M-Files, Part 3 | 11:54 | |

IX. Graphics Continued | ||

More on Graphics | 11:13 | |

Graphical User Interface, Part 1 | 7:25 | |

Graphical User Interface, Part 2 | 9:33 | |

X. Dynamical Systems | ||

Dynamical Systems | 12:45 | |

XI. Simulation | ||

Examples of Simulation | 10:45 | |

XII. Numerical Methods | ||

Numerical Methods | 22:20 | |

XIII. Simulink | ||

Simulink | 9:37 |

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')

else

x1=....

x2=....

end