# 计算方法：Euler法及其改进

x0 = 1; p0 = 2; h = 0.05;

tlist = 0 : h : 50;

xlist = zeros(1, length(tlist));

plist = zeros(1, length(tlist));

xlist(1) = x0; plist(1) = p0 ;

for s = 2 : length(tlist)

xlist(s) = xlist(s-1) + h * plist(s-1);

plist(s) = plist(s-1) - h* xlist(s-1);

end

h1 = figure;

plot(tlist, xlist, tlist, plist)

xlabel('t'); ylabel('x/p')

h2 = figure;

plot(xlist, plist)

xlabel('x'); ylabel('p')

Euler法很差，但是其改进的余地很大，改进也很容易。比如我们有Heun方法：

x0 = 1; p0 = 2; h = 0.1;

tlist = 0 : h : 50;

xlist = zeros(1, length(tlist));

plist = zeros(1, length(tlist));

xlist(1) = x0;plist(1) = p0 ;

for s = 2 : length(tlist)

tempx = xlist(s-1) + h * plist(s-1);

tempp = plist(s-1) - h* xlist(s-1);

xlist(s) = xlist(s-1) + 0.5 * h * (plist(s-1) + tempp);

plist(s) = plist(s-1) - 0.5* h * (xlist(s-1) + tempx);

end

h1 = figure;

plot(tlist, xlist, tlist, plist)

xlabel('t');ylabel('x/p')

h2 = figure;

plot(xlist, plist)

xlabel('x');ylabel('p')

http://blog.sciencenet.cn/blog-100379-1094683.html

## 全部精选博文导读

GMT+8, 2018-10-20 10:46