numerical integration methods question python












1















I am trying to integrate the following formula:



enter image description here



Below is my attempt to perform this integration using a homemade scheme for f(a) = sin(a).



def func(x):
return math.sin(x)

def integration (f, n, r, a, dtheta ):

summation = 0
theta = 0
while theta <= 2*np.pi:

f_arg = a + r*np.exp(1j*theta)
second = np.exp(-1j*theta*n)

summation += f(f_arg) * second * dtheta
theta += dtheta

return math.factorial(n)*summation / (2*np.pi*r**n)

integration(func, n=1, r=1, a=0, dtheta=2*np.pi/10000)


The first derivative (n=1) of f(a) = sin(a) is f'(a) = cos(a). When evaluated at a = 0, this should give cos(0) = 1, however, it does not. Where am I going wrong?










share|improve this question

























  • return math.factorial(n) / (2*np.pi*r**n) doesn't seem to involve f, theta, dtheta, or anything you did in the while loop.

    – user2357112
    Nov 12 '18 at 20:00











  • @user2357112, thank you for attention, updating question (as that was a typo), still didn't fix.

    – RedPen
    Nov 12 '18 at 20:01
















1















I am trying to integrate the following formula:



enter image description here



Below is my attempt to perform this integration using a homemade scheme for f(a) = sin(a).



def func(x):
return math.sin(x)

def integration (f, n, r, a, dtheta ):

summation = 0
theta = 0
while theta <= 2*np.pi:

f_arg = a + r*np.exp(1j*theta)
second = np.exp(-1j*theta*n)

summation += f(f_arg) * second * dtheta
theta += dtheta

return math.factorial(n)*summation / (2*np.pi*r**n)

integration(func, n=1, r=1, a=0, dtheta=2*np.pi/10000)


The first derivative (n=1) of f(a) = sin(a) is f'(a) = cos(a). When evaluated at a = 0, this should give cos(0) = 1, however, it does not. Where am I going wrong?










share|improve this question

























  • return math.factorial(n) / (2*np.pi*r**n) doesn't seem to involve f, theta, dtheta, or anything you did in the while loop.

    – user2357112
    Nov 12 '18 at 20:00











  • @user2357112, thank you for attention, updating question (as that was a typo), still didn't fix.

    – RedPen
    Nov 12 '18 at 20:01














1












1








1








I am trying to integrate the following formula:



enter image description here



Below is my attempt to perform this integration using a homemade scheme for f(a) = sin(a).



def func(x):
return math.sin(x)

def integration (f, n, r, a, dtheta ):

summation = 0
theta = 0
while theta <= 2*np.pi:

f_arg = a + r*np.exp(1j*theta)
second = np.exp(-1j*theta*n)

summation += f(f_arg) * second * dtheta
theta += dtheta

return math.factorial(n)*summation / (2*np.pi*r**n)

integration(func, n=1, r=1, a=0, dtheta=2*np.pi/10000)


The first derivative (n=1) of f(a) = sin(a) is f'(a) = cos(a). When evaluated at a = 0, this should give cos(0) = 1, however, it does not. Where am I going wrong?










share|improve this question
















I am trying to integrate the following formula:



enter image description here



Below is my attempt to perform this integration using a homemade scheme for f(a) = sin(a).



def func(x):
return math.sin(x)

def integration (f, n, r, a, dtheta ):

summation = 0
theta = 0
while theta <= 2*np.pi:

f_arg = a + r*np.exp(1j*theta)
second = np.exp(-1j*theta*n)

summation += f(f_arg) * second * dtheta
theta += dtheta

return math.factorial(n)*summation / (2*np.pi*r**n)

integration(func, n=1, r=1, a=0, dtheta=2*np.pi/10000)


The first derivative (n=1) of f(a) = sin(a) is f'(a) = cos(a). When evaluated at a = 0, this should give cos(0) = 1, however, it does not. Where am I going wrong?







python numerical-methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 23:07









Matthias Ossadnik

57427




57427










asked Nov 12 '18 at 19:57









