ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 29 Mar 2017 20:12:57 +0200Integrate piecewise function with change of variablehttps://ask.sagemath.org/question/37114/integrate-piecewise-function-with-change-of-variable/I would like to integrate a piecewise defined function while operating a change of variable. I start by defining the function and another variable involved in the change of variable:
phi(x) = piecewise([([-1,1], (1-abs(x))*(1-abs(x))*(1+2*abs(x)))]);
phi(x) = phi.extension(0);
h=pi/n;
h=h.n();
What I would like to do is integrate the function `phi(x/h-1)` between `0` and `pi` so I try it and results in
integral(phi(x/h-1),x,0,pi)
ValueError: substituting the piecewise variable must result in real number
So I then try to use another variable which I try to define to be 'real'
t=var('t')
assume(t,'real');
integral(phi(t/h-1),t,0,pi)
but it results in the same error... Now I try the "lambda" method since it worked when calling the `plot` function with the same change of variable; but fail again
integral(lambda t: phi(t/h-1),t,0,pi)
TypeError: unable to convert <function <lambda> at 0x16d71f140> to a symbolic expression
Now I try to use another integration method with `definite_integral` but get the same errors, only different for the "lambda" method
definite_integral(lambda x: phi(x/h-1),x,0,pi)
TypeError: cannot coerce arguments: no canonical coercion from <type 'function'> to Symbolic Ring
Is there any way around this? I really do not know what else to try...
Wed, 29 Mar 2017 01:28:56 +0200https://ask.sagemath.org/question/37114/integrate-piecewise-function-with-change-of-variable/Answer by calc314 for <p>I would like to integrate a piecewise defined function while operating a change of variable. I start by defining the function and another variable involved in the change of variable:</p>
<pre><code>phi(x) = piecewise([([-1,1], (1-abs(x))*(1-abs(x))*(1+2*abs(x)))]);
phi(x) = phi.extension(0);
h=pi/n;
h=h.n();
</code></pre>
<p>What I would like to do is integrate the function <code>phi(x/h-1)</code> between <code>0</code> and <code>pi</code> so I try it and results in</p>
<pre><code>integral(phi(x/h-1),x,0,pi)
ValueError: substituting the piecewise variable must result in real number
</code></pre>
<p>So I then try to use another variable which I try to define to be 'real'</p>
<pre><code>t=var('t')
assume(t,'real');
integral(phi(t/h-1),t,0,pi)
</code></pre>
<p>but it results in the same error... Now I try the "lambda" method since it worked when calling the <code>plot</code> function with the same change of variable; but fail again</p>
<pre><code>integral(lambda t: phi(t/h-1),t,0,pi)
TypeError: unable to convert <function <lambda> at 0x16d71f140> to a symbolic expression
</code></pre>
<p>Now I try to use another integration method with <code>definite_integral</code> but get the same errors, only different for the "lambda" method</p>
<pre><code>definite_integral(lambda x: phi(x/h-1),x,0,pi)
TypeError: cannot coerce arguments: no canonical coercion from <type 'function'> to Symbolic Ring
</code></pre>
<p>Is there any way around this? I really do not know what else to try...</p>
https://ask.sagemath.org/question/37114/integrate-piecewise-function-with-change-of-variable/?answer=37116#post-id-37116This has to do with the implementation of `piecewise`, I think.
I can get a numerical integral to work by doing the following:
phi(x)=(heaviside(x+1)-heaviside(x-1))*(1-abs(x))*(1-abs(x))*(1+2*abs(x))
numerical_integral(phi(x/h-1),0,pi)
Another option would be to stop the integral at the point at which the function is zero.
phi2(x)=(1-abs(x))*(1-abs(x))*(1+2*abs(x))
integral(phi2(x/h-1),x,0,2*pi/n)
Wed, 29 Mar 2017 03:46:53 +0200https://ask.sagemath.org/question/37114/integrate-piecewise-function-with-change-of-variable/?answer=37116#post-id-37116Comment by jrojasqu for <p>This has to do with the implementation of <code>piecewise</code>, I think.</p>
<p>I can get a numerical integral to work by doing the following:</p>
<pre><code>phi(x)=(heaviside(x+1)-heaviside(x-1))*(1-abs(x))*(1-abs(x))*(1+2*abs(x))
numerical_integral(phi(x/h-1),0,pi)
</code></pre>
<p>Another option would be to stop the integral at the point at which the function is zero.</p>
<pre><code>phi2(x)=(1-abs(x))*(1-abs(x))*(1+2*abs(x))
integral(phi2(x/h-1),x,0,2*pi/n)
</code></pre>
https://ask.sagemath.org/question/37114/integrate-piecewise-function-with-change-of-variable/?comment=37128#post-id-37128@calc314 This is awesome... Had I thought about Heaviside for defining my piecewise functions, many of my problems in Sage would have been solved instantly... ThanksWed, 29 Mar 2017 20:12:57 +0200https://ask.sagemath.org/question/37114/integrate-piecewise-function-with-change-of-variable/?comment=37128#post-id-37128