From: Jan on 16 May 2010 14:24 Dear I want to make a function of a variable, I have a string variable with an equation in it and I want to construct a function which I can use for lsqcurvefit. I have this: % x0 are my initial parameters % ind is a column matrix with x and y values in it equation = x0(1)*ind(:,1).^x0(2)+x0(3)*ind(:,2).^x0(4); % = a*x^c+b*y^d equation = strcat('@(x0,ind)',equation,',''x0'',''ind'''); param = lsqcurvefit(f,x0,ind,z,-100,100,options); I got the following error: ??? Error using ==> inline.feval at 26 Too many inputs to inline function. The problem is that matlab doesn't recognize equation as a function. Can somebody help me?? Thanks in advance! Jan
From: Walter Roberson on 16 May 2010 15:43 Jan wrote: > Dear > > I want to make a function of a variable, I have a string variable with > an equation in it and I want to construct a function which I can use for > lsqcurvefit. > > I have this: > % x0 are my initial parameters > % ind is a column matrix with x and y values in it > > equation = x0(1)*ind(:,1).^x0(2)+x0(3)*ind(:,2).^x0(4); % = a*x^c+b*y^d > equation = strcat('@(x0,ind)',equation,',''x0'',''ind'''); > param = lsqcurvefit(f,x0,ind,z,-100,100,options); > > I got the following error: > ??? Error using ==> inline.feval at 26 > Too many inputs to inline function. You do not show a definition for f, and you do not use equation in your lsqcurvefit call? Your second line for equation does not make sense to me. You start with the text for an anonymous function declaration, but you leave no space between the body and the string 'x0' that you insert at the end, and you leave no space between that and the string 'ind' that you finish with. Your line would thus look like @(x0,ind)<SomethingHere>'x0''ind' which, if evaluated, would be something with the string x0'ind at the end of it. You appear to be confusing anonymous functions and inline functions. If you have an inline function, it should not have @ at the beginning of it; if you have an anonymous function, you should need to evaluate the definition of that function and pass the resulting function handle to lsqcurvefit. The <SomethingHere> that I indicated before is problematic. If x0 and ind are defined before the first line in which you define equation, then unless they are defined as symbolic, that first line is going to evaluate to a fixed numeric value (even if some of the items in it are strings), and that numeric value is not going to be compatible with strcat unless strcat converts the numeric value to characters in the range 0 to 255. If x0 and ind are defined and symbolic, strcat will not be able to convert the resulting symbolic expression into a string. If, though, x0 and ind are not defined before that line, then you will get an execution time error because of the undefined variables. What I _suspect_ you want is just equation = @(x0,ind) x0(1)*ind(:,1).^x0(2)+x0(3)*ind(:,2).^x0(4); without a second line and without any strings or inline functions at all.
From: Jan on 17 May 2010 03:06 Hi Thanks for your reply, but the difficult point is that I work with 2 figures: 1 main figure and a small figures that pops up and asks for an equation to be solved. When the equation is filled in, I write it to a textfile and then I should use that equation to be solved. But when I manually set up an equation, the following code works perfect: f = @(x0,ind)x0(1)*ind(:,1).^x0(2)+x0(3)*ind(:,2).^x0(4),'x0','ind'; param = lsqcurvefit(f,x0,ind,z,-100,100,options); To clear everything up: x0 = [0 0 0 0]; % Initial parameters ind = [x y]; % Column vectors of x and y datapoints equation = x0(1)*ind(:,1).^x0(2)+x0(3)*ind(:,2).^x0(4); % = a*x^c+b*y^d equation = strcat('@(x0,ind)',equation,',''x0'',''ind'''); param = lsqcurvefit(equation,x0,ind,z,-100,100,options); Can anyone help me? Thanks in advance
From: Steven Lord on 17 May 2010 10:26 "Jan " <jan_neyens(a)skynet.be> wrote in message news:hspd83$h42$1(a)fred.mathworks.com... > Dear > > I want to make a function of a variable, I have a string variable with an > equation in it and I want to construct a function which I can use for > lsqcurvefit. > > I have this: > % x0 are my initial parameters > % ind is a column matrix with x and y values in it > > equation = x0(1)*ind(:,1).^x0(2)+x0(3)*ind(:,2).^x0(4); % = > a*x^c+b*y^d > equation = strcat('@(x0,ind)',equation,',''x0'',''ind'''); If you're using a relatively recent version of MATLAB (release R2009a or later), use STR2FUNC. equation = 'x0(1)*ind(:,1).^x0(2)+x0(3)*ind(:,2).^x0(4)'; equation = ['@(x0,ind) ',equation]; fh = str2func(equation); fh(1:4, [1 2;3 4;5 6]) http://www.mathworks.com/access/helpdesk/help/techdoc/rn/bry1ecg-1.html#bry2cut-1 -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Walter Roberson on 17 May 2010 10:51 Jan wrote: > Thanks for your reply, but the difficult point is that I work with 2 > figures: 1 main figure and a small figures that pops up and asks for an > equation to be solved. When the equation is filled in, I write it to a > textfile and then I should use that equation to be solved. In that case, use eval() on the string to construct the function handle. > But when I manually set up an equation, the following code works perfect: > f = @(x0,ind)x0(1)*ind(:,1).^x0(2)+x0(3)*ind(:,2).^x0(4),'x0','ind'; I am having difficulty figuring out what that means. Ah... That is interpreted as 3 different commands. The first command is the assignment to f, the second command is 'x0' which displays the string x0 to the display, and the third command is 'ind'; which computes the string ind and then does not send it to the display because the semi-colon tells it not to output results. I would suggest that all in all, there is no point in having the ,'x0','ind' in that line. The syntax for inline functions takes variable names after it, but the syntax for anonymous functions does not.
|
Pages: 1 Prev: Call for Papers:Special Issue on Cognitive Information Processing Next: image processing |