numerical integration methods question python
I am trying to integrate the following formula:
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
add a comment |
I am trying to integrate the following formula:
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
return math.factorial(n) / (2*np.pi*r**n)
doesn't seem to involvef
,theta
,dtheta
, or anything you did in thewhile
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
add a comment |
I am trying to integrate the following formula:
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
I am trying to integrate the following formula:
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
python numerical-methods
edited Nov 12 '18 at 23:07
Matthias Ossadnik
58427
58427
asked Nov 12 '18 at 19:57
RedPenRedPen
14317
14317
return math.factorial(n) / (2*np.pi*r**n)
doesn't seem to involvef
,theta
,dtheta
, or anything you did in thewhile
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
add a comment |
return math.factorial(n) / (2*np.pi*r**n)
doesn't seem to involvef
,theta
,dtheta
, or anything you did in thewhile
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
add a comment |
1 Answer
1
active
oldest
votes
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
1
Replacingmath.sin
withnp.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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
1
Replacingmath.sin
withnp.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
add a comment |
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
1
Replacingmath.sin
withnp.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
add a comment |
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
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
edited Nov 12 '18 at 20:29
answered Nov 12 '18 at 20:22
Matthias OssadnikMatthias Ossadnik
58427
58427
1
Replacingmath.sin
withnp.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
add a comment |
1
Replacingmath.sin
withnp.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
return math.factorial(n) / (2*np.pi*r**n)
doesn't seem to involvef
,theta
,dtheta
, or anything you did in thewhile
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