简体   繁体   English

行星物理学是不稳定的

[英]Planetary physics are wonky

So in my physics class we're doing stuff with Kepler's law and other such equations.所以在我的物理课上,我们正在用开普勒定律和其他类似的方程做一些事情。 I figured I'd understand it better if i could make a model of it.我想如果我能制作一个模型,我会更好地理解它。 and I've got it running... sorta.我已经让它运行了......有点。 The motion of the planets, or moons in this instance, are supposed to be ellipses and they just aren't anywhere close.在这种情况下,行星或卫星的运动应该是椭圆形的,它们只是离得很近。

I'm using actual masses, distances, and speeds for the planetoids so I'm not sure whats going wrong我使用的是小行星的实际质量、距离和速度,所以我不确定出了什么问题

 let G = 6.67408*Math.pow(10,-11) let scl = 4000 let mars; let phobos; let deimos; function setup() { createCanvas(600, 600) mars = new Planet(0, 0, 0, 0, 6.4171*pow(10,24)) phobos = new Planet(-9234420, 0, 0, -2138 , 1.0659*pow(10,16)) deimos = new Planet(23463200, 0, 0, 1351.3, 1.4762*pow(10,15)) background(0) } function draw() { translate(width/2-mars.pos.x/scl, height/2-mars.pos.y/scl) background(0, 1) for(let i=0;i<10;i++){ mars.show() phobos.show() deimos.show() mars.update() phobos.update() deimos.update() grav(phobos, mars) grav(mars, deimos) grav(phobos, deimos) if(deimos.pos.dist(mars.pos)/scl>(width/2)){ scl = deimos.pos.dist(phobos.pos)/(0.99*width/2); background(0) } } } function force(p1, p2){ d = p1.pos.dist(p2.pos) f = G*(p1.mass*p2.mass)/(d*d) return f } function grav(p1,p2){ d1 = p2.pos.copy().sub(p1.pos).normalize() d2 = p1.pos.copy().sub(p2.pos).normalize() f = force(p1,p2) p1.acc.add(d1.mult(force/p1.mass)) p2.acc.add(d2.mult(force/p2.mass)) } function Planet(x, y, vx, vy, mass){ this.mass = mass; this.pos = createVector(x, y) this.vel = createVector(vx, vy) this.acc = createVector(0, 0) this.show = function(){ stroke(255) strokeWeight(width/100) point(this.pos.x/scl, this.pos.y/scl) } this.update = function(){ this.pos = this.pos.add(this.vel); this.vel = this.vel.add(this.acc) this.acc = this.acc.mult(0) } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.js"></script>

as i said one would think that they would have elliptical orbits yet they're not.正如我所说,人们会认为它们会有椭圆轨道,但实际上并非如此。 so I'm just at a loss.所以我很茫然。

To make the physics work in this code we need to make a couple of corrections.为了使此代码中的物理工作,我们需要进行一些更正。

The grav function needs to calculate force and use it to update acceleration, however, there is a mistake that causes the force function to be used instead of the calculated force and the original code multiplies by NaN instead of force/mass as the force function is not a number. grav函数需要计算力并用它来更新加速度,但是有一个错误导致使用力函数而不是计算力,原始代码乘以NaN而不是力/质量,因为force函数是不是一个数字。

function grav(p1,p2){
  d1 = p2.pos.copy().sub(p1.pos).normalize()
  d2 = p1.pos.copy().sub(p2.pos).normalize()

  f = force(p1,p2) // we need to store the calculated force
  p1.acc.add(d1.mult(f/(p1.mass))) // and use it here
  p2.acc.add(d2.mult(f/(p2.mass))) // and here
}

The code also needs to call update after each call to the grav function:代码还需要在每次调用grav函数后调用update

grav(phobos, mars);
mars.update();
phobos.update();

grav(mars, deimos);
mars.update();
deimos.update();

grav(phobos, deimos);
phobos.update();
deimos.update();

 let G = 6.67408*Math.pow(10,-11) let scl = 4000 let mars; let phobos; let deimos; function setup() { createCanvas(600, 600); mars = new Planet(0, 0, 0, 0, 6.4171*pow(10,24)); phobos = new Planet(-9234420, 0, 0, -2138 , 1.0659*pow(10,16)); deimos = new Planet(23463200, 0, 0, 1351.3, 1.4762*pow(10,15)); background(0); } function draw() { translate(width/2-mars.pos.x/scl, height/2-mars.pos.y/scl); background(0, 1); for(let i=0;i<10;i++){ mars.show(); phobos.show(); deimos.show(); grav(phobos, mars); mars.update(); phobos.update(); grav(mars, deimos); mars.update(); deimos.update(); grav(phobos, deimos); phobos.update(); deimos.update(); if(deimos.pos.dist(mars.pos)/scl>(width/2)){ scl = deimos.pos.dist(phobos.pos)/(0.99*width/2); background(0) } } } function force(p1, p2){ d = p1.pos.dist(p2.pos) f = G*(p1.mass*p2.mass)/(d*d) return f } function grav(p1,p2){ let d1 = p2.pos.copy().sub(p1.pos).normalize() let d2 = p1.pos.copy().sub(p2.pos).normalize() let f = force(p1,p2) p1.acc.add(d1.mult(f/(p1.mass))) p2.acc.add(d2.mult(f/(p2.mass))) } function Planet(x, y, vx, vy, mass){ this.mass = mass; this.pos = createVector(x, y) this.vel = createVector(vx, vy) this.acc = createVector(0, 0) this.show = function(){ stroke(255) strokeWeight(width/100) point(this.pos.x/scl, this.pos.y/scl) } this.update = function(){ this.pos = this.pos.add(this.vel); this.vel = this.vel.add(this.acc) this.acc = this.acc.mult(0) } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script>

With these corrections we get roughly elliptical orbits, however, after a few revolutions the moons drift away from Mars.通过这些修正,我们得到大致椭圆轨道,然而,经过几次旋转后,卫星会远离火星。 This is not surprising as all of the initial conditions and the value of G are approximations.这并不奇怪,因为所有初始条件和 G 的值都是近似值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM