Prev: linear model from nonlinear
Next: Parsing a filename
From: Kevin on 17 Nov 2009 09:46 Hello, I have a question to which the answer is most likely simple. I'm having trouble using anonymous functions with fsolve. The gist of my problem is this: I have a somewhat large function that I would like to break into smaller functions, all of which are depenedent on one variable (say, x in the example below). Ultimately, I would like to use fsolve to find the root of the function with respect to x. However, I get the following error message: "?? Undefined function or method 'plus' for input arguments of type 'function_handle'. Error in ==> @(x)a+b-x Error in ==> fsolve at 254 fuser = feval(funfcn{3},x,varargin{:}); caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue." My actual function is much more complex, but the series of functions below layout the same basic structure. I believe that the error is syntax in nature, but I can not seem to find the proper construction. Any help would be appreciated. a=@(x)x^2+2 b=@(a)a+5 c=@(x)a+b-x fsolve(c,1)
From: Steven Lord on 17 Nov 2009 10:21 "Kevin " <remove_underscores_kevin_west(a)southalabama.edu> wrote in message news:hducvb$sib$1(a)fred.mathworks.com... > Hello, > > I have a question to which the answer is most likely simple. I'm having > trouble using anonymous functions with fsolve. The gist of my problem is > this: > > I have a somewhat large function that I would like to break into smaller > functions, all of which are depenedent on one variable (say, x in the > example below). Ultimately, I would like to use fsolve to find the root > of the function with respect to x. However, I get the following error > message: > > "?? Undefined function or method 'plus' for input arguments of type > 'function_handle'. That's correct. You can't add together function handles; what you want to do is add together _the values returned by those function handles_. > Error in ==> @(x)a+b-x > Error in ==> fsolve at 254 > fuser = feval(funfcn{3},x,varargin{:}); > caused by: > Failure in initial user-supplied objective function evaluation. FSOLVE > cannot continue." > > My actual function is much more complex, but the series of functions below > layout the same basic structure. I believe that the error is syntax in > nature, but I can not seem to find the proper construction. Any help > would be appreciated. > > a=@(x)x^2+2 > b=@(a)a+5 > c=@(x)a+b-x a and b are function handles, which as I've said above you can't add together. What you can do is: a = @(x) x.^2+2; b = @(x) a(x)+5; c = @(x) a(x)+b(x)-x; fsolve(c, 1) But note that as written, this doesn't have a solution over the reals; you can confirm this by looking at the graph and zooming in on the minimum. ezplot(c) or by expanding it out: a = x^2+2 b = (x^2+2)+5 = x^2 + 7 c = (x^2+2)+(x^2+7)-x = 2*x^2-x+9 and using ROOTS: roots([2 -1 9]) -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
|
Pages: 1 Prev: linear model from nonlinear Next: Parsing a filename |