Effacer les filtres
Effacer les filtres

setting up a step function

101 vues (au cours des 30 derniers jours)
Sean Smith
Sean Smith le 1 Oct 2011
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 commentaires
Fangjun Jiang
Fangjun Jiang le 3 Oct 2011
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 le 3 Oct 2011
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

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 3 Oct 2011
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 commentaires
Walter Roberson
Walter Roberson le 3 Oct 2011
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 le 3 Oct 2011
It is I must have been doing something wrong before I am sorry about that. Honestly, thank you so much for your help.

Connectez-vous pour commenter.

Plus de réponses (1)

Rick Rosson
Rick Rosson le 2 Oct 2011
  6 commentaires
Sean Smith
Sean Smith le 3 Oct 2011
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 le 3 Oct 2011
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by