3

I have following python code in Sage:

%python
import math

T = 2.0 * math.pi
a = 1.0 / 4.0

def SumT(t, n):
    global T
    global a

    sum = 0

    if n < 0:
        return 0
    else:
        for i in range (1, n+1):
            sum = sum + ( (1.0 / (2.0 * i - 1.0)) * math.sin((2.0 * (2.0 * i - 1.0) * math.pi * t) / T) )
        return (4.0 / math.pi) * sum

t = var('t')
p = plot(SumT(t, 1), (t, 0, 1))
show(p)

Calling a function plot(SumT(t, 1), (t, 0, 1)) gives following error:

unable to simplify to float approximation

I've already looked for same problems in other threads and posts: seems that this error should be fixed with formula simplification, but I don't know how to simplify it.

The formula itself:

$$ \begin{eqnarray*}S(t;n) & = & \frac{4}{\pi}\sum_{i=1}^{n}\frac{1}{2i-1}\sin\frac{2(2i-1)\pi t}{T}\end{eqnarray*} $$

Thanks in advance!

UPDATE The problem is fixed if use pure Sage without python math import:

T = 2.0 * pi
a = 1.0 / 4.0

def SumT(t, n):
    global T
    global a

    sum = 0

    if n < 0:
        return 0
    else:
        for i in range (1, n+1):
            sum = sum + ( (1.0 / (2.0 * i - 1.0)) * sin((2.0 * (2.0 * i - 1.0) * pi * t) / T) )
        return (4.0 / pi) * sum

t = var('t')
show(plot(SumT(t, 1), (t, 0, 1)))
irina
  • 61

3 Answers3

1

The problem is with

t = var('t')

I don't know what you were getting at but t needs to be a number to produce a result.

Additionally, I had to replace pi to math.pi. And I personally wouldn't use global variables. I rewrote the code (I think your calculation is correct though!):

%python
import math

def SumT(t, n):
    T = 2.0 * math.pi
    a = 1.0 / 4.0
    sum = 0

    if n < 0:
        return 0
    else:
        for i in range (1, n+1):
            sum = sum + ((1.0 / (2.0 * i - 1.0)) * math.sin((2.0 * (2.0 * i - 1.0) * math.pi * t) / T) )
        return (4.0 / math.pi) * sum

p = list_plot([(r,SumT(t=r, n=10).n()) for r in srange(-1, 1, 0.1, include_endpoint=True)], plotjoined=True)
show(p)
  • Sorry, I didn't make myself clear enough: the final goal is to plot this function (I also made these changes in the post): p = plot(SumT(t, 1), (t, 0, 1)) show(p) – irina Feb 19 '16 at 16:38
  • I've never used sage python so I'm out of my depth. But what I gathered is sage "evaluates expressions by coercing to float I.E. plot calls 'float(SumT)'". Or the expression is too complicated for it to evaluate. I was able to get it to somewhat work with list_plot. – user3798776 Feb 19 '16 at 17:15
  • The problem is fixed if use pure Sage, without python math import. But thank you for your contribution! – irina Feb 19 '16 at 17:25
1

The problem is fixed if use pure Sage without python math import:

T = 2.0 * pi
a = 1.0 / 4.0

def SumT(t, n):
    global T
    global a

    sum = 0

    if n < 0:
        return 0
    else:
        for i in range (1, n+1):
            sum = sum + ( (1.0 / (2.0 * i - 1.0)) * sin((2.0 * (2.0 * i - 1.0) * pi * t) / T) )
        return (4.0 / pi) * sum

t = var('t')
show(plot(SumT(t, 1), (t, 0, 1)))
irina
  • 61
0

The problem here is somewhat deeper and I think it's a bug, rather than a feature. I have tried the following in a Sage IPython notebook at CoCalc:

Cell 1:

t = var('t')
plot(cos(t),(t, -pi, pi)  # works

Cell 2:

import math
plot(math.cos(t),(t, -pi, pi)  # raises TypeError("unable to simplify to float approximation")

Cell 3:

plot(math.cos, (x, -pi, pi)) # works

Cell 4:

plot(math.cos, (-pi,pi)) # this also works

Cell 5:

plot(math.cos(x), (x, -pi, pi)) # raises TypeError like in Cell 2

Basically if you want to plot a function using a named argument (x, t, whatever), then you must use the "SAGE version" of that function. The standard Python functions in the math package can be plotted only if the name of the function is passed to plot.

Or so I think. In any case this is rather confusing.