0

This program aims to generate a resistance value according to:

enter image description here

(Given an array of resistances and temperatures).

In order to obtain the current value of resistance, the equation requires an initial resistance and initial temperature values (R_0, T_0) from the array. It also requires the successive value of temperature (T) from the array.

My try:

r_model=[]
for r in r_values:
    result = r*(1+2.9*(t_values[r+1]-t_values[r]))
    fit2.append(result)
r_model = array(r_model)

My error:

index out of bounds
8765674
  • 1,074
  • 4
  • 15
  • 31
  • Why are you looping over r_values which you are trying to compute rather than the t_values which are known? – Floris Feb 16 '13 at 16:33

3 Answers3

1

If you need the array index while processing a for loop, you can use enumerate:

r_model=[]
for (index, r) in enumerate(r_values):
    result = r*(1+2.9*(t_values[index+1]-t_values[index]))
    fit2.append(result)
r_model = array(r_model)

If r_values is [1500,2500,0.0001], then enumerate(r_values) will iterate through this sequence:

(0, 1500)
(1,2500)
(2,0.0001)

And at each step, you can use the index (0,1, and 2) to get the proper value from the t_values list.

Ian Clelland
  • 39,551
  • 8
  • 79
  • 83
0

You have:

r_values = arange(1500,2500,0.0001)    #R_0 values
for r in r_values:
# . . .
    t_values[r+1]

So the first time through the loop you wind up with:

t_values[1501]

But t_values only has 3 elements.

jimhark
  • 4,613
  • 1
  • 23
  • 26
  • Yeah, sorry the arrays are something I wrote up quickly. I actually take the arrays from a .txt files with elements of the same dimensions. Thanks for noticing! – 8765674 Feb 16 '13 at 16:23
  • It doesn't matter they are the same dimension - you are indexing with the value, not the index. – Floris Feb 16 '13 at 16:26
  • And at any rate if the _were_ the same dimensions you will still get a problem because of the `[r+1]` index... T array must be larger than r array. – Floris Feb 16 '13 at 16:27
0

You are indexing t with the value of r rather than the index. for ri, r in r_values I believe, is the syntax - then use ri to index t_values

See also Accessing the index in Python 'for' loops

Another problem in your code: your equation talks about difference with T0, but you take the difference t_values[r+1]-t_values[r]. Should be t_values[ri]-t_values[0] ?

Community
  • 1
  • 1
Floris
  • 43,828
  • 5
  • 63
  • 112