4

我试图通过在 Matlab 中绘制给定的势函数来找到三体系统的 5 个拉格朗日点。唯一的问题是我不太擅长编程。任何帮助将不胜感激。我想知道的是如何让这段代码给我一个像样的等高线图:

function Lagrange(a)


x = ( -10000: 1 : 10000);
y = ( -10000: 1 : 10000);
Potential = zeros(length(x));

for i = 1: length(x)
    for j = 1 : length(y)

    Potential(i,j) =  ( 1 - a ) / sqrt( ( x(i) - a )^2 + y(j)^2)  + a / sqrt( ( x(i) + 1    - a )^2 + y(j)^2 ) + ( x(i)^2 + y(j)^2 ) / 2 ;

    end

    j = 1;
end

contour(Potential);

xlabel('X axis'); 
ylabel('Y axis'); 
zlabel('Z axis');
4

2 回答 2

4

三体问题的设置方式,距离坐标归一化为a。因此,您应该选择x并且y更像:

x = linspace(-1.5, 1.5, 1000);
y = linspace(-1.5, 1.5, 1000);

对于等高线图,您可以使用meshgrid,它可以让您避免 for 循环并更轻松地绘制:

[X, Y] = meshgrid(x, y);

对于势,尝试绘制 2U - 这称为Jacobi 常数,信息量更大。

U = (1-a)./sqrt(Y.^2 + (X + a).^2) + ...
     a./sqrt(Y.^2 + (X + a - 1).^2) + ...
     0.5*(Y.^2 + X.^2);
Z = 2*U;

最后,你需要轮廓。你会想为你的情节调整这些,但我使用了类似的东西

c = [2.988:0.05:3.1, 3.2:0.2:5];

对于地月系统。现在,要绘制,只需使用contourf如下:

figure
contourf(X, Y, Z, c)
colorbar

另请注意,您可以使用运动方程解析拉格朗日点本身 - 您也可以考虑绘制这些点,因为等高线只会会聚在点上,但永远不会碰到它们。

于 2012-12-18T16:11:43.550 回答
1

Recommendations

  • Try using vector operations (rather than for loops), they're much faster. This is done by adding a '.' in front of the operator: * becomes .*
  • The matrix sizes suggested above may be a bit large, you'll likely run out of memory. Try first with a smaller step size, then increase resolution.
  • Your 'Z' axis in the Matlab contour() plot will be the color of the lines, so there is nothing to label. Try colorbar instead.
  • Use ... to continue long statements on multiple lines.
  • By convention, words starting with a capital letter are reserved for class definitions.

Suggested Code

function lagrange(a)
  n = 100000;
  stepsize = 100;
  [x,y] = ndgrid(-n:stepSize:n, -n:stepSize:n)
  potential = ( 1 - a ) ./ sqrt( ( x - a ).^2 + y.^2)  + ...
    a ./ sqrt( ( x + 1 - a ).^2 + y.^2 ) + ( x.^2 + y.^2 ) ./ 2 ;

  contour(x,y,potential)
  xlabel('X axis')
  ylabel('Y axis')
  colorbar
end
于 2012-12-17T22:54:26.070 回答