Ask your own question, for FREE!
Mathematics 51 Online
OpenStudy (raffle_snaffle):

matlab help

OpenStudy (raffle_snaffle):

%% Problem 9.8 %{ One interesting preoperty of a Fibonacci sequence is that the ratio of the values of adjacent members of the sequence aproaches a number valled "the golden ratio" or (phi). Create a program that accepts the first two additonal values in the sequence as user input and then calculates additional values in the sequence until the ratio of adjacent values converges to within 0.001. You can do this in a while loop by comparing the ratio of element k to element k - 1 and the ratio of element k - 1 to element k - 2. If you call your sequence x, then the code for the while statement is while abs(x(k)/x(k-1)-x(k-1)/x(k-2))>0.001 %}

OpenStudy (raffle_snaffle):

@jim_thompson5910

jimthompson5910 (jim_thompson5910):

how much do you have written?

OpenStudy (raffle_snaffle):

nothing. I didn't really know where to start. Let me work on it for a bit. I made a mini program before on the Fib sequence.

jimthompson5910 (jim_thompson5910):

`Let me work on it for a bit` alright, take your time

OpenStudy (raffle_snaffle):

@jim_thompson5910 I don't undertand what it is asking

jimthompson5910 (jim_thompson5910):

ok so the first thing I'd do is write the code where you get your inputs these inputs will help set up the fibonacci sequence

jimthompson5910 (jim_thompson5910):

`Create a program that accepts the first two additonal values in the sequence as user input`

OpenStudy (raffle_snaffle):

N = input('Enter first value: '); fib = zeros(1, N); fib(1) = 1; fib(2) = 2; k = 3; while k <= N fib(k) = fib(k-1) + fib(k-2); k = k + 1; end fprintf('The Fibonacci sequence to %d terms is \n', N); fprintf('%0.f', fib); fprintf('\n');

jimthompson5910 (jim_thompson5910):

ok that code looks like it will work

OpenStudy (raffle_snaffle):

But this is for a single input

jimthompson5910 (jim_thompson5910):

btw you don't have to have `fib = zeros(1, N);` at least I think you don't

OpenStudy (raffle_snaffle):

am I heading in the right direction here?

OpenStudy (raffle_snaffle):

% asking user to input any number N1 = input('Enter first value: '); N2 = input('Enter second value: '); fib(1) = N1; fib(2) = N2; k = 3; fib(k) = fib(k-1) + fib(k-2); while abs(fib(k)/fib(k-1)-fib(k-1)/fib(k-2))>0.001 fib(k) = fib(k-1) + fib(k-2); k = k + 1; end disp(fib)

jimthompson5910 (jim_thompson5910):

% ask the user for the starting 2 values % the user should type in 1, then 2. fib(1) = input('Enter first value: '); fib(2) = input('Enter second value: '); % in all honesty, this shouldn't be an input section since the fib sequence doesn't change. % The starting values are always the same. % as long as the difference between the adjacent ratios is greater % than 0.001, keep generating terms k = 3; while diff > 0.001 fib(k) = fib(k-1) + fib(k-2); diff = fib(k)/fib(k-1) - fib(k-1)/fib(k-2); k = k + 1; end fprintf('The Fibonacci sequence to %d terms is \n', k); fprintf('%0.f', fib); fprintf('\n');

OpenStudy (raffle_snaffle):

Reading and studying what you displayed

OpenStudy (raffle_snaffle):

I like what you posted better.

jimthompson5910 (jim_thompson5910):

You should find that `fib(k)/fib(k-1)` will approach the golden ratio phi (as k approaches infinity) https://www.mathsisfun.com/numbers/golden-ratio.html http://prntscr.com/b6xji4

jimthompson5910 (jim_thompson5910):

actually I see why the teacher wants an input section. The only problem is that the right inputs must be typed in or else it won't work. For instance, input1 = 1 input2 = 2 or input1 = 5 input2 = 8 are valid input combos but input1 = 5 input2 = 10 is invalid

jimthompson5910 (jim_thompson5910):

ignore the comment where I wrote `% the user should type in 1, then 2. ` you can start at any point in the fib sequence as long as you type in numbers that belong to the sequence and those numbers are adjacent (second input being larger)

OpenStudy (raffle_snaffle):

so fib(1) and fib(2) won't work since I need to assign an input() func to them because fib(1) = 1 and fib(2) = 2 won't work

OpenStudy (raffle_snaffle):

^That means I would have to enter value 1 or 2

jimthompson5910 (jim_thompson5910):

when you type in `fib(1) = 1` it says there's an error?

OpenStudy (raffle_snaffle):

I have not tested. I was fixing my code

jimthompson5910 (jim_thompson5910):

but you said `because fib(1) = 1 and fib(2) = 2 won't work`

OpenStudy (raffle_snaffle):

It was more of a question. if you assign fib(1) and fib(2) I thought this doesn't allow the user to input values beyond 2

jimthompson5910 (jim_thompson5910):

you can add another input line if you need more inputs

