擅长:python、mysql、java
<p>正如注释中所建议的,您可以创建两个列表来存储<code>x</code>和<code>y</code>值,并在<code>while</code>循环后打印:</p>
<pre><code>import math
import numpy as np
import matplotlib.pyplot as plt
h=0.01;
ti=0;
x=0;
n=0;
y=5;
def f(x,y):
return 1.3*math.exp(-x)-2*y
xs = [x] # <<<
ys = [y] # <<<
while x < 10:
k1=f(x,5);
k2=f(x+h/2,y+(h/2)* k1);
k3=f(x+h/2,y+(h/2)* k2);
k4=f(x+h,y+h*k3);
y=y+h/6*(k1+2*(k2+k3)+k4);
x=x+h;
xs.append(x) # <<<
ys.append(y) # <<<
plt.plot(xs,ys);
</code></pre>
<p><a href="https://i.stack.imgur.com/Ss4Tr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ss4Tr.png" alt="enter image description here"/></a></p>