# Plotting question

I am trying to plot Sage calculation listed below. I get the following error:

verbose 0 (3989: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 200 points. verbose 0 (3989: plot.py, generate_plot_points) Last error message: 'unable to simplify to float approximation'

What am I doing wrong?

Gather all for Full H off resonance

Ix=1/2*matrix(SR,2,2,[[0,1],[1,0]])
Iy=1/(2*I)*matrix(SR,2,2,[[0,1],[-1,0]])
Iz=1/2*matrix(SR,2,2,[[1,0],[0,-1]])
Al=vector(SR,[1,0])
Be=vector(SR,[0,1])

g = 26.7522128e7 # 1H Gamma rad/s/T
rf_field = 100 # B1 rf field in kHz
Brf = rf_field*1e3*2*pi/g*2 # B1 in T for Wnut/2pi= 100 kHz
Thetarf = pi/2 # angle between BO and Brf

Wnut = (1/2*g*Brf*sin(Thetarf))
t360 = 1/((1/2*g*Brf*sin(Thetarf))/(2*pi))# 360 pulse
tip = 180 # Tip angle
tp = tip/360*t360
Bo = 500e6*2*pi/g# T Spectrometer Field Strength
wref = g*Bo # spectrometer reference frequency for Protons, rad/S
Bo.n(),(wref/(2*pi)).simplify()/1e6;(Wnut/(2*pi)).simplify()/1e3,tp.simplify()*1e6

Ratio = var ('Ratio')# Wnut/O0, Ratio creates offset, 0=on res
Boo=(Wnut*Ratio+wref)/g
woo = g*Boo
O0=woo-wref

Phip = var('Phip') # Phip = phase of rf pulse
Hrfrf = (O0)*Iz + Wnut*(Ix*cos(Phip)+Iy*sin(Phip))
Pabrf=((abs(Be*exp(-i*Hrfrf(Phip=0)*tp)*Al))^2)

for Ratio in srange (-1,1,0.25,include_endpoint=True):
Pabrf(Ratio=Ratio).n(digits=5) #(pi)x on Alpha off resonance Full H

plot (Pabrf,(Ratio, -1,1))

edit retag close merge delete

looks like nmr to me

( 2011-01-05 17:41:52 +0200 )edit

two quick tips that don't address your actual problem - you can use "var('Ratio')" instead of "Ratio = var('Ratio')", and the loop in the second and third last lines is unnecessary if you want to use plot()

( 2011-01-05 18:51:30 +0200 )edit

Sort by ยป oldest newest most voted

I have been able to get most things working with:

list_plot([(r,Pabrf(Ratio=r).n()) for r in srange(-1, 1, 0.1, include_endpoint=True)], plotjoined=True, frame=true).show(ymin=0,axes_labels=('$\Omega _{0}/\omega _{nut}$',"$P _{a\Rightarrow b}$"))

1. How do I turn off or turn on the lines at the origin(o)?

2. How do I turn off the minor ticks?

3. How do I get the \rightarrow to display instead of the \Rightarrow? Only the \Rightarrow displays properly.

Thank you again for your help.

more

Not really sure about any of these. Have a look through the plotting documentation http://sagemath.org/doc/reference/sage/plot/plot.html . If you want lots of control over your graphs then it might be worth checking out matplotlib/pylab, which is included with Sage but has separate documentation. Personally I've found sage's plotting functions convenient for quick and dirty figures, but use matplotlib when I want finesse.

( 2011-01-07 01:53:47 +0200 )edit

'unable to simplify to float approximation'

Came here because I have a similar problem

Apparently the plotting command provided by Sage evaluate expressions by coercing to float, i.e. plot calls

float(Pabrf)

This fails on your expression because it's too complicated.

Can someone explain why Sage doesn't evaluate expressions numerically when plotting? What would be a straightforward why to do this?

more

One solution is to use list_plot instead of plot. plot takes a function, whereas list_plot accepts a list of (x,y) coordinates to plot. list_plot therefore gives you a bit more control and flexibility. If you replace your last three lines by:

Pabrfs= []
for r in srange (-1,1,0.25,include_endpoint=True):
#(pi)x on Alpha off resonance Full H
Pabrfs.append((r,Pabrf(Ratio=r).n(digits=5)))

list_plot (Pabrfs)


then it should work. Alternatively you can make this a bit shorter using a list comprehension:

#(pi)x on Alpha off resonance Full H
Pabrfs = [(r,Pabrf(Ratio=r).n()) for r in srange(-1,1,0.25,include_endpoint=True)]

list_plot(Pabrfs)

more

Try

def Pabrf:
return N((abs(Be*exp(-i*Hrfrf(Phip=0)*tp)*Al))^2)
plot(Pabrf)

more

you could also use a lambda function

( 2011-05-25 02:46:42 +0200 )edit

the first code in the question does not work for me with Ubuntu 18.04 notebook SageMath 9.1, but I noticed that if we ask for the graph of a function outside its domain of definition it causes a verbose and moreover the graph refuses to extend at the requested values

r=1
C_d(x)= -sqrt(r^2-x^2)
gf=plot(C_d,(x,-2,2),color='black') #produce verbose and do not span
#gf=plot(C_d,(x,-1,1),color='black') # no verbose

show(gf,aspect_ratio=1)

more

Thank you this works for me. I was able to simplify it further to:

list_plot([(r,Pabrf(Ratio=r).n()) for r in srange(-1, 1, 0.1, include_endpoint=True)], plotjoined=True)

more