From: catrina on 21 Jul 2010 13:30 Hi everyone, I would be grateful if you could help me with this basic question as I am very new to MATLAB. I need to find all the roots of some equation in the interval [o, pi], so I wrote the code below and it does work fine and give the right result, but the problem is that I need to find all the roots in the interval and the code nly giving me the root which close to the the intial value. How can I update the code below to find all the roots?? may be I need extra while statemnet?? function [root, numIter] = newton_simple(func,dfunc,x,tol) N = 3 format long g % while(root < pi) if nargin < 7; tol = 1.0e-8; for numIter = 1:500 dx = -feval(func,x,N)/feval(dfunc,x,N); x = x + dx; if abs(dx) < tol root = x % return end end root = NaN; end %%%%%%%%%%%% function y = dfunc(x,N) % N = 3; global lampda % lampda = 0.2; y = (N+1)*lampda^(-0.5)*cos((N+1)*x)-2*N*cos(N*x)+(N-1)*lampda^(0.5)*cos((N-1)*x); %%%%%%%%%%%%%% function y = func(x,N) % N = 3; global lampda % lampda = 0.2; y = lampda^(-0.5)*sin((N+1)*x)-2*sin(N*x)+lampda^(0.5)*sin((N-1)*x %%%%%%%%%%%%%%% the code above seems to give only one root, where I want to find all the roots in the interval [0,pi] I tried to do this by adding while(root < pi), befor if stataemnt, but it is still giving me only one root appreciate ur help thanks
From: John D'Errico on 21 Jul 2010 18:23 catrina <mohammed.saad37(a)yahoo.com> wrote in message <648906852.32852.1279747887767.JavaMail.root(a)gallium.mathforum.org>... > Hi everyone, > > I would be grateful if you could help me with this basic question as I am very new to MATLAB. > > I need to find all the roots of some equation in the interval [o, pi], so I wrote the code below and it does work fine and give the right result, but the problem is that I need to find all the roots in the interval and the code nly giving me the root which close to the the intial value. > > How can I update the code below to find all the roots?? may be I need extra while statemnet?? > Putting a spare while statement into your code will not make matlab understand your intentions. Matlab cannot read your mind! You might try a tool that is designed to find all roots of a general function on an interval. My rmsearch is one such tool. It works on one dimensional problems, using fzero as the underlying optimizer, but by first applying a scan through the interval looking for zero crossings. http://www.mathworks.com/matlabcentral/fileexchange/13733 John
From: Torsten Hennig on 21 Jul 2010 22:48 > Hi everyone, > > I would be grateful if you could help me with this > basic question as I am very new to MATLAB. > > I need to find all the roots of some equation in the > interval [o, pi], so I wrote the code below and it > does work fine and give the right result, but the > problem is that I need to find all the roots in the > interval and the code nly giving me the root which > close to the the intial value. > > How can I update the code below to find all the > roots?? may be I need extra while statemnet?? > > function [root, numIter] = > newton_simple(func,dfunc,x,tol) > N = 3 > format long g > % while(root < pi) > if nargin < 7; > tol = 1.0e-8; > for numIter = 1:500 > dx = -feval(func,x,N)/feval(dfunc,x,N); > x = x + dx; > if abs(dx) < tol > root = x > % return > end > end > root = NaN; > end > %%%%%%%%%%%% > function y = dfunc(x,N) > % N = 3; > global lampda > % lampda = 0.2; > y = > (N+1)*lampda^(-0.5)*cos((N+1)*x)-2*N*cos(N*x)+(N-1)*la > mpda^(0.5)*cos((N-1)*x); > %%%%%%%%%%%%%% > function y = func(x,N) > % N = 3; > global lampda > % lampda = 0.2; > y = > lampda^(-0.5)*sin((N+1)*x)-2*sin(N*x)+lampda^(0.5)*sin > ((N-1)*x > %%%%%%%%%%%%%%% > the code above seems to give only one root, where I > want to find all > the roots in the interval [0,pi] > > > I tried to do this by adding while(root < pi), befor > if stataemnt, but > it is still giving me only one root > > > appreciate ur help > > > thanks Newton's method is not adequate for your requirement. Use the method of nested intervals: Start at x1 = 0 and x2 = deltax for a small value of deltax. If f(x1)*f(x2) <= 0, there must be a zero in between. Locate that zero (by interpolation, e.g.), set x1 = x2, x2 = x2 + deltax and continue until x2 >= pi. Best wishes Torsten.
From: John D'Errico on 22 Jul 2010 07:11 Torsten Hennig <Torsten.Hennig(a)umsicht.fhg.de> wrote in message <1077803916.34490.1279781337416.JavaMail.root(a)gallium.mathforum.org>... > Newton's method is not adequate for your requirement. > Use the method of nested intervals: > Start at x1 = 0 and x2 = deltax for a small value > of deltax. If f(x1)*f(x2) <= 0, there must be a zero > in between. Locate that zero (by interpolation, e.g.), > set x1 = x2, x2 = x2 + deltax and continue until x2 >= pi. > > Best wishes > Torsten. Essentially what rmsearch already does, when faced with a 1-d problem. John
From: Steven_Lord on 22 Jul 2010 09:59 "Torsten Hennig" <Torsten.Hennig(a)umsicht.fhg.de> wrote in message news:1077803916.34490.1279781337416.JavaMail.root(a)gallium.mathforum.org... *snip* > Newton's method is not adequate for your requirement. > Use the method of nested intervals: > Start at x1 = 0 and x2 = deltax for a small value > of deltax. If f(x1)*f(x2) <= 0, there must be a zero > in between. Finding ALL the roots of an equation, even a continuous one, is not an easy problem. Your approach, Torsten, will work in many circumstances, but would fail if there are multiple roots in that small interval between x1 and x2. An approach that would return all the roots that MATLAB can represent in that interval, but would be VERY slow, is: x1 = lowerLimitOfSearchInterval; while x1 < upperLimitOfSearchInterval if f(x1) == 0 % root found end x1 = x1 + eps(x1); end If the function's vectorized, you could use this to create a vector of X values and simply evaluate f on that vector once at the end. Just make sure your search interval is VERY short or you have a LOT of time and memory on your hands. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
|
Pages: 1 Prev: Translate matrix up and down spatially Next: help with retrieving information about pixels |