将牛顿迭代存储在数组中

时间:2019-05-19 14:27:31

标签: arrays matlab storage newtons-method

我已经在matlab中编写了一个函数,该函数将牛顿方法应用于给定的方程组及其雅可比派。接近方程系统中给定的两个函数的交点的x向量按如下方式存储:

2.06733483062522    -0.615946742374531
2.25465524043858    -0.428626332561163
1.13788162420748    -1.54539994879227
1.32520203402085    -1.35807953897890
0.802454971005455   -1.88082660199429
0.989775380818824   -1.69350619218092
0.743382354827609   -1.93989921817214
0.930702764640978   -1.75257880835877
0.741429028102197   -1.94185254489755
0.928749437915565   -1.7545321350841

这里

 x1 = [2.06733~ ; 2.25465~]

,因此它们是垂直存储的,但是我希望它们在彼此下方水平放置。感谢您的帮助

我试图使用偶数和奇数项在for循环中取出这些值,但是对于高阶方程组,这是行不通的。

% INPUT
% f root function (may be vector valued )
% df derivative of f (or function returning Jacobian matrix )
% x0 initial guess
% tol desired tolerance
% maxIt maximum number of iterations
%
% OUTPUT
% x approximate solution
% success true means converged according to error estimator
% errEst error estimate per iteration
% xHist ( optional ) array with intermediate solutions

function [x, success , errEst, xHist ] = newton (f, df, x0 , tol , maxIt )
errEst = []; xHist = [];
for k = 1:1:size(f)

iter = 0; err = inf; x = x0; success = false;


while err > 0 && iter < maxIt
      Fun = f{k}(x);
      Jac = df{k}(x);
      delta = -Jac\Fun;
      err = norm(delta);
      x = x + delta;
      errEst = [errEst; err];
      xHist = [xHist; x];
      iter = iter + 1;
end
end
if err < tol
    success = true;
end

end

它被这样称呼:

x0 = 1/sqrt(2) * [2;1]; grad = [sqrt(2)/2;sqrt(2)]; n = grad/norm(grad); t = [n(2),-n(1)]; p = 0.5;
x0 = x0 + 2 * (n+t); %x0 = x0 - 2 * (n+t);
f = {@(x)(x(1)/2)^2 + x(2)^2 - 1, @(x)n .* (x - x0) - p - 0.5 .* (t .* (x - x0)) .^ 2};
df = {@(x)x(1)/2 + 2 * x(2), @(x)n - t .* (t .*(x - x0))};
[x, success , errEst , xHist ] = newton(f, df, x0 , 10^(-12) , 20);

0 个答案:

没有答案