1

These two sections my code are giving the following error message.

def Simpsons(a, b, n, f, x):
    h=(b-a)/n
    s = 0.
    step = a + h 
    for i in range(1, int(n/2)):
        s += 4*f(x)
        #step += 2*h

    step = a + 2 * h
    for i in range(2, int(n/2 - 1)):
        s += 2*f(x)
        #step += 2 * h

    area = (h/3) * (f(a) + f(b) + s)
    return area

and

for i in range(0, len(list_n)):
    for j in range(0, len(Functions)):
        x = np.linspace(a, b, list_n[i])
        error_simp[j, i] = Exact_intergrals[j] - Simpsons(0, 1,         list_n[i], Functions[j], x)
print(error_simp)

Give this message:

   error_simp[j, i] = Exact_intergrals[j] - Simpsons(0, 1, list_n[i], Functions[j], x)
ValueError: setting an array element with a sequence.

Why is this? everything I have tried does not get rid of it.

ettanany
  • 19,038
  • 9
  • 47
  • 63
EmmaB
  • 21
  • 2
  • Make sure if you post Python code that you reproduce your indentation accurately. Otherwise you're introducing new problems in the code people are trying to find problems in. – khelwood Dec 02 '16 at 11:41
  • side remark: you should make a case destinction in Simpson (integration) if n is even or odd. The simpson rule can be extended to an odd number of intervalls, by handling the last intervall seperately (Fitting a parabola into the last 3 points and integrate between the last 2 points only) – Markus Dutschke Dec 02 '16 at 12:16
  • What is `error_simp`? Shape, dtype? What does the RHS produce? – hpaulj Dec 02 '16 at 12:45

2 Answers2

0

I guess you are trying to load array into error_simp[j,i] which might be able to contain only float numbers.

Here is a simple example which illustrates this error:

import numpy
b = numpy.zeros(5)
b[0] = numpy.zeros(5) - 1

Produces the same error.

Looks like either Simpsons function returns an array or Exact_intergrals[j] contains an array.

If you need to save arrays in error_simp matrix, try to add one more dimension:

error_simp = np.zeros((rows, cols, additional_dimension_size))

(Another option is to explicitly set dtype=object option in constructor, but it's much slower and not recommended).

There are a few similar questions that might help: this one and this

Community
  • 1
  • 1
idmitme
  • 899
  • 7
  • 13
0

Your Simpsons function when given an array returns an array of the same size:

In [335]: Simpsons(0,1,10, lambda x:x**2, np.linspace(0,1,3))
Out[335]: array([ 0.03333333,  0.2       ,  0.7       ])

You don't show how error_simp is initialized. The use error_simp[j, i]=... and the error suggests that

np.zeros((n,m), float)

or something like that.

For further help you need to specify what you expect to put in error_simp. If x is the same for all elements, you could create a matching 3d array. If x has different lengths, it will have to be dtype=object. But I like to discourage this usage by beginners. Such an array is little more than a glorified (or debased) list of lists. It's easy to have unrealistic expectations of what you can do with such an array.

=========

Looking more carefully at

for i in range(0, len(list_n)):
    for j in range(0, len(Functions)):
        x = np.linspace(a, b, list_n[i])

you probably are creating different size x. You don't show list_n, but it probably is something like [10,12,14,...], testing this function for different n.

hpaulj
  • 221,503
  • 14
  • 230
  • 353