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.Mon, 17 Oct 2011 00:06:57 +0200Plotting cos(x+2)cos(x)-cos(x+1)²https://ask.sagemath.org/question/8335/plotting-cosx2cosx-cosx12/Hello.
Why does sage fail to plot this function?
> plot(cos(x+2)*cos(x)-cos(x+1)*cos(x+1),(x,-pi,pi))
Returns
> IndexError: index out of bounds
Sun, 16 Oct 2011 11:47:57 +0200https://ask.sagemath.org/question/8335/plotting-cosx2cosx-cosx12/Answer by John Palmieri for <p>Hello.</p>
<p>Why does sage fail to plot this function?</p>
<blockquote>
<p>plot(cos(x+2)<em>cos(x)-cos(x+1)</em>cos(x+1),(x,-pi,pi))</p>
</blockquote>
<p>Returns </p>
<blockquote>
<p>IndexError: index out of bounds</p>
</blockquote>
https://ask.sagemath.org/question/8335/plotting-cosx2cosx-cosx12/?answer=12764#post-id-12764In general, you need "*" to indicate multiplication: replace "cos(x+2) cos(x)" with "cos(x+2) * cos(x)", for example. In this particular example, there seems to be another issue, and it might be a bug. Try replacing "2" with "2.01" and it should plot okay.Sun, 16 Oct 2011 12:44:58 +0200https://ask.sagemath.org/question/8335/plotting-cosx2cosx-cosx12/?answer=12764#post-id-12764Answer by Xaver for <p>Hello.</p>
<p>Why does sage fail to plot this function?</p>
<blockquote>
<p>plot(cos(x+2)<em>cos(x)-cos(x+1)</em>cos(x+1),(x,-pi,pi))</p>
</blockquote>
<p>Returns </p>
<blockquote>
<p>IndexError: index out of bounds</p>
</blockquote>
https://ask.sagemath.org/question/8335/plotting-cosx2cosx-cosx12/?answer=12765#post-id-12765There are two points to your question:
1)
cos(x+2)*cos(x)-cos(x+1)*cos(x+1)=-sin(1)^2 #i.e. a constant
(Actually one of the experts should kick in and tell us what to do to make sage simplify your expression to that. I am frequently having problems to force sage to simplify even the most trivial expressions. A tutorial on that would be nice...)
2) sage uses matplotlib to do the plotting of your expression. The automatic selection of a 'proper' y-axis extent of that can cause problems when the actual y-extent of your data is identically zero. So, do something like:
plot(cos(x+2)*cos(x)-cos(x+1)*cos(x+1),(-pi,pi),ymin=-sin(1)^2*1.1,ymax=-sin(1)^2*.9)
and you'll get: ... a nice straight lineSun, 16 Oct 2011 12:50:47 +0200https://ask.sagemath.org/question/8335/plotting-cosx2cosx-cosx12/?answer=12765#post-id-12765Comment by John Palmieri for <p>There are two points to your question:
1)</p>
<pre><code>cos(x+2)*cos(x)-cos(x+1)*cos(x+1)=-sin(1)^2 #i.e. a constant
</code></pre>
<p>(Actually one of the experts should kick in and tell us what to do to make sage simplify your expression to that. I am frequently having problems to force sage to simplify even the most trivial expressions. A tutorial on that would be nice...)</p>
<p>2) sage uses matplotlib to do the plotting of your expression. The automatic selection of a 'proper' y-axis extent of that can cause problems when the actual y-extent of your data is identically zero. So, do something like:</p>
<pre><code>plot(cos(x+2)*cos(x)-cos(x+1)*cos(x+1),(-pi,pi),ymin=-sin(1)^2*1.1,ymax=-sin(1)^2*.9)
</code></pre>
<p>and you'll get: ... a nice straight line</p>
https://ask.sagemath.org/question/8335/plotting-cosx2cosx-cosx12/?comment=21119#post-id-21119For your first point, try `f = cos(x+2)*cos(x)-cos(x+1)*cos(x+1)` and then `f.trig_reduce()`.Mon, 17 Oct 2011 00:06:57 +0200https://ask.sagemath.org/question/8335/plotting-cosx2cosx-cosx12/?comment=21119#post-id-21119