MATLAB Loop & Electricity Billing Help

  • MATLAB
  • Thread starter milo0s
  • Start date
In summary: Why are you storing the data in temporary storage? Just create a vector and stuff the values into it. I can see you're trying to get the output as a 20x2 matrix, but it's not necessary. Just create a vector and append to it. I see you're trying to save the data, but the save statement is inside the for loop. You only need to save once. The code I gave you was intended to be a starting point, not a solution. You need to understand it and make it yours.
  • #1
milo0s
4
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
  • #2
i managed to do the loop but how would you go about saving your answers as a vector
 
  • #3
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
 
  • #4
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.
 
  • #5
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:
  • #6
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.
 

What is a MATLAB loop?

A MATLAB loop is a programming construct that allows a series of instructions to be repeated multiple times. It is useful for automating repetitive tasks and performing calculations on large amounts of data.

How can a MATLAB loop be used in electricity billing?

A MATLAB loop can be used to calculate the total electricity usage for a given period of time by looping through each individual meter reading and adding them together. It can also be used to calculate the cost of electricity based on the current rates.

Can a MATLAB loop be used to identify patterns in electricity usage?

Yes, a MATLAB loop can be used to analyze electricity usage data and identify patterns or trends. This can be helpful in understanding peak usage times and optimizing energy consumption.

What are the advantages of using a MATLAB loop for electricity billing?

One advantage is that it can save time and reduce errors by automating the process of calculating electricity usage and costs. It also allows for more flexibility in analyzing and manipulating large amounts of data.

Are there any limitations to using a MATLAB loop for electricity billing?

One limitation is that it requires some knowledge of programming and familiarity with the MATLAB software. Additionally, it may not be suitable for highly complex billing systems that require more advanced calculations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
10K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
Back
Top