MatLab problem - inline function zero finder
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)
??? 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)
0
Comments
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')
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?