OpenStudy (raffle_snaffle):

true or I could just do this % asking user to input any number N1 = input('Enter first value: '); N2 = input('Enter second value: '); fib(1) = N1; fib(2) = N2; k = 3;

jimthompson5910 (jim_thompson5910):

yes that works too. It seems a bit wasteful though because N1 isn't used for anything else. So why not just take out the middleman and connect the two sections directly?

OpenStudy (raffle_snaffle):

so go like this fib(1) = input('... fib(2) = input('....

jimthompson5910 (jim_thompson5910):

your way isn't wrong, it just takes up more lines than needed

jimthompson5910 (jim_thompson5910):

yes you can write it like that

OpenStudy (raffle_snaffle):

so what does the 1 and 2 represent? Induces or the program knowing you put a value in for the first prompt and second prompt?

jimthompson5910 (jim_thompson5910):

you mean fib(1) what the 1 represents?

OpenStudy (raffle_snaffle):

yeah

jimthompson5910 (jim_thompson5910):

fib(1) represents the first value in the fib vector fib(2) represents the second value in the fib vector you do not need to create a blank fib vector because you can add on values by simply writing the next bit of code this bit of code fib(k) = fib(k-1)+fib(k-2) will sets up the kth term based on the previous two terms. Again we don't need to set up a blank vector. This allows the fib vector to get as big as we need (as long as the memory can hold it of course)

OpenStudy (raffle_snaffle):

Oh okay

OpenStudy (raffle_snaffle):

% asking user to input any number fib(1) = input('Enter first value: '); fib(2) = input('Enter second value: '); k = 3; while diff > 0.001 fib(k) = fib(k-1) + fib(k-2); diff = fib(k)/fib(k-1) - fib(k-1)/fib(k-2); k = k + 1; end fprintf('The Fibonacci sequence to %d terms is \n', k); fprintf('%0.f', fib); fprintf('\n');

OpenStudy (raffle_snaffle):

it gives me an error Enter first value: 1 Enter second value: 2 Error using diff Not enough input arguments.

jimthompson5910 (jim_thompson5910):

hmm let me think

jimthompson5910 (jim_thompson5910):

this is what my matlab says Error: "diff" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval.

OpenStudy (raffle_snaffle):

why did we remove the abs() function? Don't we need it?

jimthompson5910 (jim_thompson5910):

hmm I messed up and forgot about the abs

jimthompson5910 (jim_thompson5910):

yes we need it

OpenStudy (raffle_snaffle):

okay

OpenStudy (raffle_snaffle):

Putting the abs() I still get an error Enter first value: 1 Enter second value: 2 Error using diff Not enough input arguments.

jimthompson5910 (jim_thompson5910):

ok I got it to work

jimthompson5910 (jim_thompson5910):

here is the m file and the command output as two separate files

jimthompson5910 (jim_thompson5910):

I changed each instance of `diff` to `difference`. Apparently Matlab has a function named `diff` for differentiation I also changed the line `fprintf('%0.f', fib);` to `fprintf('%0.f, ', fib);` I added in a comma and a space to make the formatting much cleaner

OpenStudy (raffle_snaffle):

why did we have to assign diff = 10?

jimthompson5910 (jim_thompson5910):

The fib sequence Matlab spits out is 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 Take the ratios of the terms 89/55 = 1.61818181818181 55/34 = 1.61764705882352 then subtract 1.61818181818181 - 1.61764705882352 = 0.0005347593583 So we're within range

jimthompson5910 (jim_thompson5910):

`why did we have to assign diff = 10?` when Matlab first sees the "difference" variable in the line while difference > 0.001 it has no idea what the value of "difference" is. So I just made up some number larger than 0.001 This is to set up the variable and define it to be some value. The value of "difference" is then recomputed to be some smaller decimal number

OpenStudy (raffle_snaffle):

Okay makes sense. Thank you very much.

jimthompson5910 (jim_thompson5910):

No problem. I'm not sure if it's me, but I have a feeling your copy of Matlab can handle "diff" just fine? and you don't have to change it to "difference" ?

OpenStudy (raffle_snaffle):

I was actually going to just leave it or at least try to run it with diff instead of difference.

jimthompson5910 (jim_thompson5910):

see if matlab throws an error if you have "diff"

OpenStudy (raffle_snaffle):

Oh that is neat when you change %0.f to %0.f, it puts commas between each number

OpenStudy (raffle_snaffle):

Okay it runs just fine now. I made diff = 5;

jimthompson5910 (jim_thompson5910):

ok great

OpenStudy (raffle_snaffle):

the only down side of putting comma after the f is that a commas appears after 89

OpenStudy (raffle_snaffle):

it shows ....89,

OpenStudy (raffle_snaffle):

It doesn't matter I will just leave it. It bothers me though.

jimthompson5910 (jim_thompson5910):

yeah true. I'm not sure how to get rid of that pesky comma

OpenStudy (raffle_snaffle):

No worries, I will just leave it.

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!