* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
3.3 KiB
id, title, challengeType, videoUrl, dashedName
id | title | challengeType | videoUrl | dashedName |
---|---|---|---|---|
59880443fb36441083c6c20e | 欧拉方法 | 5 | euler-method |
--description--
欧拉方法在数值上近似具有给定初始值的一阶常微分方程(ODE)的解。它是解决初始值问题(IVP)的一种显式方法,如维基百科页面中所述 。
ODE必须以下列形式提供:
:: $ \ frac {dy(t)} {dt} = f(t,y(t))$
具有初始值
:: $ y(t_0)= y_0 $
为了得到数值解,我们用有限差分近似替换LHS上的导数:
:: $ \ frac {dy(t)} {dt} \ approx \ frac {y(t + h)-y(t)} {h} $
然后解决$ y(t + h)$:
:: $ y(t + h)\ about y(t)+ h \,\ frac {dy(t)} {dt} $
这是一样的
:: $ y(t + h)\ about y(t)+ h \,f(t,y(t))$
然后迭代解决方案规则是:
:: $ y_ {n + 1} = y_n + h \,f(t_n,y_n)$
其中$ h $是步长,是解决方案准确性最相关的参数。较小的步长会提高精度,但也会增加计算成本,因此必须根据手头的问题手工挑选。
示例:牛顿冷却法
Newton的冷却定律描述了在温度$ T_R $的环境中初始温度$ T(t_0)= T_0 $的对象如何冷却:
:: $ \ frac {dT(t)} {dt} = -k \,\ Delta T $
要么
:: $ \ frac {dT(t)} {dt} = -k \,(T(t) - T_R)$
它表示物体的冷却速率$ \ frac {dT(t)} {dt} $与周围环境的当前温差$ \ Delta T =(T(t) - T_R)$成正比 。
我们将与数值近似进行比较的解析解是
:: $ T(t)= T_R +(T_0 - T_R)\; Ë^ { -克拉} $
任务:实现欧拉方法的一个例程,然后用它来解决牛顿冷却定律的给定例子,它有三种不同的步长:
:: * 2秒
:: * 5秒和
:: * 10秒
并与分析解决方案进行比较。
初始值::: *初始温度$ T_0 $应为100°C
:: *室温$ T_R $应为20°C
:: *冷却常数$ k $应为0.07
:: *计算的时间间隔应为0s──►100s
--hints--
eulersMethod
是一个函数。
assert(typeof eulersMethod === 'function');
eulersMethod(0, 100, 100, 10)
应该返回一个数字。
assert(typeof eulersMethod(0, 100, 100, 10) === 'number');
eulersMethod(0, 100, 100, 10)
应返回20.0424631833732。
assert.equal(eulersMethod(0, 100, 100, 2), 20.0424631833732);
eulersMethod(0, 100, 100, 10)
应返回20.01449963666907。
assert.equal(eulersMethod(0, 100, 100, 5), 20.01449963666907);
eulersMethod(0, 100, 100, 10)
应返回20.000472392。
assert.equal(eulersMethod(0, 100, 100, 10), 20.000472392);
--seed--
--seed-contents--
function eulersMethod(x1, y1, x2, h) {
}
--solutions--
function eulersMethod(x1, y1, x2, h) {
let x = x1;
let y = y1;
while ((x < x2 && x1 < x2) || (x > x2 && x1 > x2)) {
y += h * (-0.07 * (y - 20));
x += h;
}
return y;
}