newtons non linear method

3 次查看(过去 30 天)
Ronald Aono
Ronald Aono 2019-12-10
评论: darova 2019-12-10
% start iteration loop
clear all
x2 = [1 0.5]';
x1 = sqrt(4-(4*x2.*x2)); % satisfies eqn. 1
xold = [x1 x2 ]';
itmax = 30; it = 0; tol = 1e-5; emax=1; n = length(xold);
fprintf(1,'\n Intermediate edit for NLDemo2 \n');
while emax > tol && it <= itmax
it = it+1;
x= xold;
%
% compute function vector using xold
f = [(x(1)*x(1))-(2*x(1))-x(2)+0.5;
(-x(1)*x(1))+(4*(x(2)*x(2)))-4];
%
% compute Jacobian matrix evaluated at xold
J = [2*x(1)-2 -1;
2*x(1) -8*x(2)];
%
% compute xnew
xnew = xold - J\f;
%
% calc & edit error (intermediate results)
emax = max(abs((xnew-xold)./xnew));
fprintf(1,' it = %3d max error = %8.3e \n',it,emax);
fprintf(1,' xnew xold \n');
for j = 1:n
fprintf(1,' %10.5f %10.5f \n',xnew(j),xold(j));
end
%
xold = xnew; % use current estimate as guess for next iteration
end
%
% print final max relative error and iteration count
fprintf(1,'\n Number of iterations to convergence = %3d\n',it);
fprintf(1,' Max relative error at convergence = %8.3e\n',emax);
if it >= itmax
fprintf(1,' ***** WARNING -- Hit max number of iterations!!! *****\n');
not able to make it past the first iteration, ang i getthe error below
>> HW6B_2c
Intermediate edit for NLDemo2
it = 1 max error = 1.000e+00
it = 1.686852e-01 max error = xnew xold
-0.25000 0.00000
1.00000 1.00000
Operands to the || and && operators must be convertible to logical scalar values.
Error in HW6B_2c (line 8)
while emax > tol && it <= itmax
  3 个评论
Ronald Aono
Ronald Aono 2019-12-10
first i had it has a 1x1 but the answer is a 2x1
darova
darova 2019-12-10
WHat we gonna do about it?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by