please help me to find the root of the equation

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

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories