How i can make this loop in matlab

17 Ansichten (letzte 30 Tage)
Zak10
Zak10 am 23 Mai 2013
Kommentiert: Walter Roberson am 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.

Antworten (2)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by