Filter löschen
Filter löschen

non linear equation using newtons method

1 Ansicht (letzte 30 Tage)
Ronald Aono
Ronald Aono am 8 Dez. 2019
Beantwortet: Walter Roberson am 8 Dez. 2019
% start iteration loop
if x2 == 1.0, x2 = 1.001; end % don't allow x2 = 3 as guess
x1 = sqrt(4-(4*x2.*x2)); % satisfies eqn. 1
xo = [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
keep getting the following error
Matrix dimensions must agree.
Error in HW_6c (line 20)
xnew = xold - J\f;

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 8 Dez. 2019
xold is not defined in your function before the first time you use it. However, your use of
xo = [x1 x2 ]';
hints that xold is likely to be 2 x 1. If so then when you do
xnew = xold - J\f;
then because J\f would be 1 x 2, you would be getting a 2 x 1 minus a 1 x 2. In R2016a or earlier that would be an error.
In R2016b or later, it is well defined, and would give you a 2 x 2 array as a result. That would probably trigger problems further down the road.

Weitere Antworten (0)

Kategorien

Mehr zu Simulink finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by