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

In summary, Kerry explained that the first error means MATLAB had trouble with its internal function mtimes (which is matrix multiplication), and the reason is that the inner matrix dimension must agree. The second part of the error message tells you where this occurred in your m-file. Kerry can't help you with your specific function in any more detail, because he doesn't know what you're trying to do. Kerry suggested trying to make different matricies at the command prompt.
  • #1
Kruum
220
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
  • #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 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
 
  • #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:
  • #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
 
  • #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.

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:
  • #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
 
  • #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:
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:

1. What is Newton's method in Matlab?

Newton's method in Matlab is a numerical algorithm used to find the root of a given function. It is an iterative process that uses the derivative of the function to approximate the root. It is commonly used for solving nonlinear equations.

2. How does Newton's method work in Matlab?

Newton's method works by starting with a guess for the root and then using the derivative of the function at that point to calculate a new guess. This process is repeated until the desired accuracy is achieved or the maximum number of iterations is reached.

3. What are the advantages of using Newton's method in Matlab?

One advantage of using Newton's method in Matlab is that it is a relatively fast and efficient algorithm for finding roots of functions. It also allows for a high degree of accuracy, especially when compared to other numerical methods.

4. What are the limitations of Newton's method in Matlab?

One limitation of Newton's method in Matlab is that it may not always converge to the root, especially if the initial guess is far from the actual root. It also requires knowledge of the derivative of the function, which may not always be readily available or easy to calculate.

5. How do I use Newton's method in Matlab to solve a real-world problem?

To use Newton's method in Matlab to solve a real-world problem, you first need to define the function and its derivative in Matlab. Then, you can use the built-in "fzero" function, which implements the Newton's method algorithm, to find the root of the function. You can also specify the desired accuracy and maximum number of iterations as inputs to the function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
805
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Calculus
Replies
13
Views
1K
  • Calculus and Beyond Homework Help
Replies
10
Views
979
  • Introductory Physics Homework Help
Replies
1
Views
991
Back
Top