RedPenRedPen

14317




14317













  • return math.factorial(n) / (2*np.pi*r**n) doesn't seem to involve f, theta, dtheta, or anything you did in the while loop.

    – user2357112
    Nov 12 '18 at 20:00











  • @user2357112, thank you for attention, updating question (as that was a typo), still didn't fix.

    – RedPen
    Nov 12 '18 at 20:01



















  • return math.factorial(n) / (2*np.pi*r**n) doesn't seem to involve f, theta, dtheta, or anything you did in the while loop.

    – user2357112
    Nov 12 '18 at 20:00











  • @user2357112, thank you for attention, updating question (as that was a typo), still didn't fix.

    – RedPen
    Nov 12 '18 at 20:01

















return math.factorial(n) / (2*np.pi*r**n) doesn't seem to involve f, theta, dtheta, or anything you did in the while loop.

– user2357112
Nov 12 '18 at 20:00





return math.factorial(n) / (2*np.pi*r**n) doesn't seem to involve f, theta, dtheta, or anything you did in the while loop.

– user2357112
Nov 12 '18 at 20:00













@user2357112, thank you for attention, updating question (as that was a typo), still didn't fix.

– RedPen
Nov 12 '18 at 20:01





@user2357112, thank you for attention, updating question (as that was a typo), still didn't fix.

– RedPen
Nov 12 '18 at 20:01












1 Answer
1






active

oldest

votes


















3














It seems that your problem is the math.sin function, which doesn't support complex arguments:



i = np.exp(.5j * np.pi)
math.sin(i), np.sin(i)
(6.123233995736766e-17, (9.44864380126377e-17+1.1752011936438014j))


It also throws a warning (but not an error...):



ComplexWarning: Casting complex values to real discards the imaginary part


Using np.sin instead fixes the problem.



In general, the implementation might be simpler to express (and easier to debug) with more use of numpy, like



def integration(func, a, n, r, n_steps):
z = r * np.exp(2j * np.pi * np.arange(0, 1, 1. / n_steps))
return math.factorial(n) * np.mean(func(a + z) / z**n)

np.allclose(1., integration(np.sin, a=0., n=1, r=1., n_steps=100))

True





share|improve this answer





















  • 1





    Replacing math.sin with np.sin isn't enough to produce a result of 1 when I try it.

    – user2357112
    Nov 12 '18 at 22:45






  • 1





    @user2357112 in the original example there is also n=2 used instead of n=1 in the function call. If you change this it is OK. Have edited question.

    – Matthias Ossadnik
    Nov 12 '18 at 22:49













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269229%2fnumerical-integration-methods-question-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














It seems that your problem is the math.sin function, which doesn't support complex arguments:



i = np.exp(.5j * np.pi)
math.sin(i), np.sin(i)
(6.123233995736766e-17, (9.44864380126377e-17+1.1752011936438014j))


It also throws a warning (but not an error...):



ComplexWarning: Casting complex values to real discards the imaginary part


Using np.sin instead fixes the problem.



In general, the implementation might be simpler to express (and easier to debug) with more use of numpy, like



def integration(func, a, n, r, n_steps):
z = r * np.exp(2j * np.pi * np.arange(0, 1, 1. / n_steps))
return math.factorial(n) * np.mean(func(a + z) / z**n)

np.allclose(1., integration(np.sin, a=0., n=1, r=1., n_steps=100))

True





share|improve this answer





















  • 1





    Replacing math.sin with np.sin isn't enough to produce a result of 1 when I try it.

    – user2357112
    Nov 12 '18 at 22:45






  • 1





    @user2357112 in the original example there is also n=2 used instead of n=1 in the function call. If you change this it is OK. Have edited question.

    – Matthias Ossadnik
    Nov 12 '18 at 22:49


















3














It seems that your problem is the math.sin function, which doesn't support complex arguments:



i = np.exp(.5j * np.pi)
math.sin(i), np.sin(i)
(6.123233995736766e-17, (9.44864380126377e-17+1.1752011936438014j))


