setting up a step function

I'm trying to do this step function, the final goal is to plot Gabriel's cake. x goes from 0 to 8. f(x)=1/n if n<=x<n+1. I'm not sure how to do this. I'm guessing the if command but I'm not sure how to do it. Any help is appreciated. Thank You.

3 Comments

i don't know if that was totally clear but basically
if 1<=x<2 f(x)=1/1
if 2<=x<3 f(x)=1/2
if 3<=x<4 f(x)=1/3
and so on up until x=8
What is Gabriel's cake? You have a function f(x) as x is the input variable, what is n? How is it related to x?
its just a step function, ignore the n i guess. when 1<=x<2 f(x)=1/1. when 2<=x<3, f(x)=1/2 and so on. x runs from 0 to 8 so when you plot x vs. y it steps down each integer. Walter posted a good article explaining it. http://www.maa.org/pubs/Calc_articles/ma044.pdf

Sign in to comment.

 Accepted Answer

Here, try this:
x = 1:.01:8;
y = zeros(1,length(x));
y(x>=1 & x<2) = 1;
y(x>=2 & x<3) = 1/2;
y(x>=3 & x<4) = 1/3;
y(x>=4 & x<5) = 1/4;
y(x>=5 & x<6) = 1/5;
y(x>=6 & x<7) = 1/6;
y(x>=7 & x<8) = 1/7;
y(x>=8) = 1/8;
plot(x, y);
What difference do you see between this plot and the plot I already gave the short formula for? Do you agree that this longer code implements the criteria for Gabriel's Cake? If there is no visible difference between the output of this code and the output of my earlier code, then are both wrong or do you agree that my earlier code was correct?

3 Comments

The code you just gave is correct. The previous code was a little different. Thank you I really appreciate it, huge help. I just have one more question. I'm using [x,y,z]cylinder(y) to make the shape and then plotting it with mesh(x,y,z). When I plot it, it makes all the axis 0-1 instead of x and y being one and z going from 0-8 (my original x values). Any idea how to get one of the axis to use my original x values? I get errors if i change anything. Is cylinder(y) the only way to do volumes of revolution?
Once the trivial change is made from
plot(x, 1/floor(x))
to
plot(x, 1./floor(x))
to get the original code to work at all, the result is pixel-for-pixel identical to the longer version I show above.
For your other question: try mesh(8*z,y,x)
It is I must have been doing something wrong before I am sorry about that. Honestly, thank you so much for your help.

Sign in to comment.

More Answers (1)

>> doc floor

6 Comments

what?
x = 1:.01:8;
plot(x, 1/floor(x))
the doesn't give me the same answers as the step function
What differences do you see between what you are looking for and what you get ? I do not have access to MATLAB itself at the moment, but when I plot in a different package, the result looks like what I would expect. Or is the problem merely with the end condition? Do you want it to end just _before_ 8, or right _at_ 8 (which would be 1/8), or do it want it to end just _before_ 9, or what ?
start at 1 and end at 8.
using the floor(x) you suggested is giving me a plot very similar to what i want but each level of it gets smaller and smaller at the same rate. whereas the step function i posted each level is smaller but the amount that its smaller is less every level. the first level or ring has a radius of 1. the next is half that (1/2). the next is a 1/3, and the next 1/4. The floor is giving me something that looks more like the first level is 1, the next is 1/2, then next is 1/4, the next is 1/8, ect. I can try to post pictures if that doesn't make sense.
The code given is the code for the function you describe, which is the same function described in http://www.maa.org/pubs/Calc_articles/ma044.pdf

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!