1

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

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Steve
  • 13
  • 3
  • the line where the error happens is also important! can you share the error as text, with the line info? – Ander Biguri Jan 02 '23 at 11:32
  • @AnderBiguri the error lines dont actually give me a good indicator since there isnt something to be fixed at those lines mentioned in the photo. – Steve Jan 02 '23 at 11:55
  • 1
    Its 100% always the line where the error is. You may not understand why, but there is almost certainly something to learn about when the error happens. You also need to show is how you call this, the error happens in `ff()` which is not shown here – Ander Biguri Jan 02 '23 at 11:57
  • @AnderBiguri also this is what you asked for [link](https://imgur.com/a/sIJs8LS) – Steve Jan 02 '23 at 12:00
  • Please [edit] your question and never ever share text as an image, it is text. – Ander Biguri Jan 02 '23 at 12:04
  • @AnderBiguri not sure how to edit the comment so i added the image on my original post – Steve Jan 02 '23 at 12:08
  • Please copy-paste the error message as text, don’t post text as an image. I can’t read it. – Cris Luengo Jan 02 '23 at 14:08

1 Answers1

0

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.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • that was it i was actually thinking that this could be the problem but sometimes the errors are just under our eyes. Thanks for the knowledge you shared with me sir. – Steve Jan 02 '23 at 12:14