Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help with looping

  1. Sep 13, 2010 #1
    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.
    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
  2. jcsd
  3. Sep 13, 2010 #2
    i managed to do the loop but how would you go about saving your answers as a vector
  4. Sep 13, 2010 #3
    hey guys is this right?

    clear all

    for u=u
    for u=u
    if u<1001
    elseif u<3001
    elseif u<6001
    elseif u<10001
    elseif u<20001
    elseif u<40001

  5. Sep 13, 2010 #4


    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
    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.
  6. Sep 13, 2010 #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
    elseif u<3001 %1001-3000 KWH usage
    elseif u<6001 %3001-6000 KWH usage
    elseif u<10001 %6001-10000 KWH usage
    elseif u<20001 %10001-20000 KWH usage
    elseif u<40001 %20001-40000 KWH usage
    else %40001+ KWH usage

    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'

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


    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook