2

I have constructed the following code using MATLAB:

numero=60;
a=zeros(numero,1)
b=zeros(numero+1,1)   
for i=1:numero+1
    a(i)=-cos(pi*(i-1)/numero)
end
figure
plot(a,b, '*')

It serves to calculate the nodes of a Chebyshev polynomial of order numero and store it in a vector called a. I need to reproduce this in Python. My attempt at a solution is

from mpmath import chebyt, chop, taylor
import numpy as np
import sympy as sp


numero=60
nodes = []
for i in range(numero+2):
     auxiliary=-np.cos(np.pi*(i-1)/numero)
     nodes.append(auxiliary)

[float(i) for i in nodes]
nodes.sort()
print(nodes)

However, there are is an issue in python's output. The first is the fact that the second number of the list, -0.9986295347545738 appears twice, it is also the third element of nodes. I don't know why this happens and I would like to know if someone can tell me how to avoid this error.

1
  • please, post your expected output Commented Dec 2, 2019 at 14:39

1 Answer 1

3

The issue comes about in this line:

for i in range(numero+2)

compared to:

for i=1:numero+1

The variable i is starting at 0, and really we want it to start at 1. We can see that this causes the error from the following:

>>> -np.cos(np.pi*(0-1)/numero)
-0.9986295347545738
>>> -np.cos(np.pi*(1-1)/numero)
1.0
>>> -np.cos(np.pi*(2-1)/numero)
-0.9986295347545738

etc. So the fix is to switch to:

for i in range(1, numero+2)

You can also omit these lines:

[float(i) for i in nodes]
nodes.sort()

This gives output:

[-1.0, -0.9986295347545738, -0.9945218953682733, -0.9876883405951378, ..., 0.9876883405951377, 0.9945218953682734, 0.9986295347545738, 1.0]

as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.