Maple code to solve a sequence problem

167 Views Asked by At

The Maple code below finds a $N$ such that $|x_n-l|<\varepsilon \: \: \forall n \geq N$ for a given $\varepsilon$ for the sequence $\mathbb{N}\to\mathbb{R}$ defined by $$n \mapsto \frac{n^2}{n^2+30n+189},$$ where $l$ is the limit of the sequence.

I am having trouble determining what is wrong with the code. The code works in general, but I want to list the last few data points, this is why I have set $m:=N-5$. But the code still prints all of the data points.

ev := proc(epsilon) 
global f, l, N, m; 

f := n -> n^2/(n^2+30*n+189);
l := limit(f(n), n = infinity); 
print(l); 

for N while epsilon < abs(f(N)-l) do 
N := N+1 
end do;
print(N); 

m := N-5; 
for m to N do 
A := [m, f(m)]; 
print(A) 
end do; 

end proc

Some help would be great!

2

There are 2 best solutions below

2
On

Try the following:

After m:=N-5 in the for loop use for k from m to n do A:=[k,f(k)]; print(A) end do;

As it is, your $m$ is temporarily used as an unassigned variable used only in the for loop, and the previous assignment of it is ignored, so that for m to N is interpreted as the default for m from 1 to N etc.

0
On

Here are a few issues I noticed:

  1. The last loop in your code doesn't do what you want it to. You want it to start at m=N-5, but the code "for m to N do" reuses m as the loop variable, implicitly discarding its existing value, and starts the loop at the default beginning point, m=1.

    You could fix this by just changing "for m to N do" to "for m from N-5 to N do". Note however that if your sequence converges within epsilon within the first 5 steps, N-5 will be negative, which is probably not what you intended.

  2. The first loop in your code is redundant. Using n as the loop variable already means n will be incremented with each iteration, there's no need to increment it explicitly, so what that actually does is increment by 2 making your code skip all even values of the sequence. That means 50% of the time you'll go one step past the point where you wanted to stop.

  3. You might run into some issues using the default greater-than comparator on an arbitrary function, as it will need to symbolically simplify the input into something than can be compared against epsilon. I'd suggest using evalf.

As a comment on style, coding is saner if you avoid global variables and passing information via print.

Here's a version of your code which incorporates the above comments, takes an arbitrary function and returns a list with a specified number values (default: 5) before the distance is within the specified threshold.

ev := proc(f, epsilon, num:=5)
    local l, N, N0, m; 
    l := limit(f(n), n=infinity);
    for N while evalf(abs(f(N)-l)-epsilon) > 0 do od;
    N0 := max(1, N-num+1);
    [seq([m,f(m), m=N0..N)];
end proc:

A couple examples:

> ev( n -> n^2/(n^2+30*n+189), 0.01 );
[[2972, 8832784/8922133], [2973, 982081/992012], [2974, 8844676/8934085], [2975, 1264375/1277152], [2976, 984064/994005], [2977, 8862529/8952028]]

> ev( x->sin(x)/x, 0.0001, 3)
[[287, (1/287)*sin(287)], [288, (1/288)*sin(288)], [289, (1/289)*sin(289)]]