How do i fix this 'not enough input arguments' error

조회 수: 2 (최근 30일)
EDWARD
EDWARD 2024년 9월 6일
답변: Jatin 2024년 9월 10일
Im trying to create a fibonacci sequence and have mostlyeverything figured out but i can't seem to fix this.
function fib = fibonacci(n)
% This function gives the first n numbers in the Fibonacci sequence
% Input: n - the number of Fibonacci numbers to generate
% Output: fib - a row vector containing the first n Fibonacci numbers
if n<=0
error('Input must be a positive integer');
elseif n == 1
fib = 0;
return;
elseif n == 2
fib = [0, 1];
return;
end
% Initialize the first two Fibonacci numbers
fib = zeros(1, n);
fib(1) = 0;
fib(2) = 1;
% Compute the Fibonacci sequence
for i = 3:n
fib(i) = fib(i-1) + fib(i-2);
end
end
  댓글 수: 5
Umar
Umar 2024년 9월 7일
Hi @John D'Errico,
Thanks for sharing your knowledge. Your insights are indeed valuable in emphasizing the importance of computational precision and the limitations inherent in numerical methods for large indices in Fibonacci calculations.
Umar
Umar 2024년 9월 7일

Hi @John D’Errico,

What are your thoughts about computing Fibonacci numbers using symbolic representation as shown below. Doesn’t this approach circumvent the limitations of floating-point precision, allowing for accurate calculations regardless of size:

function f = fibonacci(n)
  if n < 0
      error('Input must be a non-negative integer.');
  end
  f = sym(zeros(1, n + 1));
  f(1) = 0; 
  f(2) = 1; 
  for k = 3:n + 1
      f(k) = f(k - 1) + f(k - 2);
  end
end
% Example usage:
f100 = fibonacci(100);
disp(f100);

Please see attached.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jatin
Jatin 2024년 9월 10일
When you define a function within a script, MATLAB identifies the function by the file name. Therefore, executing the script is equivalent to running the command:
>> fibonacci
This is equal to calling the function without providing any input arguments. To handle this situation, you should verify the input arguments and provide a meaningful error message or notification within the function script. You can utilize the nargin variable, which holds the number of input arguments passed to the function, to display an informative message as shown below:
function fib = fibonacci(n)
% This function gives the first n numbers in the Fibonacci sequence
% Input: n - the number of Fibonacci numbers to generate
% Output: fib - a row vector containing the first n Fibonacci numbers
if nargin < 1
error('You need to pass at least one parameter to this function!');
end
if n <= 0
error('Input must be a positive integer');
elseif n == 1
fib = 0;
return;
elseif n == 2
fib = [0, 1];
return;
end
% Initialize the first two Fibonacci numbers
fib = zeros(1, n);
fib(1) = 0;
fib(2) = 1;
% Compute the Fibonacci sequence
for i = 3:n
fib(i) = fib(i-1) + fib(i-2);
end
end
Kindly go through the documentation of "nargin" for more details:
Hope this helps!

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by