MatLab problem - inline function zero finder

edited May 2007 in Science & Tech
Trying to come up with a function that takes a function h(x) searches for all of its zeros in a given range using the fzero() command in MatLab. Now me and my friends have gotten it to work on simple functions like 'Sin(CX)', 'Cos(Cx)', 'Log(CX)' and such but when we try to enter a fuction like 'x^2-3x+6' we get an error that says:

??? Error using ==> fzero
FZERO cannot continue because user supplied inline object ==> H
failed with the error below.
Error using ==> inlineeval
Error in inline expression ==> x^2+5x+6
??? Error: Unexpected MATLAB expression.
Error in ==> zsearch at 77
B=fzero(H,x0);

here is what we've come up with (any ideas on how to get around our little problem, something basic preferably)
function [] = zsearch(h,x0,x1,n,eps,r) % - output values give roots & dydx
H=inline(h);
if (x1<x0)
disp('Error: The Value Of x1 Must Be Greater Than x0; Please Restart The Function.')
return
elseif
(x1==x0)
disp('Error: No Roots Can Be Found; Please Restart The Function.')
return
end
if
(n>=1)
Inc=(x1-x0)/n;
elseif(n==0)
Inc=(x1-x0)/100;
else
disp('Error: The Input Of n Must Be Greater Than 0; Please Restart The Function.')
return
end
if
(eps==0)
eps=1e-5;
elseif (1e-6>eps)&(1e-1<eps)
else
eps=1e-5;
end
if
(r==0)
Inc;
elseif (r==1)
P=rand(1,1);
Inc=P;
else
disp('Error: r Must Be Either 1 Or 0; Please Restart The Function.')
return
end
i=1; % Citation Of Dr. Longtin's Original zsearch Code.
x(i)=0;
B=fzero(H,x0);
y(i)=B;
for (L=x0:Inc:x1)
B=fzero(H,L);
if abs(B - y(i)) > eps;
i=i+1;
y(i)=B;
x(i)=L;
end
end
Z=(eps);
J=H(y+eps);
I=H(y);
Slope=(J-I)/Z;
disp('The Roots Of The Function Are:')
disp(y)
disp('The Derivative Of The Function At The Roots Are: ')
disp(Slope)

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited May 2007
    I've not used inline functions or function handles in matlab, so take this info with a grain of salt.

    First: do you have a typo in the declaration of f?
    'x^2-3x+6'
    should be
    'x^2-3*x+6'

    also, the roots of that are imaginary. does fzero work for complex zeros?

    Try your code with something that doesn't have a negative determinant (ie: 'x^2-5*x+4')
  • edited May 2007
    Yea i think i figured it out... fuctions like that do work but MatLab seems to be picky about what it accepts, function in the form of something like x.^2+5*x+6 do work, but anything else results in that same error. Now i just have to implement a try catch block. Thanks for the help.
  • edited May 2007
    Yea our program doesn't handle complex roots.
  • edited May 2007
    i have a quick question, this program generates a set of derivatives at the roots, and the slope of the derivative at the roots alters sign from + to -. Now my problem is i have to right a line of code for my program that checks the sign of the derivatives to make sure the same sign doesn't repeat itself... any ideas guys? the code above hasn't change much.... Thanks.

    The code generates a vector of the roots, so what i figure was that i could just extract like the first 2 numbers use the sign command to get the signs and run a test on that but that doesn't really work... any ideas?
Sign In or Register to comment.