Nov-28-2018, 03:18 AM
Hello. I'm new to this website and need to help. I'm trying to change code that loads the python library by "import numpy as np" to loading the library as "from numpy import".
So, I'm using this code:
...and replacing "import numpy as np" with "from numpy import *". Here's what I tried to do:
I ended up with the "TypeError: only size-1 arrays can be converted to Python scalars" error on Python 3 on GDB Online.
Can anyone help?
So, I'm using this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# load the numpy numerical python library import numpy as np # define a function "Area", that performs the calculations def Area(r, N): # compute the width of each rectangle step = (r[ 1 ] - r[ 0 ]) / N # split the interval x_start to x_stop into "step" spaced intervals x = np.arange(r[ 0 ], r[ 1 ], step) # The midpoint of these intervals is half a step # into the middle of them. x_m = x + (step / 2 ) # Compute the value of the function f(x) at the # midpoints of the intervals. f = 0.1 * (x_m * * 2 ) + np.cos( 0.8 * np.pi * x_m) + np.exp( - x_m * * 2 / 10 ) # Sum up the areas of the triangles, where area is given # by height into width. A = np. sum (f * step) return A A = Area([ 0 , 10 ], 5 ) print (A) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# load the numpy numerical python library from numpy import arange import math # define a function "Area", that performs the calculations def Area(r, N): # compute the width of each rectangle step = (r[ 1 ] - r[ 0 ]) / N # split the interval x_start to x_stop into "step" spaced intervals x = arange(r[ 0 ], r[ 1 ], step) # The midpoint of these intervals is half a step # into the middle of them. x_m = x + (step / 2 ) # Compute the value of the function f(x) at the # midpoints of the intervals. f = 0.1 * (x_m * * 2 ) + math.cos( 0.8 * math.pi * x_m) + math.exp( - x_m * * 2 / 10 ) # Sum up the areas of the triangles, where area is given # by height into width. A = math. sum (f * step) return A A = Area([ 0 , 10 ], 5 ) print (A) |
Can anyone help?