使用 Python3 在 Sage 中绘制欧拉方法的图形

Plotting a Graph for Euler's Method in Sage using Python3

我一直收到错误,我认为它来自 yy=function ("yy", x) 行,但我不确定如何修复它。甚至肯定这是我的问题。

我的编码(连同我要回答的问题)如下:

print("1.  Using Euler’s method, compute an approximate value for y(1) ")
print("    for the differential equation")
print("                y'=-y")
print("                y(0)=1.")
print("    using n=100 steps.Then graph its true solution and the table")
print("    obtained from Euler’s Method.")
print()

def PrintEuler(a,b,n,f, y0):
    #copy here its implementation!!!
    #set up the initial values
    h = (b-a)/n 
    xi = a
    yi = y0
    #print the first line of the table 
    print ("y("+str(a.n()) +") = ", y0.n())

    for i in [1,2,..,n]:
        #compute the next value of yi
        yi= yi+h*f(xi,yi)
        #prepare for the next x
        xi = xi + h
        #print the next line of the table
        print ("y("+str(xi.n()) +") = ", yi.n())
#set up the problem
a = 0; b = 1; n = 100
f(x,y) = -y
y0 = 1
#call the function for Euler’s Method
PrintEuler(a,b,n,f, y0)

def PlotEuler(a,b,n,f, y0):
    #set up the initial values
    h = (b-a)/n 
    xi = a
    yi = y0
    #print the first line of the table 
    EulerTable= [(xi, y0)]

    for i in [1,2,..,n]:
        #compute the next value of yi 
        yi= yi+h*f(xi,yi)
        #prepare for the next x 
        xi = xi + h

        #compute the next node, add it to the table 
        EulerTable.append((xi.n(),yi.n()))
    #plot the points obtained from Euler’s Method
    p = list_plot (EulerTable, color= "red", size = 70)

    #compute the actual solution 
    yy = function(yy, x)
    ActualSol(x) = desolve( diff(yy,x) == f(x,yy), yy, ics=[a,y0])

    #plot the actual solution
    p+=plot(ActualSol(x), x, a, a+n*h ,color = "blue", thickness = 2)
    p.show()

#set up the problem
a = 0; b = 1; n = 100
f(x,y) = -y
y0 = 1
#call the function for Euler’s Method
PlotEuler(a,b,n,f, y0)

这是我收到的错误消息:

Error in lines 26-39
Traceback (most recent call last):
  File "/cocalc/lib/python3.8/site-packages/smc_sagews/sage_server.py", line 1231, in execute
    compile(block + '\n',
  File "<string>", line 11
    yy = function(yy, x)
                     ^
SyntaxError: invalid syntax

任何帮助或见解将不胜感激!

更改这些行

    yy = function(yy x)
    ActualSol(x) = desolve( diff(yy,x) == f(x,yy), yy, ics=[a,y0])

    yy = function("yy")
    ActualSol(x) = desolve(diff(yy(x),x) == f(x,yy(x)), yy(x), ics=[a,y0])