To talk on Icrontic, just register!

It only takes 30 seconds.

Have an account? Sign in:

Forgot?
PGolasze
New to the neighborhood
PGolasze
4 Posts

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 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 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)
shwaip
elaborate bot
shwaip
5,729 Posts

Re: MatLab problem

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')
__________________ my photostream for ic photography challenge

Anyone who wants dropbox, please use my referral link
PGolasze
New to the neighborhood
PGolasze
4 Posts

Re: MatLab problem

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.
PGolasze
New to the neighborhood
PGolasze
4 Posts

Re: MatLab problem

Yea our program doesn't handle complex roots.
PGolasze
New to the neighborhood
PGolasze
4 Posts

Re: MatLab problem

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?
Similar Threads
Thread Thread Starter Forum Replies Last Post
Raid 5 array problem RamRam Storage 4 21 Jan 2008 11:52pm
matlab help..strange problem jaspreet85 Web & Digital Media 1 2 Jun 2006 6:10pm
[resolved]Request for help on a particular spyware problem. york Resolved / Inactive 15 29 May 2006 2:19am
A unique problem not my own. :x Drax Resolved / Inactive 1 26 Dec 2004 5:55am
Quotation Mark (Key) Problem vanagon40 Operating Systems 11 18 Jun 2004 4:40pm

Go Back   Icrontic Forums > Tech: Software > General Software > Matlab Help
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 8:35am (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.