find zero of a custom function
18 views (last 30 days)
Hello, I would like to know if it is possible to use fzero for a custom function written using a script. For example: let us suppose I have defined a function f.m which takes 3 arguments f(x,y,z) I know the values of x and y and I want to find the value of z which returns the value of my function 0,
is it possible to use fzero ? if yes could you post a very simple example?
Thanks you Paolo
John D'Errico on 7 Dec 2013
First of all, do NOT use inline functions. They are slow, a hack given to us in the days before function handles were provided.
% a better fun. See that since all parameters but
% y are known, the function handle uses those values.
% I could also have written fun as a function of 5
% parameters, and then reduced it to a single variable
% function using a function handle in the call to zero.
fun = @(y) -Pt1+y+(zcb+(rate*tau2*y));
Next, plot the function!!!!!!!!!! Always do this.
As you can see, there is clearly a root, just under 1.
ysol = fzero(fun,[0,2])
Anyone who claims this was not a root should learn to read scientific notation. So I'm not at all sure what you did wrong, but it looks like you used some inaccurate value in the computation.