1

How do I fix the issue of duplication in the following code? The first entry is the same as the second for example.

Input:
import numpy as np
t_max=np.arange(0,2.1,0.1)
for j in range (2,len(t_max)):
    time=np.arange(t_max[2],t_max[j]+0.1,0.1)
    print(time)

Output:
[0.2 0.3]
[0.2 0.3]
[0.2 0.3 0.4]
[0.2 0.3 0.4 0.5]
[0.2 0.3 0.4 0.5 0.6]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
 2. ]
1
  • Probably floating point errors in trying to hit the max of the arrange exactly. Try t_max[j] + 0.11 as the second parameter of the arange. Commented Aug 20, 2021 at 2:28

2 Answers 2

1

Just try using slicing:

import numpy as np
t_max=np.arange(0,2.1,0.1)
for j in range(4, len(t_max) + 1):
    time=t_max[2:j]
    print(time)

Output:

[0.2 0.3]
[0.2 0.3 0.4]
[0.2 0.3 0.4 0.5]
[0.2 0.3 0.4 0.5 0.6]
[0.2 0.3 0.4 0.5 0.6 0.7]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
 2. ]
Sign up to request clarification or add additional context in comments.

Comments

0

This question is about why 0.1+0.2>0.3 in computer programming language.

In your case, when j=2 the code is turn to np.arrange(0.2, 0.2+0.1, 0.1).

There's code output below:

Input:
    a = np.arange(0.2,0.3,0.1)
    print(a)

Output:
    [0.2]

Input:
    a = np.arange(0.2,0.2+0.1,0.1)
    print(a)

    [0.2 0.3]

You can just simply use round to fix it in your case. Like this:

time=np.arange(t_max[2],round(t_max[j]+0.1,1),0.1)

Output:

[0.2]
[0.2 0.3]
[0.2 0.3 0.4]
[0.2 0.3 0.4 0.5]
[0.2 0.3 0.4 0.5 0.6]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]
[0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.]

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.