It also throws a warning (but not an error...):



ComplexWarning: Casting complex values to real discards the imaginary part


Using np.sin instead fixes the problem.



In general, the implementation might be simpler to express (and easier to debug) with more use of numpy, like



def integration(func, a, n, r, n_steps):
z = r * np.exp(2j * np.pi * np.arange(0, 1, 1. / n_steps))
return math.factorial(n) * np.mean(func(a + z) / z**n)

np.allclose(1., integration(np.sin, a=0., n=1, r=1., n_steps=100))

True





share|improve this answer





















  • 1





    Replacing math.sin with np.sin isn't enough to produce a result of 1 when I try it.

    – user2357112
    Nov 12 '18 at 22:45






  • 1





    @user2357112 in the original example there is also n=2 used instead of n=1 in the function call. If you change this it is OK. Have edited question.

    – Matthias Ossadnik
    Nov 12 '18 at 22:49
















3












3








3







It seems that your problem is the math.sin function, which doesn't support complex arguments:



i = np.exp(.5j * np.pi)
math.sin(i), np.sin(i)
(6.123233995736766e-17, (9.44864380126377e-17+1.1752011936438014j))


It also throws a warning (but not an error...):



ComplexWarning: Casting complex values to real discards the imaginary part


Using np.sin instead fixes the problem.



In general, the implementation might be simpler to express (and easier to debug) with more use of numpy, like



def integration(func, a, n, r, n_steps):
z = r * np.exp(2j * np.pi * np.arange(0, 1, 1. / n_steps))
return math.factorial(n) * np.mean(func(a + z) / z**n)

np.allclose(1., integration(np.sin, a=0., n=1, r=1., n_steps=100))

True





share|improve this answer















It seems that your problem is the math.sin function, which doesn't support complex arguments:



i = np.exp(.5j * np.pi)
math.sin(i), np.sin(i)
(6.123233995736766e-17, (9.44864380126377e-17+1.1752011936438014j))


It also throws a warning (but not an error...):



ComplexWarning: Casting complex values to real discards the imaginary part


Using np.sin instead fixes the problem.



In general, the implementation might be simpler to express (and easier to debug) with more use of numpy, like



def integration(func, a, n, r, n_steps):
z = r * np.exp(2j * np.pi * np.arange(0, 1, 1. / n_steps))
return math.factorial(n) * np.mean(func(a + z) / z**n)

np.allclose(1., integration(np.sin, a=0., n=1, r=1., n_steps=100))

True






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 20:29

























answered Nov 12 '18 at 20:22









Matthias OssadnikMatthias Ossadnik

57427




57427








  • 1





    Replacing math.sin with np.sin isn't enough to produce a result of 1 when I try it.

    – user2357112
    Nov 12 '18 at 22:45






  • 1





    @user2357112 in the original example there is also n=2 used instead of n=1 in the function call. If you change this it is OK. Have edited question.

    – Matthias Ossadnik
    Nov 12 '18 at 22:49
















  • 1





    Replacing math.sin with np.sin isn't enough to produce a result of 1 when I try it.

    – user2357112
    Nov 12 '18 at 22:45






  • 1





    @user2357112 in the original example there is also n=2 used instead of n=1 in the function call. If you change this it is OK. Have edited question.

    – Matthias Ossadnik
    Nov 12 '18 at 22:49










1




1





Replacing math.sin with np.sin isn't enough to produce a result of 1 when I try it.

– user2357112
Nov 12 '18 at 22:45





Replacing math.sin with np.sin isn't enough to produce a result of 1 when I try it.

– user2357112
Nov 12 '18 at 22:45




1




1





@user2357112 in the original example there is also n=2 used instead of n=1 in the function call. If you change this it is OK. Have edited question.

– Matthias Ossadnik
Nov 12 '18 at 22:49







@user2357112 in the original example there is also n=2 used instead of n=1 in the function call. If you change this it is OK. Have edited question.

– Matthias Ossadnik
Nov 12 '18 at 22:49




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269229%2fnumerical-integration-methods-question-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values