Need help with looping

1. Sep 13, 2010

milo0s

hey guys im 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.
• Use FOR to form loop structure.
• Use IF … ELSE to form selection structure.
• Save data into files.

2. Sep 13, 2010

milo0s

i managed to do the loop but how would you go about saving your answers as a vector

3. Sep 13, 2010

milo0s

hey guys is this right?

clear all
clc

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. Sep 13, 2010

Staff: Mentor

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 (Text):

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. Sep 13, 2010

milo0s

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

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
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: Sep 13, 2010
6. Sep 13, 2010

Staff: Mentor

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.