필터 지우기
필터 지우기

setting up a step function

조회 수: 101 (최근 30일)
Sean Smith
Sean Smith 2011년 10월 1일
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