How i can make this loop in matlab

17 visualizzazioni (ultimi 30 giorni)
Zak10
Zak10 il 23 Mag 2013
Commentato: Walter Roberson il 26 Giu 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.

Risposte (2)

Image Analyst
Image Analyst il 24 Mag 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 il 26 Giu 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 Commento
Walter Roberson
Walter Roberson il 26 Giu 2022
"If 500 units of fewer are used, the cost is 2 cents per unit."
2 cents per unit. Not unit squared

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by