[Tutor] Maximum value in an array

Peter Otten __peter__ at web.de
Fri Mar 17 13:02:03 EDT 2017


Aaliyah Ebrahim wrote:

> Hi

Hello again. Was your previous question answered to your satisfaction?

> I am trying to practice the following question for my test tomorrow.
> Please help me find my errors.
 
> *Consider the following displacement sequence  [x1; x2;..... x11] and
> velocity sequence  [v1; v2;....... v11]:x[k+1] = x[k]+ 0.1 v[k]v[k+1] =
> v[k] - 10* x[k] + 0.1 sin(0.1* k *theta)Compute the maximum displacement
> from the displacementsequence. Use theta = 1 [rad] and theinitial values
> x1 = 0 and v1 = 0.*

A few hints:

The above description uses x1, ..., x11 to describe the entries in the 
sequence. Python on the other side uses zero-based indices:

x[0], ..., x[10] # eleven entries

A good start might be to convert the above task to that system before you 
begin coding. Be careful about the one place where k is not an array index.

> Here is my code:

> x = np.zeros(11)
> v = np.zeros(11)
> 
> x[0] = 1
> v[0] = 1

Where do the ones come from?

> a = np.arange(1,10,1)

How many entries does a have? How many should it actually have?

> for k in a:
>     x[k+1] = x[k] + 0.1*v[k]
>     v[k+1] = v[k] - 10*x[k] +0.1*sin(0.1*k*1)

Does this loop ever use the inital values v[0] and x[0]?
 
> print(x.max())

> *This was a follow-up question with a suggested answer:Repeat the maximum
> displacement computation for theta = 1; 2,3.... 20 [rad] and store the
> maximum displacement for each in a list.from math import sinmax_disp =
> []omega_values = range(1, 21, 1)for omega in omega_values:        x =
> [[0.0]
> * 11    v = [0.0] * 11    for k in range(10):        x[k+1] = x[k] + 0.1 *
> v[k]        v[k+1] = v[k] - 10 * x[k] + 0.1 * sin(0.1 * omega * (k + 1))
> max_disp.append( max(x) )    print(max_disp)*
> My question is what do the lines in red mean?

I'm sorry I see garbled text rather than colors. Please try to compose your 
messages using text only, not HTML. Thank you.



More information about the Tutor mailing list