setting up a step function

101 次查看(过去 30 天)
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 个评论
Fangjun Jiang
Fangjun Jiang 2011-10-3
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?
Sean Smith
Sean Smith 2011-10-3
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

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2011-10-3
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 个评论
Walter Roberson
Walter Roberson 2011-10-3
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)
Sean Smith
Sean Smith 2011-10-3
It is I must have been doing something wrong before I am sorry about that. Honestly, thank you so much for your help.

请先登录,再进行评论。

更多回答(1 个)

Rick Rosson
Rick Rosson 2011-10-2
  6 个评论
Sean Smith
Sean Smith 2011-10-3
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.
Walter Roberson
Walter Roberson 2011-10-3
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by