How i can make this loop in matlab

18 visualizaciones (últimos 30 días)
Zak10
Zak10 el 23 de Mayo de 2013
Comentada: Walter Roberson el 26 de Jun. de 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.

Respuestas (2)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by