Home > Back-end >  Matlab newton method with finite differences
Matlab newton method with finite differences

Time:01-03

I would like some help with my program. I still don’t understand where my problem is, since it’s kind of a big mess. So it consists of the main program:

function x = NewtonM(funcF,JacF)
    x= zeros(2,1);
    x(1) = 1
    x(2) = 5
    k = 1;
    kmax = 100;
    TOL = 10^(-7);
    while k < kmax
    s = J(x)\(-F(x));
    x= x   s
    if (norm(s,2)< TOL)
        break;
    endif
end

and these are the fellow functions:

function y = F(x)
x1 = x(1);
x2 = x(2);
y = zeros(2,1);
y(1) = x1 x2-3;
y(2) = x1^2   x2^2 -9;
end
function z = Z(x)
x1 = x(1);
x2 = x(2);
z = zeros(3,1);
z(1) = x1 x2-3 10^(-7);
z(2) = (x1 10^(-7))^2   x2^2 -9;
z(3) = x1^2   (x2 10^(-7))^2 -9;
end
function J = J(x)
x1 = x(1);
x2 = x(2);
J = zeros(2,2);
J(1,1) = (Z(1)-F(1))/(10^(-7))
J(1,2) = (Z(1)-F(1))/(10^(-7))
J(2,1) = (Z(2)-F(2))/(10^(-7))
J(2,2) = (Z(3)-F(2))/(10^(-7))
end

These are the error messages:

also this is my current errors

errors vol 2

CodePudding user response:

The problem is that you are calling both Z and F with only one input, in the function J.

Then, the first thing you do is try to interpret the input as a 2 valued array (x1,x2) but they don't exist, as you defined x as e.g. 1, by doing Z(1).

I wonder if instead of using Z(1) etc, you meant to do z=Z(x) and then use z(1), inside J.

  • Related