The Ctrapezoidal rule

This is the place for queries that don't fit in any of the other categories.

The Ctrapezoidal rule

Postby Cereal » Tue Apr 15, 2014 7:53 am

I tried to post this before, but I don't know if it came up.

I have problems with coding the Ctrapezoidal rule, and I don't know
why it doesn't work.

I wrote like this:
Code: Select all
def ctrapezoidal (f,a,b,n):
    return ((((b-a)/n)/2)*(f(a)+f(b))+((b-a)/n)*(sum((a+((i/n)*(b-a))) for i in xrange(1,n-1)))

print ctrapezoidal (e**x,0,2,50)


Thanks!
L.
Last edited by stranac on Tue Apr 15, 2014 9:34 am, edited 1 time in total.
Reason: Added code tags. Locked.
Cereal
 
Posts: 15
Joined: Tue Apr 15, 2014 7:42 am

Re: The Ctrapezoidal rule

Postby stranac » Tue Apr 15, 2014 9:35 am

Maybe you should try splitting your code into more lines.
It's completely unreadable the way it is.

You should also explain the problem better. "It doesn't work" is not a description of the problem.

Also, read this to learn how to post code: viewtopic.php?t=145
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1115
Joined: Thu Feb 07, 2013 3:42 pm

The Trapezoidal rule

Postby Cereal » Wed Apr 16, 2014 6:43 pm

Hi!

I have an exercise about the Trapezoidal rule; that you can use the formel below instead of an integral:

g = h/2( f(a) + f(b) + h*(the sum of f(xi) from i=1 to (n-1)),
where h= (b-a)/n,
xi= (a+ (i/n)(b-a).


I am coding this and this is my code:



Code: Select all
def ctrapezoidal(f,a,b,n):
def f(x):
return exp(x)
h = ((b-a)/n)
return h/2*((f(a)+f(b)) + h*(a+(i/n)*(b-a) for i in xrange(0,n-1)))


print ctrapezoidal(f,1,2,10)



I have thought a lot about what to do with the function. I have defined the function, but I also have it as an argument in the function ctrapezoidal. Could this be a problem?


The code works great if I don't try to print it, and I wonder how I can fix it. It is complaining on the line above that too when printing.


It says like this:


Code: Select all
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 523, in runfile
    execfile(filename, namespace)
  File "C:\Users\mat11cjo\Downloads\Homeowork 1 Task 1.py", line 18, in <module>
    print ctrapezoidal(f,1,2,10)                                                                         
  File "C:\Users\mat11cjo\Downloads\Homeowork 1 Task 1.py", line 16, in ctrapezoidal
    return h/2*((f(a)+f(b)) + h*(a+(i/n)*(b-a) for i in xrange(0,n-1)))
TypeError: unsupported operand type(s) for *: 'float' and 'generator'
>>> .


Anyone that could help me?

Thanks,
Lucy.
Last edited by micseydel on Wed Apr 16, 2014 8:29 pm, edited 1 time in total.
Reason: OP created new thread, which I merged.
Cereal
 
Posts: 15
Joined: Tue Apr 15, 2014 7:42 am

Re: The Ctrapezoidal rule

Postby micseydel » Wed Apr 16, 2014 8:47 pm

Please don't create new threads regarding the same topic. If the discussion is about the same thing, simply make a new post in that same thread.

Cereal wrote:I am coding this and this is my code:
Code: Select all
def ctrapezoidal(f,a,b,n):
def f(x):
return exp(x)
h = ((b-a)/n)
return h/2*((f(a)+f(b)) + h*(a+(i/n)*(b-a) for i in xrange(0,n-1)))

print ctrapezoidal(f,1,2,10)

This is not valid Python. The point of code tags is preserve the indentation. If when you post in code tags, there is no indentation, you must fix it.

Luckily we can help immediately with the current traceback you have. Your problem is equivalent to this
Code: Select all
2*(i for i in xrange(10))

which is nearly the same as this
Code: Select all
2*xrange(10)

To fix it, if I understand correctly, you probably want to get the sum of that generator
Code: Select all
return h/2*sum((f(a)+f(b)) + h*(a+(i/n)*(b-a) for i in xrange(0,n-1)))

Let us know if you need more help than this.

Cereal wrote:The code works great if I don't try to print it

When you define a function, any issues like the above aren't triggered. You have to run or call the function to have the issue. Strictly speaking, what you said means this
Code: Select all
ctrapezoidal(f,1,2,10)

instead of this
Code: Select all
print ctrapezoidal(f,1,2,10)

when I think you mean that you did this
Code: Select all

(meaning you defined the function above, but didn't call; if I understand what you said, you defined the function but never called it, which is only a problem with a syntax error)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1268
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: The Ctrapezoidal rule

Postby Cereal » Thu Apr 17, 2014 7:59 pm

Thanks for your reply.

I wrote with indentations, but they disapeared when I copied it.

The code works!
I fixed the sum and some parantheses :)

But no to the NEXT problem (exercise)...


"Write a program that calls ctrapezoidal(f,a,b,n) for an increasing number
of discretization points n in a loop. Stop the loop when two successive results are
less than a given tolerance and return the final approximation."

I switched place on n-1 and 0 in the sum to make it increasing. Is that enough?
How do I make the function only depend on the n? Any ideas?
I know how to do with the for loop and break, but there are so many arguments.

Thank you!

// Luce.
Cereal
 
Posts: 15
Joined: Tue Apr 15, 2014 7:42 am


Return to General Coding Help

Who is online

Users browsing this forum: 7stud and 3 guests