这个问题与"C++中的数字配方"一书有关,因此它将保留给了解它的人以及多维优化.
我正在编写一个需要搜索多维根的程序,为了解决这个问题,我使用的是多维牛顿根查找方法,即"newt"程序.
对于那些对细节感兴趣的人,我试图将一个可变形的3D模型拟合到一个物体的立体视图,基于一些特征点(两个摄像机可以看到的特征点).
为此,我使用newt过程如下:
我的问题是我有比输入参数(11)更多的输出参数(14):每当我调用"newt"时,算法总是收敛,但是它会找到一个解决方案,几乎完美地最小化了11个第一个输出参数,但是剩下的3个参数有很多错误.
但是我希望错误在输出参数之间统一划分.
我已经尝试过下面描述的方法:
有没有人知道更通用的方法,其中根查找算法会支持在输出参数之间均匀划分的错误,而不是支持第一个参数?
c++ mathematical-optimization newtons-method numerical-methods
在过去的几周里,我一直在尝试模拟太阳系模拟中的轨道,我正在将其作为大学模块的一部分.简而言之,我的模拟使用Ogre3D渲染引擎用C++编写.我曾尝试使用牛顿万有引力定律实现轨道运动,这使得我的行星朝着太阳直线前进,穿过太阳然后回到它的起始位置.我还尝试了这篇维基百科文章中 "位置作为时间函数"部分的步骤,但这对我来说也不起作用.
我用简单的Euler积分方法驱动模拟.如果任何人对这种模拟有任何经验,或者只是对这些物理定律有很多了解,那么任何帮助或指向我正确的方向将非常感激.
C中的这段代码(附在帖子中)使用Newton-Raphson方法在特定区间中找到多项式的根.
对于某些多项式,这段代码可以完美地x^3 + x^2 + x + 1运行,但运算符对于某些多项式来说是无限的x^3 - 6*x^2 + 11*x - 6.也就是说,此代码适用于输入间隔中具有一个或零根的多项式,但如果存在多个根,则它将无限期运行.
如果有人找到解决方案,请告诉我.我在代码中写了一些评论来引导读者,但如果有人发现很难理解,可以在评论中提问,我会解释一下.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
int check(float num) //just a function to check for the correct input
{
char c;
scanf("%c",&c);
if(isalpha((int)c))
printf("you entered an alphabet\n");
else
printf("you entered a character, please retry\n");
return 0;
}
float func(float *p,int order,double x) //calculates the value of the function required in the formmula in main
{
double fc=0.0;
int i;
for(i=0;i<=order;i++)
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过牛顿方法创建一个计算立方根的函数,但由于某种原因,我似乎在这里有一个无限循环?
#include <iostream>
#include <math.h>
using namespace std;
double CubicRoot(double x, double e);
int main()
{
cout << CubicRoot(5,0.00001);
}
double CubicRoot(double x, double e)
{
double y = x;
double Ynew;
do
{
Ynew = y-((y*y)-(x/y))/((2*y)+(x/(y*y)));
cout << Ynew;
} while (abs(Ynew-y)/y>=e);
return Ynew;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个在二维中实现牛顿方法的函数,虽然我已经完成了这个,但我现在必须调整我的脚本,以便我的函数的输入参数必须是列矢量中的f(x),雅可比矩阵的f(x),最初的猜测x0和宽容其中函数f(x)及其雅可比矩阵是在单独的.m文件.
作为我编写的实现牛顿方法的脚本示例,我有:
n=0; %initialize iteration counter
eps=1; %initialize error
x=[1;1]; %set starting value
%Computation loop
while eps>1e-10&n<100
g=[x(1)^2+x(2)^3-1;x(1)^4-x(2)^4+x(1)*x(2)]; %g(x)
eps=abs(g(1))+abs(g(2)); %error
Jg=[2*x(1),3*x(2)^2;4*x(1)^3+x(2),-4*x(2)^3+x(1)]; %Jacobian
y=x-Jg\g; %iterate
x=y; %update x
n=n+1; %counter+1
end
n,x,eps %display end values
Run Code Online (Sandbox Code Playgroud)
因此,使用此脚本,我已将函数和雅可比矩阵实现到实际脚本中,我正在努力研究如何使用所需的输入参数实际创建脚本.
谢谢!
我是matlab的新手,我需要创建一个函数,该函数使用起始近似x = a进行N次迭代的Newton-Raphson方法.这种起始近似不算作交互,另一个要求是需要for循环.我看过其他类似的问题,但在我的情况下,我不想使用while循环.
这是我的输入应该是:
mynewton(f,a,n) which takes three inputs:
f: A function handle for a function of x.
a: A real number.
n: A positive integer.
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的代码.
function r=mynewton(f,a,n)
syms x;
z=f(x);
y=a;
for i=1:n
y(i+1)=y(i)-(z(i)/diff(z(i)));
end
r=y
end
Run Code Online (Sandbox Code Playgroud)
当我尝试调用该函数时,我收到一条错误消息:
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in mynewton (line 6)
y(i+1)=y(i)-(z(i)/diff(z(i)));
Run Code Online (Sandbox Code Playgroud)
问题是我如何使用这个VPA功能?当然,我的其余代码可能也不是100%正确,但任何解决vpa问题或修复代码其他部分的帮助都将非常感激.
谢谢!
我正在做Go教程,我想知道是否有更优雅的方法来计算平方根,使用Newton的方法练习:循环和函数比这个:
func Sqrt(x float64) float64 {
count := 0
var old_z, z float64 = 0, 1
for ; math.Abs(z-old_z) > .001; count++ {
old_z, z = z, z - (z*z - x) / 2*z
}
fmt.Printf("Ran %v iterations\n", count)
return z
}
Run Code Online (Sandbox Code Playgroud)
(规范的一部分是提供迭代次数.)这是完整的程序,包括package语句,导入和main.
好吧,到目前为止,我想很多人都知道着名的快反平方根(详见编写自己的平方根函数和0x5f3759df)
这是代码
float FastInvSqrt(float x) {
float xhalf = 0.5f * x;
int i = *(int*)&x; // evil floating point bit level hacking
i = 0x5f3759df - (i >> 1); // what the fuck?
x = *(float*)&i;
x = x*(1.5f-(xhalf*x*x)); // one Newton Method iteration
return x;
}
Run Code Online (Sandbox Code Playgroud)
好吧,我不需要知道更多的魔法0x5f3759df.
我不明白为什么x*(1.5f-(xhalf*x*x))是Newton Method迭代?
我试过分析,但却无法得到它.
所以我们假设r是实数,x是r的逆sqrt.
1 / (x^2) = r,然后f(x) = r*(x^2)-1和f'(x) = 2 * r …
我正在尝试使用8086处理器编写汇编程序,该处理器将找到数字的立方根.显然我使用的是浮点数.
root := 1.0;
repeat
oldRoot := root;
root := (2.0*root + x/(root*root)) / 3.0
until ( |root – oldRoot| < 0.001;
Run Code Online (Sandbox Code Playgroud)
如何将(2*root + x)除以(root*root)?
.586
.MODEL FLAT
.STACK 4096
.DATA
root REAL4 1.0
oldRoot REAL4 2.0
Two REAL4 2.0
inttwo DWORD 2
itThree DWORD 3
three REAL4 3.0
x DOWRD 27
.CODE
main PROC
finit ; initialize FPU
fld root ; root in ST
fmul two ; root*two
fadd x ; root*two+27
fld root ; root …Run Code Online (Sandbox Code Playgroud)