Ask your own question, for FREE!
Mathematics 8 Online
OpenStudy (loser66):

matlab help, please I am learning matlab by myself. I got trouble when creating fibonaccing sequence. the output shows 11235813..... in a whole thing like this. I don't know how to separate them like 1 1 2 3 5 8 13 21.....

OpenStudy (loser66):

Here is the code I used fprintf('The Fibonacci sequence to %d terms is \n',N) fprintf('%g',fib) fprintf('/n')

OpenStudy (mathmate):

end each line with a \n (not /n) fprintf('%g\n',fib) will do the trick. For simple output, you can also use disp(); A crude code would be: a=0; b=1; disp(a); disp(b); for i=1:6 c=a+b; disp(c); a=b; b=c; end; which gives the output of: 0 1 1 2 3 5 8 13

OpenStudy (loser66):

I wonder why do we have to use fprintf while we can work on it directly?

OpenStudy (mathmate):

Oh, wait, if you want to print them all on one single line, then you just have to put an extra space after the %g fprintf('The Fibonacci sequence to %d terms is \n',N) fprintf('%g ',fib) fprintf('/n') or separate with a semi-colon + a space (or any other character(s) by doing: fprintf('The Fibonacci sequence to %d terms is \n',N) fprintf('%g; ',fib) fprintf('/n')

OpenStudy (loser66):

oh, got you. Thanks a lot

OpenStudy (mathmate):

fprintf gives you many formatting options, control the number of decimal places, or spacing, or carriage returns, different kinds of numeric or string outputs, etc. It's fascinating! :)

OpenStudy (loser66):

Yes, it is. thanks again :)

OpenStudy (mathmate):

You're welcome! :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!