How i can make this loop in matlab

12 views (last 30 days)
Zak10
Zak10 on 23 May 2013
Commented: Walter Roberson on 26 Jun 2022
The electricity accounts of residents in a small town are calculated as follows:
  • a. If 500 units of fewer are used, the cost is 2 cents per unit.
  • b. If more than 500 but no more than 1000 units are used, the cost is $10 for the first units and 5 cents for every unit in excess of 500.
  • c. If more than 1000 units are used, the cost is $35 for the first 1000 units plus 10 cents for every unit in excess of 1000.
  • d. A basic service fee of $5 is charged, no matter how much electricity is used.
Write a program that enter the following five conceptions into a vector and uses a for loop to calculate and display the total charges for each one: 200, 500, 700, 1000, 1500.

Answers (2)

Image Analyst
Image Analyst on 24 May 2013
Start by taking each of those numbers and assigning them to a variable with a descriptive name, for example:
electricityUsed = [200, 500, 700, 1000, 1500];
centsPerUnit = 2;
and so on. Then use a for loop and write the equations. The whole program in only around 10 lines or so. I'm certain you can do it, especially if you have done any programming whatsoever before.

Aleem
Aleem on 26 Jun 2022
electricityUsed = [1 200 500 700 1000 1500];
for units = electricityUsed
if units <= 500;
cost = units * units;
elseif units <= 1000
cost = 10 + 0.05*(units - 500);
else
cost = 35 + 0.1*(units - 1000);
end
amount_payable = 5 + cost;
disp ([(units) amount_payable])
end
  1 Comment
Walter Roberson
Walter Roberson on 26 Jun 2022
"If 500 units of fewer are used, the cost is 2 cents per unit."
2 cents per unit. Not unit squared

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!