Help with my program in MATLAB CODE:

조회 수: 1 (최근 30일)
Anthony Fuentes
Anthony Fuentes 2016년 11월 26일
댓글: bio lim 2016년 11월 28일
HI! I have my program already done, but I need to change to this: The program calculates the sequence of Fibonacci, but i need in it, the following: [I need this in my program]Write a program that asks the user a limit number (> 4) to generate the Fibonacci sequence. The program must calculate the terms of the series and graph them without exceeding the limit number entered by the user. For example, if the user enters 9, the program will calculate the series: 0, 1, 1, 2, 3, 5, 8.% Thanks a lot!
%MY PROGRAM
N=input('Pick a number\n'); %N is the terms, I need to change this in a limit
fib=zeros(1,N);
fib(1)=0;
fib(2)=1;
figure;
for k=3:N
fib(k)=fib(k-2)+fib(k-1);
end
fprintf('The Fibonacci sequence to %d terms is\n',N);
fprintf('%g ',fib);
fprintf('\n');
v=1:N;
% create subplot
figure
% first subplot
subplot(1,2,1)
plot(v,fib,'-o')
title('SubPlot 1')
%second subplot
subplot(1,2,2)
semilogy(v,fib, '-o')
title('SubPlot 2')

채택된 답변

bio lim
bio lim 2016년 11월 28일
coffee = 0;
while ~coffee
G = input('Please insert the limit number that is greater than 4\n');
if G <= 4
coffee = 0;
else coffee = 1;
end
end
num = 100;
fib = zeros(1,num);
fib(1)=0;
fib(2)=1;
for k=3:num;
fib(k)=fib(k-2)+fib(k-1);
end
coffee2 = 1;
for i=1:length(fib)
if fib(i) < G
newFib(coffee2) = fib(i);
coffee2 = coffee2 + 1;
end
end
newFib = newFib';
fprintf('The Fibonacci sequence less than %d term is\n',G);
fprintf('%g ',newFib);
fprintf('\n');
Since num = 100, only the first 100 numbers of the Fibonacci sequence is generated, which you can change the way you want it. The result is:
Please insert the limit number that is greater than 4
6000
The Fibonacci sequence less than 6000 term is
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

추가 답변 (1개)

Anthony Fuentes
Anthony Fuentes 2016년 11월 28일
Thanks a lot!

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by