1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab - Newton's method

  1. Mar 16, 2009 #1
    1. The problem statement, all variables and given/known data

    Our assignement is to fix an .m file, which should produce Newton's method for a function. I'm very new to Matlab and some expressions are weird to me.

    3. The attempt at a solution

    So I've got the code to look like this:
    Code (Text):
    function point=teemunewton(xbeg, ybeg, iternmbr)

    iternmbr=10;
    X=zeros(2,iternmbr);
    xbeg=X(1,1);
    ybeg=X(1,2);

    for roun=2:iternmbr;
        x=X(1,roun-1);
        y=X(2,roun-1);
        X(:,roun)= X(:,roun-1)-inv([8*y 2*x ; 2*x-2*y -2*x+18*y])*[x^2+4*y^2-13, x^2-2*x*y+9*y^2-17];  
    end

    point=X(:,iternmbr)
    Every time I tr to run it I get a warning:
    ??? Error using ==> mtimes
    Inner matrix dimensions must agree.

    Error in ==> teemunewton at 11
    X(:,roun)= X(:,roun-1)-inv([8*y 2*x ; 2*x-2*y -2*x+18*y])*[x^2+4*y^2-13,
    x^2-2*x*y+9*y^2-17];

    What does the first error mean? And how should I define the X matrix?
     
    Last edited: Mar 16, 2009
  2. jcsd
  3. Mar 16, 2009 #2
    This is actually all one error (the code may contain more, I didn't look at it).

    What this is telling you is that MATLAB had trouble with its internal function mtimes (which is matrix multiplication) and the reason is that the inner matrix dimension must agree (I assume you're familiar with some linear algebra basics?).

    The second part of the error message tells you where this occured in your m-file. It tells you that the error is on the 11th line, and it prints the line for you.

    Is this enough of a clue?

    -Kerry
     
  4. Mar 17, 2009 #3
    Yeah, thanks, I found out what caused the error. It was this command: [x^2+4*y^2-13 , x^2-2*x*y+9*y^2-17]. How does Matlab interpret that line? Obviously I have to use semicolon there.

    Anyways there's a new problem. Now the function runs okay, but it gives me two identical values, both of which (I assume) are the values for x. Do I need to create another loop to get the value for y or is there a trick that allows me to do it more simply? Oh and, I have modified this line inv([2*x 8*y; 2*x-2*y -2*x+18*y])*[x^2+4*y^2-13, x^2-2*x*y+9*y^2-17] to give determinants. The problem is that I need to create one matrix as the latter one for x and another one for y. I'm guessing it's only possible if I do another loop.
     
    Last edited: Mar 17, 2009
  5. Mar 17, 2009 #4
    OK, taking a look at your function, it looks like you're doing a couple of things you may not want to do. The first is that you're overwritting the input arguments. If you call your function with iternmbr set to 5, it won't matter - it always gets set to 10. This is true for all of your input arguments.

    If you're creating a matrix, you need to be able to specify columns (use commas or spaces) as well as rows (this is what semi-colons are for). So the following matrix would be 2 x 3:
    A = [1 2 3;4 5 6]

    which is the same as:
    A = [1, 2, 3;4, 5, 6]

    You can experiment with this by trying to make different matricies at the command prompt.

    I can't help you with you're specific function in any more detail, because I'm not sure what you're trying to do. Newton's method is usually an optimization technique, so saying that you're trying to 'produce Newton's method for a function' is confusing. Your code also has no comments and the variable names don't tell me anything about what you're trying to do. Without knowing the intent of each line, debugging becomes very difficult.

    -Kerry
     
  6. Mar 17, 2009 #5
    Thank you, Kerry. I appreciate your input a lot! As I said I'm new to Matlab and I haven't code anything that much, so adding comments isn't automatic yet.

    I'm a bit confused, what iternmbr you mean. If I remove the definition of iternmbr (i.e. the 3rd line), the function won't work.

    Here's my current version. I added another loop to get y, this seems to work. Still, can I do the same with only one loop?

    Code (Text):
    function point=teemunewton(iternmbr)

    iternmbr=10;  %times to run the method
    X=[1 zeros(1,iternmbr-1);1 zeros(1,iternmbr-1)];  %matrix for variables, first row for x and second for y

    for roun1=2:iternmbr;
        x=X(1,roun1-1);  %value to call for x
        y=X(2,roun1-1);  %value to call for y
        X(1,roun1)= X(1,roun1-1)-det(inv([2*x 8*y; 2*x-2*y -2*x+18*y]))*det([x^2+4*y^2-13 8*y; x^2-2*x*y+9*y^2-17 -2*x+18*y]); %value for x_(n+1) saved to correspoding element in matrix X
       
      for roun2=2:iternmbr;
           x=X(1,roun2-1);
           y=X(2,roun2-1);
           X(2,roun2)= X(2,roun2-1)-det(inv([2*x 8*y; 2*x-2*y -2*x+18*y]))*det([2*x x^2+4*y^2-13; 2*x-2*y x^2-2*x*y+9*y^2-17]);  %same as above, only for y
     
       end
    end

    point=X(:,iternmbr); %calling the root
    Edit: The system of equations I'm trying to solve is: http://www.aijaa.com/img/b/00857/3824579.jpg [Broken].[/URL] I found out my Matlab function gives me one correct root But if I plotted the functions right, there should be more than one root.
     
    Last edited by a moderator: May 4, 2017
  7. Mar 17, 2009 #6
    If you write a function (like you have done), rather than a script, you can call your function from the MATLAB command prompt or from other files by typing the name of your function and supplying arguments (assuming you have saved you m-file with the correct name). If you have saved your file as teemunewton.m, then you can write this:

    teemunewton(7)% This will call your function with iternmbr = 7

    or

    teemunewton(8342)% This will call your function with iternmbr = 8342

    but the first thing you do is overwrite this value so that no matter what this value is, you are setting it to 10.

    Have you googled newton's method yet? I think Wikipedia has a decent write up. It will only find one root - that's how it works. Generally, you supply a starting point, and it will follow the gradient of your function until it converges (if it converges) on a root. If you supply a different starting point, it might find a different root. Looking at your code, I cannot see for sure where you specify your starting point. Maybe the first entry in your X matrix is your starting point (x = 1, y = 1). Is the root that you find the one that is closest to this point?

    -Kerry
     
  8. Mar 18, 2009 #7
    Ah, okay. I didn't guite get the idea behind the arguments, I didn't know the user could set them. This clears a lot! The function is now working perfectly and I can find all the roots. Earlier I just didn't play around enough with initial values.

    If someone's interested here's what I got:
    Code (Text):
    function point=teemunewton(xbeg, ybeg, iternmbr)

    X=zeros(2,iternmbr);  %matrix for variables, first row for x and second for y
    X(1,1)=xbeg
    X(2,1)=ybeg

    for roun1=2:iternmbr;
        x=X(1,roun1-1);  %value to call for x
        y=X(2,roun1-1);  %value to call for y
        X(1,roun1)= X(1,roun1-1)-det(inv([2*x 8*y; 2*x-2*y -2*x+18*y]))*det([x^2+4*y^2-13 8*y; x^2-2*x*y+9*y^2-17 -2*x+18*y]); %value for x_(n+1) saved to correspoding element in matrix X
        for roun2=2:iternmbr;
        x=X(1,roun2-1);
        y=X(2,roun2-1);
        X(2,roun2)= X(2,roun2-1)-det(inv([2*x 8*y; 2*x-2*y -2*x+18*y]))*det([2*x x^2+4*y^2-13; 2*x-2*y x^2-2*x*y+9*y^2-17]);
        end
    end

    point=X(:,iternmbr);
    Thanks for the help, Kerry!
     
    Last edited: Mar 18, 2009
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab - Newton's method
  1. Euler's method/matlab (Replies: 2)

Loading...