Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Holy fuck.

Name: Lauren yo. 2013-01-18 5:15

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: Lauren 2013-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.

Name: Anonymous 2013-01-18 5:34

does it need to be recursive?

Name: Anonymous 2013-01-18 5:36

Here is some nice demoscene music:
http://www.youtube.com/watch?v=UTG1nnJih1Q

also, read SICP.

that is all I can do to help you.

Name: Anonymous 2013-01-18 5:36

>>3
of course, else it would be just a waste of exercise time, teaching no important concept.

Name: Anonymous 2013-01-18 5:37

oh wait, sorry it just looked like it was

Name: Anonymous 2013-01-18 5:40

oh it is? i couldn't see the function declaration... are you sure your code is recursive? i see a loop.. i guess kinda if you count that part..

Name: Anonymous 2013-01-18 5:42

i can write one in octave pretty quick if you need ;)

Name: Anonymous 2013-01-18 5:42

>>1
Why don't you just precompute the values and copy them somewhere?

Name: Anonymous 2013-01-18 5:43

>>1
BTW, are you FFP?

Name: Anonymous 2013-01-18 5:44

But... i can see it
x = 3; is your trouble... it needs to increment

Name: Lauren 2013-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: Lauren 2013-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: Lauren 2013-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: Anonymous 2013-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

ie for (iter = 3:n)
 f(iter) = f(iter - 1) + f(iter - 2)


and i think that's it =)

Name: Anonymous 2013-01-18 5:55

Wow! There are a lot of shitty things in that code. Just look at the algorithm on wikipedia and implement it. It's not difficult.

F(1) is 0
F(2) is 1
F(n) is F(n-2) + F(n-1) 

function fibo(n)
   if(n<0)
     return -1
   else if(n==1)
     return 0
   else if(n==2)
     return 1
   else
    //complete this part on your own
   endif
end

Name: Anonymous 2013-01-18 6:00

>>16 shutup ^^

Name: Anonymous 2013-01-18 6:06

uh, good point actually. do you start at zero or 1..? i thought it started at 1... but i dunno

Name: Anonymous 2013-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.

Name: Anonymous 2013-01-18 6:07

>>18 My book says it starts at 0.

Name: Anonymous 2013-01-18 6:12

ah no worries =) , except the Almost..? It is all working now right?

Name: Anonymous 2013-01-18 6:16

goes nicely with my super hyperactivity mode caused the Monster I drank 5 hours ago.
WHAT

Dump matlab and read SICP. Pfft.

Name: Anonymous 2013-01-18 6:37

>>21
=)
What's with the actual redditards lately?

Name: Anonymous 2013-01-18 6:44

>>23 see #17

>>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: Anonymous 2013-01-18 8:04

>>24
I was trying to tell him that the if statement condition he used could be done better.

Name: Anonymous 2013-01-18 8:30

>>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

Name: Anonymous 2013-01-18 8:37

...assuming it was going to be like
   else
    //complete this part on your own
      return (fibo(n-1) + fibo(n-2))
   endif

Name: Anonymous 2013-01-18 9:07

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!?

Name: Anonymous 2013-01-18 10:23

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: Anonymous 2013-01-18 13:48

>>29
As a black hacker, I am offended by this post.

Name: Anonymous 2013-01-18 13:56

>>30
black hacker
is that the new nickname for codemonkeys?

Name: Anonymous 2013-01-18 15:39

>>31
yes

Name: Anonymous 2013-01-18 15:58

>>31
oh god I fucking lol'd.

Name: Anonymous 2013-01-18 16:23

>>33
fucking lel XD

Name: Anonymous 2013-01-18 18:33

#include <iostream>
using namespace std;

int main() {
  int x = 0, y = 1, z = 0;

  for(int i = 0; i < 30; i++) {
    cout << x << endl;
    z = x + y;
    x = y;
    y = z;
  }
  return 0;
}


c++ is a better matlab than matlab :-)

Name: Anonymous 2013-01-18 20:26

<<

Name: Anonymous 2013-01-19 23:20

restoring...

Don't change these.
Name: Email:
Entire Thread Thread List