Convert a function with 6 variables to a function with a vector of variables

3 views (last 30 days)
i have problem in optimizing a function with 6 variables with fminunc this function is calculated in a separate mfile and its variables are symbolic. i used "matlabFunction' to convert symbolic variables to double and it is the result of converted function:
fht = @(X1,X2,X3,X4,X5,X6)X1.*(-7.816841030404245e17./3.602879701896397e16)-X2.*(2.903662166186983e17./9.007199254740992e15)-X3.*(2.473111021279802e18./7.205759403792794e16)-X4.*(3.154514183980667e18./1.441151880758559e17)-X5.*(3.784458384964962e18./1.441151880758559e17)-X6.*(1.558659168414693e18./7.205759403792794e16)+X5.*(X5.*(6.691230574683395e15./1.40737488355328e14)-5.793287589989213e31./2.028240960365167e31).*(5.621830876825228e18./5.764607523034235e17)+X3.*(X3.*(6.990230458976231e15./8.796093022208e12)-2.296952299100395e32./4.056481920730334e31).*(5.621830876825228e18./5.764607523034235e17)+X2.*(X2.*(4.351445659255419e15./7.0368744177664e13)-1.579619832936913e31./5.070602400912918e30).*(5.621830876825228e18./5.764607523034235e17)+X1.*(X1.*(5.635276523765723e15./1.40737488355328e14)-1.090492421236695e32./4.056481920730334e31).*(5.621830876825228e18./5.764607523034235e17)+X4.*(X4.*(8.329047667948217e15./1.40737488355328e14)-2.492204817037969e32./8.112963841460668e31).*(5.621830876825228e18./5.764607523034235e17)+X6.*(X6.*(4.656868418989877e15./5.62949953421312e14)-5.140807783280828e31./4.056481920730334e31).*(5.621830876825228e18./5.764607523034235e17)-3.493633073667787e15./3.602879701896397e16
i set appropriate values for x0 and then i used fminunc and this error appeared: Error using makeFhandle/@(X1,X2,X3,X4,X5,X6)X1.*(-7.816841030404245e17./3.602879701896397e16)-X2.*(2.903662166186983e17./9.007199254740992e15)-X3.*(2.473111021279802e18./7.205759403792794e16)-X4.*(3.154514183980667e18./1.441151880758559e17)-X5.*(3.784458384964962e18./1.441151880758559e17)-X6.*(1.558659168414693e18./7.205759403792794e16)+X5.*(X5.*(6.691230574683395e15./1.40737488355328e14)-5.793287589989213e31./2.028240960365167e31).*(5.621830876825228e18./5.764607523034235e17)+X3.*(X3.*(6.990230458976231e15./8.796093022208e12)-2.296952299100395e32./4.056481920730334e31).*(5.621830876825228e18./5.764607523034235e17)+X2.*(X2.*(4.351445659255419e15./7.0368744177664e13)-1.579619832936913e31./5.070602400912918e30).*(5.621830876825228e18./5.764607523034235e17)+X1.*(X1.*(5.635276523765723e15./1.40737488355328e14)-1.090492421236695e32./4.056481920730334e31).*(5.621830876825228e18./5.764607523034235e17)+X4.*(X4.*(8.329047667948217e15./1.40737488355328e14)-2.492204817037969e32./8.112963841460668e31).*(5.621830876825228e18./5.764607523034235e17)+X6.*(X6.*(4.656868418989877e15./5.62949953421312e14)-5.140807783280828e31./4.056481920730334e31).*(5.621830876825228e18./5.764607523034235e17)-3.493633073667787e15./3.602879701896397e16 Not enough input arguments.
Error in fminunc (line 250) f = feval(funfcn{3},x,varargin{:});
Error in gasifi (line 170( [xfinal fval exitflag output] = fminunc(fht,x0);
Caused by: Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue.
as I learned fminunc only accepts function with a vector of variables like fth=@(x)..... i used X1=x(1) X2=x(2) X3=x(3) X4=x(4) X5=x(5) X6=x(6) to replace Xi with x(i) but i did not help and "fht" remained a function of(X1,X2,X3,X4,X5,X6).
i would be so thankful if anyone helps me to convert fht(X1,X2,X3,X4,X5,X6) to fht(x(

Accepted Answer

James Tursa
James Tursa on 31 Mar 2013
One way: Create an m-file function from your code and make a function handle to it. E.g.,
fht = @fht_fun
The file fht_fun.m:
function y = fht_fun(x)
X1 = x(1);
X2 = x(2);
X3 = x(3);
X4 = x(4);
X5 = x(5);
X6 = x(6);
y = X1.*(-7.816841030404245e17./3.602879701896397e16)-X2.*(2.903662166186983e17./9.007199254740992e15)-X3.*(2.473111021279802e18./7.205759403792794e16)-X4.*(3.154514183980667e18./1.441151880758559e17)-X5.*(3.784458384964962e18./1.441151880758559e17)-X6.*(1.558659168414693e18./7.205759403792794e16)+X5.*(X5.*(6.691230574683395e15./1.40737488355328e14)-5.793287589989213e31./2.028240960365167e31).*(5.621830876825228e18./5.764607523034235e17)+X3.*(X3.*(6.990230458976231e15./8.796093022208e12)-2.296952299100395e32./4.056481920730334e31).*(5.621830876825228e18./5.764607523034235e17)+X2.*(X2.*(4.351445659255419e15./7.0368744177664e13)-1.579619832936913e31./5.070602400912918e30).*(5.621830876825228e18./5.764607523034235e17)+X1.*(X1.*(5.635276523765723e15./1.40737488355328e14)-1.090492421236695e32./4.056481920730334e31).*(5.621830876825228e18./5.764607523034235e17)+X4.*(X4.*(8.329047667948217e15./1.40737488355328e14)-2.492204817037969e32./8.112963841460668e31).*(5.621830876825228e18./5.764607523034235e17)+X6.*(X6.*(4.656868418989877e15./5.62949953421312e14)-5.140807783280828e31./4.056481920730334e31).*(5.621830876825228e18./5.764607523034235e17)-3.493633073667787e15./3.602879701896397e16;
return
end

More Answers (1)

Alan Weiss
Alan Weiss on 29 Apr 2013
FYI, you could also use the technique in this example.
Alan Weiss
MATLAB mathematical toolbox documentation

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!