Cobweb graph

I want to draw a cobweb graph for sin(x). Here is a code (based on an online source) that seem to fail to work, grateful if someone could help me troubleshoot or suggest a better code.
function cobweb(f,a,b,x0,x1,N)
x(1)=-2; % plot orbit starting at x0
for i=1:99
x(i+1)= sin(x(i));
plot([x(i),x(i)],[x(i),x(i+1)]);
hold on
plot([x(i),x(i+1)],[x(i+1),x(i+1)]);
hold on
end
hold off
Thanks.

2 Comments

Fangjun Jiang
Fangjun Jiang on 20 Aug 2011
What is failing? Don't assume everybody knows cobweb graph. This is a MATLAB forum.
Richard
Richard on 21 Aug 2011
It just doesn't plot...

Sign in to comment.

 Accepted Answer

Paulo Silva
Paulo Silva on 21 Aug 2011

0 votes

Check this it cobweb by Warwick Tucker , it works very good.

9 Comments

Fangjun Jiang
Fangjun Jiang on 21 Aug 2011
Don't! If it's a homework, that's too much help!
Oleg Komarov
Oleg Komarov on 21 Aug 2011
Too late! The OP was smart enough to get a link :)
Fangjun Jiang
Fangjun Jiang on 21 Aug 2011
@Oleg, ??
Paulo Silva
Paulo Silva on 21 Aug 2011
The OP said "...or suggest a better code.", I just did the suggestion, anyone can find examples of code online, now if it's really homework and the OP delivers it without understanding the code and gets a bad grade it's not our fault, it's really his fault for being lazy. If on the other hand the OP takes time to understand the code and adds comments or changes/improves it, that would be great.
Richard
Richard on 21 Aug 2011
Thanks, guys. Oleg is quite right in that this given code is indeed based on a code available online. However, this code is NOT required in some assignment. Rather I just want to add an illustration to to enhance a piece of writing -- it is not necessary, just nice to have. I do appreciate your concern though
Oleg Komarov
Oleg Komarov on 21 Aug 2011
@Fangjun: the OP posted a very concise request in a polite way, he's given a properly formatted sample snippet which appears to be the core of the cobweb plot (from Paulo's link). It has shown enough to tell him apart from people that claim spending a week on problems as what 1:10 means so he deserved the link that Paulo suggested.
Also as Paulo correctly points out, understanding the code in the link (as opposed to simply using it) would be more than enough for a hypothetical homework.
@Richard: if you feel that the link suggested by Paulo is what you were looking for, please accept his answer.
Richard
Richard on 21 Aug 2011
@Oleg: Thanks :)
@Paulo: Your link works perfectly indeed, thanks.
Fangjun Jiang
Fangjun Jiang on 21 Aug 2011
@Oleg, The question may be concise. But it's not a good question at all according to your "TUTORIAL: how to ask a question ..." post.
First, there is no explanation or reference to "Cobweb graph". Second, the vague "fail to work" claim. Third, even the further comment "It just doesn't plot" is not accurate. It plots but maybe not as expected.
Now take a look at the OP's original code. There are many input arguments but none was used. So I would assume the code was work-in-progress. Paulo's link provides a complete code with almost identical arguments. That's why I said the help was too much. We are all here to help with MATLAB questions. But I didn't see a MATLAB question.
Everybody can do a search and find some links. If the OP found Paulo's link, that's great. He did a good research. But apparently he didn't.
Oleg Komarov
Oleg Komarov on 21 Aug 2011
@Fangjun: I said at first he was "smart enough" and then he was able "to tell him apart from": I wasn't trying to say, as it may have appeared from the first sentence of my previous comment, that his question was in absolute terms very good.
I am not here to uncover people as free-riders at all costs (or to classify as such). I just didn't feel "fooled" as it may happen with some other posts and that's why I would have posted a comment or maybe (who knows) a link, which on the other side can't be considered in absolute terms a complete answer.

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 20 Aug 2011

0 votes

There is a nice animation here. I think it needs some if-statement in your code.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!