It's come to this point. I've been trying to create a program in MATLAB for hours now that will calculate Fibonacci's sequence to 20 numbers (I'm just going with nterms=input('Number of terms.')
But anyway...I'm fucked. I have class at 1:35, not to mention a lab report for this class that I've yet to finish. Plus a shitton of other programming also due. Any help at this ungodly hour would be really appreciated.
Btw, this is what I have so far:
x=0;
nterms=input('Enter the number of Fibonacci numbers to display: ');
if nterms<=0
disp('Error: Input must be a positive integer.')
end
for f=(1:nterms)
while x<=nterms
f(1)=0;
f(2)=1;
if nterms<2&&nterms>0
disp(f(1))
end
if nterms>1&&nterms<3
disp(f(1:2))
end
x=3;
f(x)=f(x-2)+f(x-1);
x=x+1;
end
end
disp(f)
Name:
Lauren2013-01-18 5:31
x=0;
nterms=input('Enter the number of Fibonacci numbers to display: ');
if nterms<=0
disp('Error: Input must be a positive integer.')
end
f(1)=0;
if nterms<2&&nterms>0
disp(f(1))
end
f(2)=1;
if nterms>1&&nterms<3
disp(f(1:2))
end
for f=(3:nterms)
x=3;
f(x)=f(x-2)+f(x-1);
x=x+1;
end
disp(f)
I'm getting closer. It got it correct for nterms=1 and nterms=2.
But... i can see it
x = 3; is your trouble... it needs to increment
Name:
Lauren2013-01-18 5:44
Yep, otherwise I'd just figure the numbers out myself and plug them in for each x value. -___-
I find all of this shit interesting, it's just that this is really challenging considering we started matlab all of a week ago and we're on chapter 6 of the book already. Oh engineering school.
I still have even harder ones to do. I tried with flowcharts and it helped, but I still can't get it. The only thing that seems to be the problem is that formula toward the end...
Thanks for the music btw, goes nicely with my super hyperactivity mode caused the Monster I drank 5 hours ago. o_0
Name:
Lauren2013-01-18 5:46
Btw, I noticed that x=3, forgot to mention I moved it. Actually my code looks a bit different now, this part anyway:
x=3;
for f=(3:nterms)
f(x)=f(x-2)+f(x-1);
x=x+1;
end
disp(f)
Name:
Lauren2013-01-18 5:50
I can't precompute the values because I'm learning how to loop and whatnot.
Uh...FPP...I googled that...flat file parsing?
No idea, so probably not.
I no almost nothing about programming. I'm an electrical/biomed engineering major. I will need matlab for research though, unfortunately. I mean it's really useful and I find programming interesting, but it's so damn difficult.
Name:
Anonymous2013-01-18 5:51
ok... so now i think your reusing the one variable, f = 3:n, and f(x) = ... try using a for with some other variable
uh, good point actually. do you start at zero or 1..? i thought it started at 1... but i dunno
Name:
Anonymous2013-01-18 6:07
Thank you, kind anon. >>16 I'm aware of my programming skills, or lack thereof. Almost all of it works though. I'm not aiming for perfection or the most efficiency here. Your explanation wasn't very useful.
>>16 plus that would be a far worse solution for the problem... 'cargo-cult' style i think they call it
...this is a little cleaner, but don't use it ;)
nterms = 0;
while (nterms<=0)
nterms=input('Enter the number of Fibonacci numbers to display:');
end;
f = [0,1];
for x=(3:nterms)
f(x)=f(x-2)+f(x-1);
end
disp(f(1:nterms))
Name:
Anonymous2013-01-18 8:04
>>24
I was trying to tell him that the if statement condition he used could be done better.
>>25 that was with the 'full' recursion, yeah?
It's no good because it branches too much (over-complicated), and it only returns the nth value, so you still need another loop to get the 1-n terms
Take calling fibo(20) once for example ... how many times did you just call/invoke fibo()?
f(20) = f(19) + f(18); [x1]
f(19) = f(18) + f(17); [x1]
f(18) = ... [x2]
f(17) = ... [x3]
f(16) = ... [x5]
f(15) = ... [x8]
....its ~ the sum of the fib sequence itself!?
but yeah, the if statements could use a look, i guess she had a little trouble with the '=' (assignment) vs '==' (equality) operator idea. 1st week though ... argh i'm being mean, sorry =)
//prog-challenge 112// correct the code in the first post in as few points as possible? (Possible moves, del a line : 1 point, move a line : 1 point, add a line : 2 points)
Name:
Anonymous2013-01-18 13:48
>>29
As a black hacker, I am offended by this post.
Name:
Anonymous2013-01-18 13:56
>>30 black hacker
is that the new nickname for codemonkeys?