function [c] = fibo (a, b, n)
n = input('n:');
Every time your user calls your fibo function, you're going to ask them to pass a value into it for n (as the third input) then immediately turn around and prompt them to type in a replacement value for n.
IMO if part of your design requires your user to enter data manually, you should have (at least) two separate functions: the interface function that can call input and then just calls the processing function and the processing function that operates solely on data passed into it (no prompting of the user allowed.)
But that's a bit of an aside. You already have most of what you want. It's not too difficult to translate your first script into a function.
Turn this first statement into the function declaration line. "function theOutputs = fibonacci(theInputs)". I'll let you decide what to write in place of theOutputs and theInputs.
fibo = zeros(1,n);
for n = 1:n
Reusing the variable name technically works, but it can be confusing that the role of n kind of changes. I'd use a different variable name for the loop variable.
if n==1
fibo(n)=0;
else
if n==2
fibo(n)=1;
else
fibo(n)=fibo(n-1)+fibo(n-2);
end
end
end
disp(fibo);
Turn this last line into the line that defines whatever variables you need to return. It should assign into whatever variables appear in theOutputs above.
Best Answer