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.Thu, 04 Jun 2020 22:40:34 +0200How to define a function that is piecewise for specific independent variable valueshttps://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/I'm solving for the coefficients of a fourier series, where the function to be approximated is
f(x) is 0 from -pi to +pi/2, and +1 from pi/2 to pi. It then repeats over all periods. The equation for the coefficients, c_n is (1/2pi)*int_-pi^pi f(x)e^{-inx}dx.
I've solved these to be c_0 = 1/4, c_n=1,5,...=-(1+i)/n, c_n=2,6,...=2i/n, c_n=3,7,...=(1-i)/n and c_n=4,8,...=0.
Now I'd like to check my work with sage. However, I'm having trouble getting my solutions into an equation form. The integral expression is easy. I can do
from sage.symbolic.integration.integral import definite_integral
from sage.symbolic.integration.integral import indefinite_integral
x=var('x')
n=var('n')
assume(n,'integer')
assume(n!=0)
c=(1/(2*pi))*definite_integral(e^(-I*n*x),x,pi/2,pi)
c.full_simplify()
However, I don't know how to represent the piecewise part so I can check it with
bool(c==...)
Is there a way to do this with sage piecewise definitions? Something else?
One thought is that I could check the cases separately. That is, first check the n=1,5,... case etc. However, I'm not sure how to restrict n to be this. I can restrict it to be integer, or odd but I'm not sure about this.
Also, please forgive the horrendous math formatting. I wasn't able to get my latex formatted correctly here and I don't have enough rep to post a picture.Thu, 04 Jun 2020 06:50:52 +0200https://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/Answer by Juanjo for <p>I'm solving for the coefficients of a fourier series, where the function to be approximated is</p>
<p>f(x) is 0 from -pi to +pi/2, and +1 from pi/2 to pi. It then repeats over all periods. The equation for the coefficients, c_n is (1/2pi)*int_-pi^pi f(x)e^{-inx}dx.</p>
<p>I've solved these to be c_0 = 1/4, c_n=1,5,...=-(1+i)/n, c_n=2,6,...=2i/n, c_n=3,7,...=(1-i)/n and c_n=4,8,...=0.</p>
<p>Now I'd like to check my work with sage. However, I'm having trouble getting my solutions into an equation form. The integral expression is easy. I can do</p>
<pre><code>from sage.symbolic.integration.integral import definite_integral
from sage.symbolic.integration.integral import indefinite_integral
x=var('x')
n=var('n')
assume(n,'integer')
assume(n!=0)
c=(1/(2*pi))*definite_integral(e^(-I*n*x),x,pi/2,pi)
c.full_simplify()
</code></pre>
<p>However, I don't know how to represent the piecewise part so I can check it with</p>
<pre><code>bool(c==...)
</code></pre>
<p>Is there a way to do this with sage piecewise definitions? Something else?</p>
<p>One thought is that I could check the cases separately. That is, first check the n=1,5,... case etc. However, I'm not sure how to restrict n to be this. I can restrict it to be integer, or odd but I'm not sure about this.</p>
<p>Also, please forgive the horrendous math formatting. I wasn't able to get my latex formatted correctly here and I don't have enough rep to post a picture.</p>
https://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/?answer=51762#post-id-51762You can define $f$ in terms of the Heaviside function and then integrate directly without any imports. Try this code:
f(x) = heaviside(x-pi/2)
def c(n):
return integral(f(x)*exp(-I*n*x), (x,-pi,pi))
for n in range(0,40):
show(html(f"$c_{{{n}}}={latex(c(n))}$"))Thu, 04 Jun 2020 20:16:45 +0200https://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/?answer=51762#post-id-51762Comment by matthuszagh for <p>You can define $f$ in terms of the Heaviside function and then integrate directly without any imports. Try this code:</p>
<pre><code>f(x) = heaviside(x-pi/2)
def c(n):
return integral(f(x)*exp(-I*n*x), (x,-pi,pi))
for n in range(0,40):
show(html(f"$c_{{{n}}}={latex(c(n))}$"))
</code></pre>
https://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/?comment=51764#post-id-51764This does the trick, thanks! However, I wasn't able to get your show(...) code to run, i kept getting syntax errors:
File "<ipython-input-23-b7fe66c04faf>", line 2
show(html(f"$c_{{{n}}}={latex(c(n))}$"))
^
SyntaxError: invalid syntax
(note the ^ lines up with the ending ")Thu, 04 Jun 2020 21:36:42 +0200https://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/?comment=51764#post-id-51764Comment by Juanjo for <p>You can define $f$ in terms of the Heaviside function and then integrate directly without any imports. Try this code:</p>
<pre><code>f(x) = heaviside(x-pi/2)
def c(n):
return integral(f(x)*exp(-I*n*x), (x,-pi,pi))
for n in range(0,40):
show(html(f"$c_{{{n}}}={latex(c(n))}$"))
</code></pre>
https://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/?comment=51765#post-id-51765The last line of code should work in a recent release of SageMath (9.0 or 9.1), as can be verified in this [SageMath Cell](https://sagecell.sagemath.org/?z=eJwtjUEKwyAQRfeeYghZzIihoXRVyAF6iiLJGIXUiLGtIN69BvrX771vMBNMYFl_3OEWxjwEd7mSWNjAjJ7uAtoip3f04HziNeoNTdMk54DDQ3qZSQFm1UwVHJEwe4QThqj9yjiq2_jvHHb_ok2vVuj6-VlK8bXWqWw6ccbzj2rfEf0AhZIt6A==&lang=sage&interacts=eJyLjgUAARUAuQ==). Are you still running version 8.9 or earlier? To check it, type `version()` and look at the ouput.Thu, 04 Jun 2020 22:10:20 +0200https://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/?comment=51765#post-id-51765Comment by matthuszagh for <p>You can define $f$ in terms of the Heaviside function and then integrate directly without any imports. Try this code:</p>
<pre><code>f(x) = heaviside(x-pi/2)
def c(n):
return integral(f(x)*exp(-I*n*x), (x,-pi,pi))
for n in range(0,40):
show(html(f"$c_{{{n}}}={latex(c(n))}$"))
</code></pre>
https://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/?comment=51767#post-id-51767Ah that would explain it, I'm running 8.9.Thu, 04 Jun 2020 22:40:34 +0200https://ask.sagemath.org/question/51748/how-to-define-a-function-that-is-piecewise-for-specific-independent-variable-values/?comment=51767#post-id-51767