MATLAB: Fsolve with structure of functions in a cell

fsolve; cell; matlab; help;MATLAB

Hi,
I'm trying to solve a structure of 10 equations using fsolve, but so far, Matlab refuses to use my cell as input:
eq =
Columns 1 through 5
[function_handle] [function_handle] [function_handle] [function_handle] [function_handle]
Columns 6 through 10
[function_handle] [function_handle] [function_handle] [function_handle] [function_handle]
Error using lsqfcnchk (line 114)
FUN must be a function handle.
Error in fsolve (line 198)
funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);
Error in NRTL (line 36)
fsolve({eq{1}; eq{2}; eq{3}; eq{4}; eq{5}; eq{6}; eq{7}; eq{8}; eq{9}; eq{10}},[0,0 0,0 0,0 0,0 0,0 0,0 0,0
0,0 0,0],options)
Matlab code (simplified):
modelRaf(i) = (((T(1,i).*G(1,i).*x_raf(1,j)) + (T(2,i).*G(2,i).*x_raf(2,j)) ... % a very long model
for i=1:2
eq{i} = str2func(['@(T) exp(',char(modelRaf(i)),') .* ',num2str(x_raf(2,i))]
end
options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt);
fsolve(eq,[0,0 0,0],options)
The model works (used it many times). Any sugestions?

Best Answer

I think that the error message is clear: fsolve expects a single function handle, not a cell array of handles.
fsolve is for solving N equations in N unknowns. If your 10 equations are functions of the same 10 variables, then rewrite your system to a function that accepts exactly one 10-dimensional input variable x and outputs a 10-dimensional result F(x). For example,
fun = @(x)[fun1(x),...,fun10(x)]
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation