我运行一个简单的脚本来估计函数的根.一切都很好,算法的每次迭代打印出当前的x和f(x),但是当脚本完成并设置x的最终估计值作为函数的输出时,返回值并舍入到3个小数位. .
while k < maxit
k = k + 1;
dx = b - a;
xm = a + 0.5*dx; % Minimize roundoff in computing the midpoint
fm = feval(fun, xm, diameter, roughness, reynolds);
fprintf('%4d %12.20e %12.4e\n',k,xm,fm);
if (abs(fm)/fref < feps) | (abs(dx)/xref < xeps) % True when root is found
r = xm;
return;
end
Run Code Online (Sandbox Code Playgroud)
这是输出的尾部位:
k xm fm
45 6.77444446476613980000e-003 1.3891e-012
46 6.77444446478035060000e-003 -1.3380e-011
47 6.77444446477324520000e-003 -5.9952e-012
48 6.77444446476969250000e-003 -2.3022e-012
49 6.77444446476791610000e-003 -4.5830e-013
ans =
0.0068
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它的输出四舍五入......我如何防止这种情况?
我正在努力完成一个数学问题,它使用Newton的猜测和检查方法来近似数字的平方根.用户应该输入一个数字,该数字的初始猜测,以及他们想要在返回之前检查他们的答案的次数.为了让事情变得更容易并且了解Python(我几个月前才开始学习这门语言),我把它分解成了许多小函数; 但现在的问题是,我无法调用每个函数并传递数字.
这是我的代码,有帮助的注释(每个函数按使用顺序):
# This program approximates the square root of a number (entered by the user)
# using Newton's method (guess-and-check). I started with one long function,
# but after research, have attempted to apply smaller functions on top of each
# other.
# * NEED TO: call functions properly; implement a counting loop so the
# goodGuess function can only be accessed the certain # of times the user
# specifies. Even if the - .001 range isn't …Run Code Online (Sandbox Code Playgroud) 我从http://blog.shay.co/newtons-method/中提取了此代码:
//a - the number to square root
//times - the number of iterations
public double Sqrt(double a, int times)
{
if (a < 0)
throw new Exception("Can not sqrt a negative number");
double x = 1;
while (times-- > 0)
x = x / 2 + a / (2 * x);
return x;
}
Run Code Online (Sandbox Code Playgroud)
对于数字的迭代次数(如果存在),有什么好的经验法则? (例如,"使用n/2次迭代".)
我正在尝试评估一个^ n,其中a和n是有理数.我不想使用任何预定义的功能,如sqrt()或pow()
所以我试图使用牛顿方法来使用这种方法得到近似解:
3 ^ 0.2 = 3 ^(1/5),因此如果x = 3 ^ 0.2,则x ^ 5 = 3.
解决这个问题的最佳方法(没有计算器,但仍然使用基本的算术运算)可能是使用"牛顿法".用于求解方程f(x)= 0的牛顿方法是建立一个数字序列xn,其定义为将x0作为一些初始"猜测"然后xn + 1 = xn-f(xn/f'(xn)其中f '(x)是f的导数.
发表于物理论坛
该方法的问题在于,如果我想计算5.2^0.33333,我需要找到这个方程的根x^10000 - 5.2^33333 = 0.我结束了庞大的数字,并获得inf和nan错误的大部分时间.
有人可以就如何解决这个问题给我建议吗?或者,有人可以提供另一种算法来计算^ n吗?
我尝试在多元函数上尝试我的牛顿方法片段并使用std::bind和std::function。但我被一个错误困住了
错误:请求从 'std::_Bind_helper&, int>::type {aka std::_Bind, int))(double, double, double)>}' 转换为非标量类型 'std::function'
此错误消息是什么意思,我应该如何修复我当前的代码?
#include <iostream>
#include<functional>
#include<cmath>
double newton(std::function<double(double)> F, std::function<double(double)> f,
double x=0, int maxiter=1000, double epsilon=0.001)
{
int n = 0;
while((n < maxiter) && (fabs(F(x)) > epsilon))
{
x = x - F(x) / f(x);
n++;
}
return x;
}
// I'd like to fix x and z at 1 and 2 and find root for y
double ftest(double x, double y, double z) …Run Code Online (Sandbox Code Playgroud) 我试图在 Julia 中实现多元牛顿法,但遇到了“无方法匹配”错误。下面是我的实现和我用来调用它的代码。
function newton(f::Vector, J::Matrix, x::Vector)
h = Inf64
tolerance = 10^(-10)
while (norm(h) > tolerance)
h = J(x)\f(x)
x = x - h
end
return x
end
Run Code Online (Sandbox Code Playgroud)
调用尝试 1
f(x::Vector) = [(93-x[1])^2 + (63-x[2])^2 - 55.1^2,
(6-x[1])^2 + (16-x[2])^2 - 46.2^2]
J(x::Vector) = [-2*(93-x[1]) -2*(63-x[2]); -2*(6-x[1]) -2*(16-x[2])]
x = [35, 50]
newton(f, J, x)
Run Code Online (Sandbox Code Playgroud)
运行上述代码时,抛出以下错误:
ERROR: LoadError: MethodError: no method matching newton(::typeof(f), ::typeof(J), ::Array{Int64,1})
Closest candidates are:
newton(::Array{T,1} where T, ::Array{T,2} where T, ::Array{Int64,1})
newton(::Array{T,1} where T, ::Array{T,2} …Run Code Online (Sandbox Code Playgroud) 如何float在python中打印超过10位数的数字?现在,什么时候做
print sqr_newton(10, 3, 0.001) (其中sqr_newton是newton的平方根算法;返回一个浮点数)
它只在小数位后给出这么多数字......我怎样才能得到更多?
我知道如何在Matlab中编写Newton方法,但是我仍然好奇Matlab中是否有内置的Newton求解器?(或者是二分法?)
在Haskell中,我使用以下函数来查找多项式的根:
polyNewton :: (Fractional a, Ord a) => Poly a -> a -> a`
polyNewton p s = if (abs(polyValue p s) <= (0 + 1e-10)) then s else polyNewton
p (s - (polyValue p s) / (polyValue (polyDeriv p) s))
Run Code Online (Sandbox Code Playgroud)
其中polyValue是一个函数,它找到多项式p的y值,x值x,而polyDeriv是一个返回多项式p的导数的函数.
对于大多数情况,此代码非常有效,除非多项式没有任何实际根(函数在x轴上方)或者给出了错误的初始猜测.无论如何确定牛顿的方法是否会事先失败,以便我的函数不会无限期地运行?
math haskell functional-programming polynomial-math newtons-method
我在 Pascal 中实现了 Newton-Raphson 方法。这很奇怪,因为相同的代码在 C++ 中给出了很好的结果(对于 9,它是 3),但在 Pascal 中,对于 9,它是 3.25,为什么会这样呢?
帕斯卡:
Program NewtonRaphsonIter(output);
{$mode objFPC}
function newton_raphson_iter(a: real; p: real; eps: real; max_i: integer) : real;
var
x: real;
i: integer;
begin
x := a / 2.0;
i := 0;
repeat
x := (x + a / x) / 2.0;
i := i + 1;
if (x * x = a) then break;
if (i >= max_i) then break;
until abs(x - a / x) > eps; …Run Code Online (Sandbox Code Playgroud)