How can I calculate the limit of sequence using MATLAB?

146 Views Asked by At

Using MATLAB I want to calculate the limit of the sequence below. $$ a(n)=\frac{n^2+1}{3n^2+1} $$ $$ \lim_{(n\to\infty)}a(n)=? $$ At first I thought \

syms n
a(n)=(n^2+1)/(3*n^2+1);

limit(a(n),'n',inf)

would solve the problem.

However, this couldn't be the answer because the limit is calculated where n is a real number here, while n should be only the natural numbers. So I think I should use 'while' loop, but I can't figure out how I should use it. Any suggestions please?

1

There are 1 best solutions below

0
On

If you want to use the while loop, try to reduce the limit value to a tolerance range using $a(n)>a(n+1)$ for any $n\geq1$. Here is an example:

n = 1;
an = (n^2 + 1)/(3*n^2 + 1);
an_prev = an;

while true
    n = n + 1;
    an = (n^2 + 1)/(3*n^2 + 1);
    if abs(an - an_prev) < 1e-256
        break;
    end
    an_prev = an;
end
disp(an);