Posts: 5
Threads: 1
Joined: Nov 2016
Nov-28-2016, 05:29 PM
(This post was last modified: Nov-28-2016, 05:35 PM by Linamellannamn.)
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 :(
Posts: 687
Threads: 37
Joined: Sep 2016
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
Posts: 5
Threads: 1
Joined: Nov 2016
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 :(
Posts: 687
Threads: 37
Joined: Sep 2016
Nov-28-2016, 06:12 PM
(This post was last modified: Nov-28-2016, 06:12 PM by Ofnuts.)
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
Posts: 5
Threads: 1
Joined: Nov 2016
Nov-28-2016, 06:33 PM
(This post was last modified: Nov-28-2016, 07:03 PM by snippsat.)
I understand! I found something similar online, would this be something?
1 2 3 4 5 6 |
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
|
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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?
Posts: 687
Threads: 37
Joined: Sep 2016
(Nov-28-2016, 06:33 PM)Linamellannamn Wrote: I understand! I found something similar online, would this be something?
1 2 3 4 5 6 |
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
Posts: 5
Threads: 1
Joined: Nov 2016
Okey, do you have any tips on where I can find the commands needed? I really appreciate your help!!
Posts: 687
Threads: 37
Joined: Sep 2016
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
Posts: 5
Threads: 1
Joined: Nov 2016
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!
|