making an Xn sequence

조회 수: 33 (최근 30일)
Erin Meyers
Erin Meyers 2020년 9월 11일
댓글: BOB MATHEW SYJI 2020년 9월 13일
I'm trying to code the function Xn=111-(1130/Xn-1)+3000/(Xn-1*Xn-2)
I don't know how to make this sequence in MatLab
I think I can figure out the rest of the problem, I'm just not sure how to do this part particularly
  댓글 수: 2
KSSV
KSSV 2020년 9월 11일
It is pretty simple and straight forward...what have you attempted?
Walter Roberson
Walter Roberson 2020년 9월 11일
If n is at least 3 then would be written X(n-2) and would be written X(n-1) and would be written X(n)

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

답변 (1개)

BOB MATHEW SYJI
BOB MATHEW SYJI 2020년 9월 13일
Hope this helps. x1 and x2 are the 1st and 2nd terms of the sequence respectively. n is the nth term you want.
x1=%the first number of the sequence
x2=%the second number of the sequence
n=%the nth element you want
x=[x1 x2];
for i=3:n
x(i)=111-(1130/x(i-1))+(3000/(x(i-1)*x(i-2)));
end
disp(x(n));
  댓글 수: 2
John D'Errico
John D'Errico 2020년 9월 13일
편집: John D'Errico 2020년 9월 13일
I would suggest you learn why and how to preallocate vectors when they will be grown. Else your code will be exceedingly slow, leaving you to soon post an anguished question of your own at some time - "Why is this code so slow?"
In this case, the code you wrote will create a vector of length n, but your code grows that vector in length one step at a time, forcing MATLAB to reallocate memory at every iteration. It also forces MATLAB to copy the entire vector over at every iteration. If n is at all large, this creates code that will run in O(n^2) time. It is an unnecesarily inefficient coding style, avoidable by a simple line at the beginning like
x = zeros(1,n);
BOB MATHEW SYJI
BOB MATHEW SYJI 2020년 9월 13일
Thank you so much for pointing this out

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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by