need help with fibo
Show older comments
good afternoon- im noob for this however
I make this code for fibonacci but I dont know how make the code ask for put in two numbers and make this condition while abs(x(k) / x(k-1) –x(k-1)/ x(k-2)) > 0.001
N=input('escoge un numero\n');
fib=zeros(1,N);
fib(1)=1;
fib(2)=1;
k=3;
while k <= N
fib(k)=fib(k-2)+fib(k-1);
k=k+1;
end
fprintf('The Fibonacci sequence to %d terms is\n',N);
fprintf('%g ',fib);
fprintf('\n');
Answers (1)
Image Analyst
on 28 Feb 2016
To ask the user for two numbers, try this snippet:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check for a valid number.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
2 Comments
carlos landin
on 28 Feb 2016
Image Analyst
on 28 Feb 2016
Well, the 0.001 could be one number, but what other number do you want the user to enter?
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!