MATLAB MATLAB Loop & Electricity Billing Help

  • Thread starter Thread starter milo0s
  • Start date Start date
AI Thread Summary
The discussion revolves around using MATLAB to calculate electricity bills based on varying usage rates. A user is trying to implement a loop structure to process data from a file called usage.dat, which contains the electricity usage of approximately 20 users. The initial code attempts to calculate the bill using nested loops and conditional statements but contains errors, particularly in the loop structure. Suggestions include correcting the loop to iterate based on the actual number of entries in the data file rather than a fixed number. Additionally, there is a need to clarify the input data format for better processing.
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
1
Views
10K
Back
Top