Ask Your Question
0

How to make a function of 'fname' that produce a file named 'fname'

asked 2017-02-18 19:38:48 +0100

Laurent B gravatar image

Hi all,

Typing a correction on some recurrent sequences $u_{n+1}=f(u_n)$ I use sage to build a nice plot illustrating the behavior of the sequence depending on the initial data. I took a nice function from this awesome book.

def escargot(f,x,u0,n,xmin,xmax,file):
    u = u0
    P = plot(x, x, xmin, xmax, color='gray')
    for i in range(n):     
        P+= line([[u,u],[u,f(u)],[f(u),f(u)]], color ='red')
        u = f(u)
    P += f.plot(x, xmin, xmax, color='blue') 
    P.save('/home/myfolder/f1a.png')

So everyhting work perfectly but, since there is a lot of examples to produce, I would like to make the function to have an extra argument say filename so that now the file so that now the plot is saved in /home/myfolder/filename.png, but I have no idea how sage manipulate string. Help would be welcome !

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-02-18 20:21:22 +0100

mforets gravatar image

Sage manipulates strings as Python does, so in

def escargot(f,x,u0,n,xmin,xmax,filename):
    ...
    P.save('/home/myfolder/' + filename)

the strings '/home/myfolder/' and that in filename will be concatenated.

(more useful operations for instance here).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-02-18 19:38:48 +0100

Seen: 216 times

Last updated: Feb 18 '17