Hi , ive tried to write a function which would Display an error but for some reason i dont know why, but it dosent work ,and i dont know where i wnet wrong .I want n to denote the positive integer that is greateer than or equal to 3 or otherwise the function will display ''error.your input is invalid
n=input ('sequence_matrix_');
if n > 3
disp ('Error. Your input is invalid.');
else
fib=[1,3:n];
for i=3:n
fibb(i)=fibb(i-1)*3+fibb(i-2);
end
end
diag(fibb)

 채택된 답변

Star Strider
Star Strider 2021년 7월 21일

0 개 추천

The code works. The if logic is the opposite of what you indicated that you want.
Correcting that (and a couple other small problems) —
n = 2
n = 2
if n < 3
disp ('Error. Your input is invalid.');
else
fibb=[1,3:n];
for i=3:n
fibb(i)=fibb(i-1)*3+fibb(i-2);
end
end
Error. Your input is invalid.
if exist('fibb')
diag(fibb)
end
n = 4
n = 4
if n < 3
disp ('Error. Your input is invalid.');
else
fibb=[1,3:n];
for i=3:n
fibb(i)=fibb(i-1)*3+fibb(i-2);
end
end
if exist('fibb')
diag(fibb)
end
ans = 4×4
1 0 0 0 0 3 0 0 0 0 10 0 0 0 0 33
.

댓글 수: 2

Sona H
Sona H 2021년 7월 21일
편집: Sona H 2021년 7월 21일
o i see thank you
Star Strider
Star Strider 2021년 7월 21일
As always, my pleasure!
.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2021년 7월 21일

댓글:

2021년 7월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by