Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Halley Iteration

Subject: Halley Iteration

From: Maddie Starks

Date: 25 Mar, 2012 12:15:11

Message: 1 of 3

Hi All,

Sorry if this seems fairly basic, I'm very new to Matlab!

I'm trying to find the root of the function f(x)=x^3+x+3 using the Halley method iteration formula. This is what I've got so far. Only problem is it keeps throwing up the error message:

"??? Attempted to access f(-19); index must be a positive integer or logical."


x1=1; % Set initial guess for the root

eps = 0.0000000001; % Choose the error bound
err = 1.;

iteration=0;
while err > eps & iteration<100 % Repeat until the error is smaller than eps
                                                        % or the number of iterations have reached a
                                                          % maximum (say 100 in this case)
[f]=fun(x1)
[ff]=halleyfunderiv(x1)
[fff]=halleyfunsecderiv(x1)

x2=x1-((f(x1)*ff(x1))/(((ff(x1))^2)-(0.5*f(x1)*fff(x1)))); % secant method
   
 err = abs(x2-x1); % Calculate the error
   
 [iteration x2 f err] % Display the iteration number, x, f(x), and err
    x1=x2;
    iteration=iteration+1;
end


Can anyone tell me where I'm going wrong???

Thanks
M

Subject: Halley Iteration

From: Roger Stafford

Date: 25 Mar, 2012 18:52:10

Message: 2 of 3

"Maddie Starks" wrote in message <jkn28f$c3i$1@newscl01ah.mathworks.com>...
> ......
> "??? Attempted to access f(-19); index must be a positive integer or logical."
> ......
> [f]=fun(x1)
> ......
- - - - - - -
  Believe what the error messages says: "index must be a positive integer or logical." Yours isn't!

Roger Stafford

Subject: Halley Iteration

From: Roger Stafford

Date: 25 Mar, 2012 19:16:12

Message: 3 of 3

"Roger Stafford" wrote in message <jknpgq$hcs$1@newscl01ah.mathworks.com>...
> "Maddie Starks" wrote in message <jkn28f$c3i$1@newscl01ah.mathworks.com>...
> > ......
> > "??? Attempted to access f(-19); index must be a positive integer or logical."
> > ......
> > [f]=fun(x1)
> > ......
> - - - - - - -
> Believe what the error messages says: "index must be a positive integer or logical." Yours isn't!
- - - - - - -
  I should have been a little clearer in my comment. By writing

[f]=fun(x1)

you have placed the value of your function, presumably x1^2+x1+3, into a variable known as "f". In subsequently writing f(x1), matlab interprets 'x1' as an index and complains about -19. Don't do things this way!

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us