Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
range interval & switch case

Subject: range interval & switch case

From: John Wong

Date: 25 Sep, 2009 01:18:05

Message: 1 of 15

Hi, in math we do [8,2) to say 8 <= x < 2
[ means included interval, and 2 means open interval (not included)

how do we accomplish that in matlab?

also, initially i wrote a program using if/else method,

let say
x = input("enter a value");
gasBill = 15;
if A <= 10
 paidAmount = gasBill + 0.02*x;
else
paidAmount = gasBill + 0.04*x;
end
fprintf('%g',paidAmount);

something like that (not exactly what i wrote)
can i use swtich case to accomplish the same purpose done in if/else method?

may someone try to model it? I looked up sources and what they told me is that switch case is usually one-single condition statement instead.

thanks

Subject: range interval & switch case

From: Matt Fig

Date: 25 Sep, 2009 02:37:02

Message: 2 of 15

You could use a switch for this:


TF = A <= 10
switch TF
    case true
        paidAmount = gasBill + 0.02*x;
    otherwise
        paidAmount = gasBill + 0.04*x;
end


Although I personally think the if else is better for these kinds of things.

Subject: range interval & switch case

From: John Wong

Date: 25 Sep, 2009 05:10:19

Message: 3 of 15

Hi Mat, thanks.

clear all;
clc;

wbVolume = input('Enter a number: ')
wbBill = 0;
flatRate = 15;
switch wbVolume
    case 0
        wbBill = 0;
    case wbVolume == ((wbVolume > 0) && (wbVolume - 1000 < 1000))
        wbBill = flatRate;
    case wbVolume >= 1000 && (wbVolume - 1000 < 2000)
        wbBill = flateRate + (wbVolume * 0.0175);
    case wbVolume >= 2000 && (wbVolume - 1000 < 3000)
        wbBill = flateRate + (wbVolume * 0.02);
    case wbVolume > 3000
        wbBill = 70;
    otherwise
        disp('Invalid input');
end
k=menu(wbVolume,wbBill);

The problem I am having is assigning something to each case
as you see, case 0 is exactly meaning wbVolume = 0
so that one works.

However, the rest I don't know how to re-assign.
any thought? everything works fine except for those case wbVolume -- - - ------- not working at all...

Subject: range interval & switch case

From: Matt Fig

Date: 25 Sep, 2009 06:20:04

Message: 4 of 15

"John Wong" <gokoproject@gmail.com> wrote in message
> The problem I am having is assigning something to each case
> as you see, case 0 is exactly meaning wbVolume = 0
> so that one works.
>
> However, the rest I don't know how to re-assign.
> any thought? everything works fine except for those case wbVolume -- - - ------- not working at all...

Which is why I said that the if-else was better for this situation. You have each case evaluating a logical value, whereas a switch is built to handle exclusive definite values only. This is fine for the case where you only have a single true/false (like in your first post), but not when you have multiple evaluations. Use if/else, which is meant to handle multiple logical evaluations.

Subject: range interval & switch case

From: Matt Fig

Date: 25 Sep, 2009 06:55:22

Message: 5 of 15

I just realized that you may not know about elseif. Is that true? If so, I am sorry I wasn't more explicit earlier.


if wbVolume==0
    wbBill = 0;
elseif ((wbVolume > 0) && (wbVolume - 1000 < 1000))
    wbBill = flatRate;
elseif (wbVolume >= 1000 && (wbVolume - 1000 < 2000))
    wbBill = flateRate + (wbVolume * 0.0175);
elseif (wbVolume >= 2000 && (wbVolume - 1000 < 3000))
    wbBill = flateRate + (wbVolume * 0.02);
elseif (wbVolume > 3000)
    wbBill = 70;
else
    disp('Invalid input');
end

Subject: range interval & switch case

From: John Wong

Date: 25 Sep, 2009 11:01:22

Message: 6 of 15

"Matt Fig" <spamanon@yahoo.com> wrote in message <h9hpgq$8ik$1@fred.mathworks.com>...
> I just realized that you may not know about elseif. Is that true? If so, I am sorry I wasn't more explicit earlier.
>
>
> if wbVolume==0
> wbBill = 0;
> elseif ((wbVolume > 0) && (wbVolume - 1000 < 1000))
> wbBill = flatRate;
> elseif (wbVolume >= 1000 && (wbVolume - 1000 < 2000))
> wbBill = flateRate + (wbVolume * 0.0175);
> elseif (wbVolume >= 2000 && (wbVolume - 1000 < 3000))
> wbBill = flateRate + (wbVolume * 0.02);
> elseif (wbVolume > 3000)
> wbBill = 70;
> else
> disp('Invalid input');
> end

thanks. i was trying to force myself to finish up this in a switch case.

Subject: range interval & switch case

From: Steven Lord

Date: 25 Sep, 2009 14:02:39

Message: 7 of 15


"Matt Fig" <spamanon@yahoo.com> wrote in message
news:h9hpgq$8ik$1@fred.mathworks.com...
>I just realized that you may not know about elseif. Is that true? If so,
>I am sorry I wasn't more explicit earlier.
>
>
> if wbVolume==0
> wbBill = 0;
> elseif ((wbVolume > 0) && (wbVolume - 1000 < 1000))
> wbBill = flatRate;
> elseif (wbVolume >= 1000 && (wbVolume - 1000 < 2000))
> wbBill = flateRate + (wbVolume * 0.0175);
> elseif (wbVolume >= 2000 && (wbVolume - 1000 < 3000))
> wbBill = flateRate + (wbVolume * 0.02);
> elseif (wbVolume > 3000)
> wbBill = 70;
> else
> disp('Invalid input');
> end

The conditions are a bit odd -- they overlap. The second condition
simplifies down to:

    elseif ((wbVolume > 0) && (wbVolume < 2000))

and the third to:

    elseif (wbVolume >= 1000 && (wbVolume < 3000))

Note that only one of the lines assigning a value to wbBill (or the line
indicating invalid input) will execute. It doesn't "check the first, then
check the second, then check the third, etc." I would probably write this
as:

if wbVolume < 0
    error('Invalid input');
elseif wbVolume == 0
    wbBill = 0;
elseif wbVolume < 1000 % because the IF condition and the ==0 conditions
were not satisfied, we know wbVolume > 0
    wbBill = flatRate + (wbVolume*0.0175);
% etc

Or, if you wanted to vectorize the billing, to process a vector of wbVolume
data at once:

wbBill = NaN(size(wbVolume)); % Preallocate to an "invalid" value
case1 = wbVolume == 0;
wbBill(case1) = 0;
case2 = wbVolume > 0 & (wbVolume < 1000);
wbBill(case2) = flatRate;
case3 = (wbVolume >= 1000) & (wbVolume < 2000);
wbBill(case3) = flatRate + 0.0175*wbVolume(case3);
% etc

Note that in case 3, I needed to index into both wbBill (to put the results
in the correct locations) as well as wbVolume (to use the appropriate volume
data.)

There are ways to automate in some sense the last approach, but for a small
number of cases it's easier to explicitly write them out.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: range interval & switch case

From: Matt Fig

Date: 25 Sep, 2009 14:56:04

Message: 8 of 15

"Steven Lord" <slord@mathworks.com> wrote in message
> The conditions are a bit odd -- they overlap. The second condition
> simplifies down to....

Yes, I didn't check the OP's logic. I guess that would have been the next question to appear in this thread! ("Now why isn't it working?!")

Subject: range interval & switch case

From: John Wong

Date: 26 Sep, 2009 03:48:03

Message: 9 of 15

hi steve and matt

Thanks for the help. You guys guided me through this. Yeah, the logic wasn't the focus at the moment i created this thread.

So here is my code

wbVolume = input('Enter your current water usage (in cubic feet): ');
flatRate = 15;

case1 = wbVolume < 0;
wbBill(case1) = NaN;

case2 = wbVolume == 0;
wbBill(case2) = 0;

case3 = wbVolume > 0;
wbBill(case3) = flatRate;

case4 = wbVolume >= 1000;
wbBill(case4) = flatRate + ((wbVolume(case4)-1000)*0.0175);

case5 = wbVolume >= 2000;
wbBill(case5) = flatRate + ((wbVolume(case5)-2000)*0.02) + (1000*0.0175);

case6 = wbVolume >= 3000;
wbBill(case6) = 70;

disp('Your water usage is ');
disp(wbVolume);
disp('Your water bill at this point is ');
disp(wbBill);

y = wbVolume;
x = [0:10:80]
plot(x,y)

To cut it short, I tried to create a graph but nothing comes out.
I have gone through this for two days and still have no luck generating this.
I have an alernative soltuon to those - defined my own function to build a piecewise function y
But like I said, I tried my best to test out this program so I can learn from my mistakes. You see, the first half deals with the display the result. Now the second half I just need to somehow, get data from the first half and plot them....

But still, thank you for all the helps,

Subject: range interval & switch case

From: Matt Fig

Date: 26 Sep, 2009 05:49:02

Message: 10 of 15

I still think the logic could use some work, and an if-elseif construct might make the code neater, but this produces a graph.



x = 0:10:80; % To plot we need a vector the same length as this.
for ii = 1:length(x)
    wbVolume(ii) = input(['Enter water usage for period ',num2str(ii),' (in cubic feet): ']);
end
flatRate = 15;
case1 = wbVolume < 0;
wbBill(case1) = NaN;
case2 = wbVolume == 0;
wbBill(case2) = 0;
case3 = wbVolume > 0;
wbBill(case3) = flatRate;
case4 = wbVolume >= 1000;
wbBill(case4) = flatRate + ((wbVolume(case4)-1000)*0.0175);
case5 = wbVolume >= 2000;
wbBill(case5) = flatRate + ((wbVolume(case5)-2000)*0.02) + (1000*0.0175);
case6 = wbVolume >= 3000;
wbBill(case6) = 70;
disp('Your water usage is ');
disp(wbVolume);
disp('Your water bill at this point is ');
disp(wbBill);
plot(x,wbVolume)

Subject: range interval & switch case

From: John Wong

Date: 26 Sep, 2009 18:07:01

Message: 11 of 15

hi matt thanks for the help
just a small question.

i used your code and tested it out, but the graph never showed up. this happened to my other trials (my previous code, i tried to plot but no graph generated)

thanks for your help still.

Subject: range interval & switch case

From: Matt Fig

Date: 26 Sep, 2009 18:38:02

Message: 12 of 15

I don't understand that. Try this: Put all of the code I posted into a new M-File (type the word 'edit' with no quotes, at the command line) then save it as mattfig. Now from the command line, type: mattfig. What happens?

Subject: range interval & switch case

From: John Wong

Date: 26 Sep, 2009 19:21:04

Message: 13 of 15

hi Matt.
>> mattfig
Enter water usage for period 1 (in cubic feet): 909
Enter water usage for period 2 (in cubic feet): 0
Enter water usage for period 3 (in cubic feet): 183
Enter water usage for period 4 (in cubic feet): 499
Enter water usage for period 5 (in cubic feet): 1002
Enter water usage for period 6 (in cubic feet): 3992
Enter water usage for period 7 (in cubic feet): 2849
Enter water usage for period 8 (in cubic feet): 2001
Enter water usage for period 9 (in cubic feet): 0.09
Your water usage is
  1.0e+003 *

    0.9090 0 0.1830 0.4990 1.0020 3.9920 2.8490 2.0010 0.0001

Your water bill at this point is
   15.0000 0 15.0000 15.0000 15.0350 70.0000 49.4800 32.5200 15.0000

Everything works fine. But there is no graph after this.

Subject: range interval & switch case

From: Matt Fig

Date: 26 Sep, 2009 20:26:02

Message: 14 of 15

You didn't happen to make a new M-File with the name 'plot' did you? If you type:

>> which plot
built-in (C:\Program Files\MATLAB\R2007b\toolbox\matlab\graph2d\plot)

Do you see something like that? If not, you have a file named 'plot.m' that is masking the built-in function plot. You need to either delete that new function or rename it to something like myplot.

Subject: range interval & switch case

From: John Wong

Date: 1 Oct, 2009 21:38:01

Message: 15 of 15

"Matt Fig" <spamanon@yahoo.com> wrote in message <h9ltcq$5p9$1@fred.mathworks.com>...
> You didn't happen to make a new M-File with the name 'plot' did you? If you type:
>
> >> which plot
> built-in (C:\Program Files\MATLAB\R2007b\toolbox\matlab\graph2d\plot)
>
> Do you see something like that? If not, you have a file named 'plot.m' that is masking the built-in function plot. You need to either delete that new function or rename it to something like myplot.
thank you matt, i got it :)
thanks for the help, although the graph didn't come out right, i will try to work around with it.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us