How i can make this loop in matlab

조회 수: 18 (최근 30일)
Zak10
Zak10 2013년 5월 23일
댓글: Walter Roberson 2022년 6월 26일
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.
  댓글 수: 1
James Tursa
James Tursa 2013년 5월 23일
What have you written so far?

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2013년 5월 24일
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 2022년 6월 26일
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
Walter Roberson
Walter Roberson 2022년 6월 26일
"If 500 units of fewer are used, the cost is 2 cents per unit."
2 cents per unit. Not unit squared

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by