MATLAB Loop & Electricity Billing Help

  • Context: MATLAB 
  • Thread starter Thread starter milo0s
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around using MATLAB to calculate electricity bills based on varying rates depending on usage. Participants are seeking help with implementing loops, conditional statements, and data handling in MATLAB, specifically for a task involving reading from a file and saving results.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes the problem of calculating electricity bills based on usage tiers and seeks guidance on using loops and conditionals in MATLAB.
  • Another participant mentions they have managed to implement a loop but is unsure how to save results as a vector.
  • Several participants share their MATLAB code attempts, with one participant questioning the correctness of their nested loop structure.
  • Another participant points out errors in the loop structure and suggests a different approach to iterating through the data.
  • A participant expresses confusion about the use of "for u = u" and questions the input data format, suggesting it would be better if the first value indicated the number of subsequent values.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct implementation of the loop and conditional logic in MATLAB. There are multiple competing views on how to structure the code and handle the input data.

Contextual Notes

Some participants note limitations in understanding the input data format and the implications of their current coding practices, such as the use of "for u = u," which may not be considered good programming style.

milo0s
Messages
4
Reaction score
0
hey guys I am new to MATLAB and cannot get my head around the whole loop thing
my problem is :

An electricity distribution company has different rates of charges on
electricity used based on the usage as follows.
Usage in KWH $ / KWH
0 -- 1000 0.15
1001 – 3000 0.17
3001 -- 6000 0.18
6001 -- 10000 0.20
10001 -- 20000 0.23
20001 -- 40000 0.25
40001 and over 0.26
Now 20 or so users have their usage recorded and saved in the file called usage.dat,
And you are asked to use Matlab to calculate the amount each user needs to pay for
his/her electricity bill.
Aim:
Practice in using Matlab for the following functionality.
• Load data from files.
• Use FOR to form loop structure.
• Use IF … ELSE to form selection structure.
• Save data into files.


thankx in advance
 
Physics news on Phys.org
i managed to do the loop but how would you go about saving your answers as a vector
 
hey guys is this right?clear all
clc
u=load('usage.dat')';

for u=u
for u=u
if u<1001
c=u*.15;
elseif u<3001
c=1000*.15+(u-1000)*.17;
elseif u<6001
c=1000*.15+2000*.17+(u-2000-1000)*.18;
elseif u<10001
c=1000*.15+2000*.17+(u-3000-2000-1000)*.2;
elseif u<20001
c=1000*.15+2000*.17+3000+.2+(u-4000-3000-2000-1000)*.23;
elseif u<40001
c=1000*.15+2000*.17+3000*.2+4000*.23+(u-10000-4000-3000-2000-1000)*.25;
else
c=1000*.15+2000*.17+3000*.2+4000*.23+10000*.25+(u-20000-10000-4000-3000-2000-1000)*.26;

end
end
 
milo0s said:
hey guys is this right?


clear all
clc
u=load('usage.dat')';

for u=u
for u=u
if u<1001
c=u*.15;
elseif u<3001
c=1000*.15+(u-1000)*.17;
elseif u<6001
c=1000*.15+2000*.17+(u-2000-1000)*.18;
elseif u<10001
c=1000*.15+2000*.17+(u-3000-2000-1000)*.2;
elseif u<20001
c=1000*.15+2000*.17+3000+.2+(u-4000-3000-2000-1000)*.23;
elseif u<40001
c=1000*.15+2000*.17+3000*.2+4000*.23+(u-10000-4000-3000-2000-1000)*.25;
else
c=1000*.15+2000*.17+3000*.2+4000*.23+10000*.25+(u-20000-10000-4000-3000-2000-1000)*.26;




end
end

No, it's not. You have a number of errors. If you don't have access to Matlab documentation, here's a link to a tutorial that I find helpful - http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf.

You seem to have roughly the right idea on your if...elseif logic, but your for loop is not right. Your for loop should look like this:
Code:
for n = 1:20 
  % body of for loop
end

The loop above will execute 20 times. Your code will need to determine how many values are in usage.dat, and use that number to control the loop, not 20.
 
when ever i do

for n=1:20

it loops n 20 times and multiplies it by .15 each time

which results in a vector from 1 to 3

this is what i put together which seems to work

clear all %Removes all variables, functions, and MEX-files from memory, leaving the workspace empty.
clc %Clear Command Window
%format bank %two digit decimal
temp_store = []; %Create temporary empty matrix
u=load('usage.dat')'; %load data

data = []; %Create empty data matrix
for u=u %open loop

if u<1001 %0-1000 KWH usage
c=u*.15;
elseif u<3001 %1001-3000 KWH usage
c=1000*.15+(u-1000)*.17;
elseif u<6001 %3001-6000 KWH usage
c=1000*.15+2000*.17+(u-2000-1000)*.18;
elseif u<10001 %6001-10000 KWH usage
c=1000*.15+2000*.17+(u-3000-2000-1000)*.2;
elseif u<20001 %10001-20000 KWH usage
c=1000*.15+2000*.17+3000+.2+(u-4000-3000-2000-1000)*.23;
elseif u<40001 %20001-40000 KWH usage
c=1000*.15+2000*.17+3000*.2+4000*.23+(u-10000-4000-3000-2000-1000)*.25;
else %40001+ KWH usage
c=1000*.15+2000*.17+3000*.2+4000*.23+10000*.25+(u-20000-10000-4000-3000-2000-1000)*.26;


end
data = [temp_store c]; %combines 'temp_store' and 'c' matrix
temp_store = data; %defines 'temp_store' as 'data'
data=data'; %coverts data to its inverse

x= data; %defines 'x' as 'data'

end
u=load('usage.dat');
Results =[u x]; %combines 'u' and 'x' as a new matrix named 'Results'
save mydata.dat Results* %saves the 'Results' matrix into a 'mysata.dat' file
 
Last edited:
I have no idea what for u = u actually does. It's not very good programming style.

What's the format of the input data file? Is it just a straight list of electricity usages for 20 or so people? It would be nice if the first data value was how many values followed it. Otherwise you need to cycle through the data, counting how many values are in the file, then use that value to control you for loop.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 2 ·
Replies
2
Views
3K