Python Forum
I dont think that this i hard, but I dont understand!
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I dont think that this i hard, but I dont understand!
#1
Hi! Can someone help me with this question? I dont understand how to do it and our teachers just say "google it" and I cant find something helpful... 

Write a function ctrapezoidal(f, a, b, n) which implements the trape- zoidal approximation. Test this function for different n and compare your result to the exact integral. (Choose a simple function f that you can integrate by hand, e.g. ex. However, don’t make it too simple.)

I found a formula on google when I googled trapezoidal rule so I think that I should use it but I dont know how to do it in python :(
Reply
#2
First, see https://en.wikipedia.org/wiki/Trapezoidal_rule

So, you have to:
  • find n+1 regularly spaced values in the interval [a,b] (including a, and b)
  • call f on each of these values
  • use these values by pairs to compute the area of the trapeze
  • sum these areas to obtain an approximation of the integral
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Thank you so much for answering! Which commands should I use? Im so sorry if I sound stupid but we really haven't done this in class :(
Reply
#4
Doing more would be doing your homework... Start simple, first find a way to compute the N+1 values. Hint, this is a rather simple formula that involves, a, b, N and a number that goes from 0 to 1.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
I understand! I found something similar online, would this be something? 
def ctrapezoidal(f,a,b,n):
   h=(b-a)/n
   s=(f(a)+f(b))*0.5*h;
   for i in xrange(1,n):
       s+=h*f(a+h*i)
   return s
Reply
#6
(Nov-28-2016, 06:33 PM)Linamellannamn Wrote: ...would this be something? 

Yes, that definitely is "something".
Do you what it does, or how it works?  Does it give the right output compared to if you do it by hand, as you said it needs to?
Reply
#7
(Nov-28-2016, 06:33 PM)Linamellannamn Wrote: I understand! I found something similar online, would this be something? 
def ctrapezoidal(f,a,b,n):
   h=(b-a)/n
   s=(f(a)+f(b))*0.5*h;
   for i in xrange(1,n):
       s+=h*f(a+h*i)
   return s

Something like this. However, this is well optimized code, absolutely not the kind of code your teacher would expect from you at this point. So be prepared to be able to explain in excruciating detail how/why this code works (hint: this has more to do with math than with Python, actually).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#8
Okey, do you have any tips on where I can find the commands needed? I really appreciate your help!!
Reply
#9
What to you mean by "commands"? You need to know how to add, multiply and divide numbers, as well as writing a loop. All this is present in the code snippet above. Have you even written code in Python? Or in any other programming language?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#10
No I haven't! This is our first "task" :( By commands I mean: How do I write a loop and what does a loop mean? I think that you use + when you add, * when you multiply and / when you divide!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  This is Very Hard! Harshil 13 4,621 Aug-16-2020, 05:09 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020