need help with fibo
이전 댓글 표시
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');
답변 (1개)
Image Analyst
2016년 2월 28일
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
carlos landin
2016년 2월 28일
Image Analyst
2016년 2월 28일
Well, the 0.001 could be one number, but what other number do you want the user to enter?
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!