Feeds:
Posts

## Playing around with Sage(math)

I was playing with Sage(math) because I wanted to manage (a bit) the presentation of the plots. Here is what I found out! (most of it is in Sage help)

## Color of Plots.

For changing the color of the plot, use the option color,

`sage: plot(sin, -3, 3, color='red')`

and you’ll get

Red Sine Function

Nice! but not enough.

One can draw recurrent plots by using a for command,

```sage: for n in range(1,6):
... plot(sin(n*x), (x, -3, 3), color=hue(n/5.0))```

but this code gives 5 different plots, if you want only one try this

```sage: p = plot( sin, -3, 3, color=hue(1/5.0))
sage: for n in range(2,6):
... p += plot(sin(n*x), (x, -3, 3), color=hue(n/5.0))
...
sage: p
```

and the result is

Plot of multiple sine functions in colors

The command for adding text to plots is text (very intuitive, Isn’t it?), and the parameters are, the actual text you’d like to insert between single quote marks, the coordinates, and any other characteristic… as color.

Additionally, the text environment accepts mathematical LaTeX inputs by using the dollar signs.

```sage: p = plot(sin(x/10.0), -10, 10, color=hue(1/6.0))
+ text('\$n\$=1', (12,1), color=hue(1/6.0))
sage: for n in range(2,6):
... p += plot(sin(n*x/10.0), -10, 10, color=hue(n/6.0))
+ text('\$n\$=%s'%n, (12,1/n), color=hue(n/6.0))
sage: p```

and get,

Note that the text may include `running` parameters, in the above example the running parameter is $n$, and is called in the text environment as `%s` and later one explain that the value to insert is $n$ via %n.

NOTE 2: The coordinates of the text might be also changing values.

NOTE 3: The fontsize and horizontal_alignment options are available, for example

```sage: text('\$E\$=%s'%Energy, (5.2, 1.2),
horizontal_alignment='right', fontsize=15, color='black')```

## Nice Example.

Try this!

```reset()
def Harm_Osc(n, x):
return sqrt(1/(2^n*factorial(n)))/(pi)^(0.25)*e^(-x^2/2)*hermite(n,x)
p = plot(x^2/2, (x, -3.2, 3.2), color='black')
+ text('\$x^2/2\$', (3.9, 5), color='black', fontsize=13)
for n in range(5):
p += plot(Harm_Osc(n, x)/2 + n + 0.5, (x, -5, 5), color=hue(n/5.0), fill=n + 0.5)
+ text('\$\Psi_%s\$' %n, (5.2, n+0.5), color=hue(n/5.0), fontsize=13)
show(p, axes_labels=['\$x\$','\$E\$'])```

😉

You’ll get this,

Wave functions for the quantum harmonic oscillator

This example got colors, text, running text, running color, axes labeling, LaTeX text, fill plot… very complete!

### 6 Responses

1. That is a very nice graph of the harmonic oscilator. I’ll copy it if you don’t mind.

• Glad you liked it! I don’t mind if you use it…

2. really love the quantum harmonic oscillator! that looks great.
this will be my standard example why sage is the tool to use from now on.

3. Awesome example! I am going to download and play with SAGE asap! Thanks..

4. […] Playing around with Sage(math) February 20104 comments 5 […]

5. Hi thanks for showing this quantum simple harmonic oscillator code – beautiful output!