Newtons-Raphsons方法很容易在Mathematica中实现,但在Matlab中似乎有点困难.如果我可以将函数传递给函数以及如何将派生函数用作函数,我不会得到.
newtonRaphson[f_, n_, guess_] :=
If[n == 0, guess, newtonRaphson[f, n - 1, guess - f[guess]/f'[guess]]]
newtonRaphsonOptimize[f_, n_, guess_] :=
If[n == 0, guess,
newtonRaphsonOptimize[f, n - 1, guess - f'[guess]/f''[guess]]]
Run Code Online (Sandbox Code Playgroud)
似乎你既不能导出函数句柄也不能导出文件中定义的函数,但我可能错了.
我有这个代码来解决牛顿的方法.但它给出了零分割错误.我无法弄清楚出了什么问题.谢谢.
import copy
tlist = [0.0, 0.12, 0.16, 0.2, 0.31, 0.34] # list of start time for the phonemes
w = w1 = w2 = w3 = w = 5
def time() :
frame = 0.04
for i, start_time in enumerate(tlist) :
end_time = tlist[i]
frame = frame * (i + 1)
poly = poly_coeff(start_time, end_time, frame)
Newton(poly)
def poly_coeff(stime, etime, f) :
"""The equation is k6 * u^3 + k5 * u^2 + k4 * u + k0 = …Run Code Online (Sandbox Code Playgroud) 在找到每个单根之前会进行多少次递归?还有,哪些是根?
这是我的代码:
e=0.000001;
f1=@(x) 14.*x.*exp(x-2)-12.*exp(x-2)-7.*x.^3+20.*x.^2-26.*x+12;
a=0;
c=3;
while abs(c-a)>e
b=(c+a)/2;
if f1(a)*f1(b)<0
c=b;
else
a=b;
end
disp(b);
end
Run Code Online (Sandbox Code Playgroud) 我有以下函数用于使用Newton-Raphson方法计算数字的平方根.
double get_dx(double y, double x)
{
return (x*x - y)/(2*x);
}
double my_sqrt(double y, double initial)
{
double tolerance = 1.0E-6;
double x = initial;
double dx;
int iteration_count = 0;
while ( iteration_count < 100 &&
fabs(dx = get_dx(y, x)) > tolerance )
{
x -= dx;
++iteration_count;
if ( iteration_count > 90 )
{
printf("Iteration number: %d, dx: %lf\n", iteration_count, dx);
}
}
if ( iteration_count < 100 )
{
printf("Got the result to converge in %d …Run Code Online (Sandbox Code Playgroud) 我试图以自己的方式编码,但发现我得到了错误的答案.
我看过这个页面.并尝试启动该过程:

F(X)= X ^ 2-E
数学:

所以有我的代码:
def sqrtRootNR(num, count, epsl):
"""
for test
"""
num = float(num)
guess = num / 2.0
diff = guess ** 2.0 - num
_cnt = 0
while abs(diff) > epsl and _cnt < count:
guess = guess - (guess ** 2.0 + epsl) / (guess * 2.0)
diff = guess ** 2.0 - num
_cnt = _cnt +1
print guess, _cnt
sqrtRootNR(2, 100, 0.0001)
Run Code Online (Sandbox Code Playgroud)
但是,我得到了错误的答案.
该函数的输出是:
D:\ poc> python sq.py …
新的一天新的问题...
我按照埃里克(Eric)所说的做,并为Acceleration创建了一种方法!
看起来像这样:
static Vector2 AccelerationOfTheMoon(Vector2 position)
{
double Mm = 7.349 * Math.Pow(10, 22);
double MagnitudeOfForce()
{
double MagF = position.LengthSquared();
return MagF;
}
Vector2 ForceAsVector()
{
return position.NegativeUnitVector() * MagnitudeOfForce();
}
Vector2 acceleration = ForceAsVector() / Mm;
}
Run Code Online (Sandbox Code Playgroud)
以我的方式看,我的名为AccelerationOfTheMoon的方法会收到带有位置的向量2。现在,我想使用此向量,因此我创建了另一个方法,该方法应将我的Magnitude作为标量(双精度)返回。为此,我创建了一种方法来计算向量的幅度并将其平方。当我在ForceAsVector-Method中调用MagnitudeOfForce-Method时,我正在尝试将负单位矢量与标量相乘,并将其作为Vector2返回。
也许我在这里做得很糟糕,但是我正在努力了解Eric所做的一切,仍然需要您的帮助!
谢谢。
c# vector newtons-method numerical-methods orbital-mechanics
我最近在博客上看到了这段代码 - 它来自Quake3引擎.它意味着使用Newton-Rhapson方法快速计算平方根.
float InvSqrt (float x){
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}
Run Code Online (Sandbox Code Playgroud)
这样做的原因是什么int i = *(int*)&x;?这样做int i = (int) x;,而不是给出一个完全不同的结果.