Debugging Newton's Method code in two variables,

Hi,
I wrote a simple code for Newton's Method in two variables but am having some trouble debugging it. Here's the message I get:
Index exceeds matrix dimensions.
Error in Root_finding_practice>@(x)[cos(x(2)),-x(1)*sin(x(2));x(2)*cos(x(1)),sin(x(1))]
Error in Root_finding_practice (line 34)
x(i+1) = x(i) - ( inv( J( x(i) ) ) * f( x(i) ) );
The function file code is:
function F = nonlinear_equations(x)
F(1) = x(1) * cos( x(2) );
F(2) = x(2) * sin( x(1) );
end
and the script file code is:
f = @(x) nonlinear_equations;
% Jacobian
J = @(x) [ cos( x(2) ), -x(1)*sin(x(2));
x(2) * cos(x(1)), sin(x(1)) ];
x = [ 1, 1 ];
for i = 1:1000 % it should be stopped when tolerance is reached
x(i+1) = x(i) - ( inv( J( x(i) ) ) * f( x(i) ) );
if( abs( f( x(i+1) ) ) < 0.0001 ) % tolerance
disp(double(x(i+1)));
break;
end
end
What am I missing? I suspect it's the way I've defined the Jacobian anonymous function ...
Thanks,

 Risposta accettata

Ameer Hamza
Ameer Hamza il 21 Set 2020
Modificato: Ameer Hamza il 21 Set 2020
Check this code
f = @(x) nonlinear_equations(x);
% Jacobian
J = @(x) [cos( x(2) ), -x(1)*sin(x(2));
x(2)*cos(x(1)), sin(x(1))];
x = [1; 1];
for i = 1:1000 % it should be stopped when tolerance is reached
x(:,i+1) = x(:,i) - inv(J(x(:,i)))*f(x(:,i));
if( abs(f(x(:, i+1))) < 0.0001) % tolerance
disp(double(x(:, i+1)));
break;
end
end
function F = nonlinear_equations(x)
F = zeros(2, 1);
F(1) = x(1) * cos( x(2) );
F(2) = x(2) * sin( x(1) );
end

8 Commenti

Noob
Noob il 21 Set 2020
Modificato: Noob il 21 Set 2020
Hi Ameer,
Thanks for your answer; how come you added the F = zeros(2,1) to the function file code? Is it necessary?
Now, I'm getting this error, after implementing your suggestions:
Not enough input arguments.
Error in nonlinear_equations (line 6)
F(1) = x(1) * cos( x(2) );
Error in Root_finding_practice>@(x)nonlinear_equations
Error in Root_finding_practice (line 36)
x(:,i+1) = x(:,i) - ( inv( J( x(:,i) ) ) * f( x(:,i) ) );
Yes, F = zeros(2, 1); is needed to make sure that the function returns a column vector. Otherwise the matrix multiplication will fail. The alternative is
function F = nonlinear_equations(x)
F(1) = x(1) * cos( x(2) );
F(2) = x(2) * sin( x(1) );
F = F(:);
end
Both are doing the same thing.
About the error; can you exactly paste the code which gives this error.
I see; sure, here's the code:
The function file first:
function F = nonlinear_equations(x)
% F = zeros(2, 1);
F(1) = x(1) * cos( x(2) );
F(2) = x(2) * sin( x(1) );
F = F(:);
end
And the script file:
% root-finding practice in two variables:
f = @(x) nonlinear_equations;
% Jacobian
J = @(x) [ cos( x(2) ), -x(1)*sin(x(2));
x(2) * cos(x(1)), sin(x(1)) ];
x = [1; 1];
for i = 1:1000 % it should be stopped when tolerance is reached
x(:,i+1) = x(:,i) - ( inv( J( x(:,i) ) ) * f( x(:,i) ) );
if( abs( f( x(:,i+1) ) ) < 0.0001 ) % tolerance
disp(double(x(:,i+1)));
break;
end
end
Thanks,
I also changed the line to
f = @(x) nonlinear_equations(x);
Alternative is
f = @nonlinear_equations;
Hi Ameer,
Thanks so much! It works now, and I think I prefer your alternative f = @nonlinear_equations, rather than creating what looks like a redundant anonymous function.
Have a great day / night.
Hi Ameer,
Just a quick question, if you don't mind: how can I evaluate the function now? The extra variable is throwing me off a bit:
>> z = nonlinear_equations(-0.000000006238568,0.809548393600924)
Error using nonlinear_equations
Too many input arguments.
Thanks,
Oh, I see; I just have to define the inputs as a vector first.
Thanks again, Ameer!
Yes, input need to be passed as vector.
I am glad to be of help :)

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2017a

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by