Matlab, iteration with Newton's method, noobs & errors

In summary, the Newton-Raphson algorithm is a method for finding the roots of a function. It involves using an initial guess to iterate and get closer to the root. However, there are some limitations to this algorithm, such as not being able to handle non-integer numbers and having issues with certain types of functions. These issues can be resolved by adjusting the code to properly handle different types of functions.
  • #1
Waxterzz
82
0
Hi,

I have to describe the famous Newton-rapshon algorithm for finding a root of the function
this is what i came up with, i aint not familiar with MATLAB and programming at all, so this is somewhat a melt from a lot of MATLAB tutorials, I don't master the code, but what's really a challenge is understanding the errors
--------------------------------------------------------
function x = Newton(fun,dfun,x0,tol,Nmax)
x = x0;
f(x) = feval(fun,x);
df(x)= feval(dfun,x);
steps = 0;

re = tol;
myrel = 1;
while myrel > re && (steps < Nmax)
xold = x;
x = x - f(x)/df(x);
steps = steps + 1;
myrel = abs((x-xold)/x);
end;
if myrel <= re
disp( 'Zero found at' )
disp( x )
else
disp( 'Zero NOT found' )
end;
--------------------------------------------------------

when i test it, i find 2 very strange facts

fact 1: it cannot handle non-integer, if I want to test the function in the prompt with sin and cos and x0=3 , it stops on the first iteration: 3,14255...
this is the error:
attempted to acces f(3.14255); index must be a positive inter or logical

My idea: i thought MATLAB had no problems at all with any kind of number, in c++(also a big noob at that) u have to declarate everything, but not so in MATLAB yes?)

and fact2
i only tested the function with sin, cos and 3

when i test it with a simple Polynomial, let's say x²-2 for finding the value of square root of 2 it gives an error in the second line: f(x) = feval...

how can that line work with the first example and not with a simple polynomial in x,

can someone get me on track
 
Physics news on Phys.org
  • #2
bump ? anyone ??
 
  • #3
It sounds like f is set up as an array instead of a function.
 
  • #4
The problem is that when you write f(x), Matlab treat this as an vector. So the solution is to write something like f=feval(...) inside the while loop (and remove the line f(x)=... and similar for df(x)
 

What is Matlab?

Matlab is a programming language and computing environment used by scientists and engineers for data analysis, visualization, and algorithm development.

What is iteration with Newton's method?

Iteration with Newton's method is a numerical method used to approximate the roots of a function. It involves using an initial guess and repeatedly applying a formula to refine the guess until the desired level of accuracy is achieved.

What are "noobs" in the context of Matlab?

"Noobs" is a slang term commonly used to refer to beginners or inexperienced users in the context of programming and technology.

What are common errors encountered when using Matlab?

Common errors encountered when using Matlab include syntax errors, variable name conflicts, and mathematical errors such as dividing by zero or taking the square root of a negative number.

How can I troubleshoot and fix errors in Matlab?

To troubleshoot and fix errors in Matlab, you can use the debugger tool to step through your code and identify the source of the error. You can also check the documentation or seek help from online forums and communities. It is important to carefully review your code for any potential mistakes and to double check your inputs and outputs.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
802
  • Calculus
Replies
13
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
Back
Top