Matlab - Newton's Method for Function: Troubleshooting and Solution

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a MATLAB implementation of Newton's method for solving a system of equations. Participants explore issues related to matrix dimensions, function arguments, and the overall logic of the code, as well as the behavior of the method in finding roots.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • The original code produces a matrix dimension error due to incorrect matrix multiplication, specifically in the line involving the calculation of the next point.
  • One participant identifies that the error is related to how MATLAB interprets matrix definitions and suggests using semicolons to separate rows.
  • Another participant mentions that the function currently returns two identical values for x, prompting questions about whether a second loop is necessary to compute y values.
  • Concerns are raised about overwriting input arguments within the function, which could lead to confusion regarding the intended number of iterations.
  • Participants discuss the need for clearer variable names and comments in the code to aid in debugging and understanding the function's purpose.
  • One participant successfully modifies the code to include a second loop for y values, but questions whether this can be achieved with a single loop.
  • There is a clarification about how Newton's method works, specifically that it typically finds one root based on the starting point provided.
  • After further experimentation with initial values, one participant reports success in finding multiple roots, indicating a better understanding of how to set up the function.

Areas of Agreement / Disagreement

Participants express a mix of agreement and confusion regarding the implementation details of the MATLAB function. While some issues are resolved, such as understanding function arguments, there remains uncertainty about the optimal structure of the code and the behavior of Newton's method in finding multiple roots.

Contextual Notes

Limitations include potential misunderstandings about the nature of Newton's method and its reliance on initial conditions, as well as the need for clearer code documentation to facilitate debugging.

Who May Find This Useful

This discussion may be useful for students learning MATLAB, particularly those working on numerical methods and seeking to understand the implementation of algorithms like Newton's method.

Kruum
Messages
217
Reaction score
0

Homework Statement



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.

The Attempt at a Solution



So I've got the code to look like this:
Code:
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:
Physics news on Phys.org
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 occurred 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
 
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:
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
 
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.

KLoux said:
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.

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:
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 .[/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:
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
 
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:
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K