# how to get the function as the sum of other functions?

91 ビュー (過去 30 日間)
Oleksandr Oleksandras 2015 年 2 月 17 日
I have a function fitfun=@(x)energy(i)-laa(i)*(1-exp(-x)), where x is unknown, energy and laa are arrays of values. I need to obtain expression for the summ of the functions: sumf=@(x)energy(1)-laa(1)*(1-exp(-x))+energy(2)-laa(2)*(1-exp(-x))...energy(N)-laa(N)*(1-exp(-x)). Next I need to minimize the "sumf" i.e. to find "x" which would give the value for total_function ~0. Fminsearch works perfectly for the individual function "fitfun" but not for the total sumf. Here is the script:
>
>syms x;
>> for i=1:N
function=@(x)energy(i)-laa(i)*(1-exp(-x))
sumf=@(x)sumf(x)+fitfun(x)
end
>> [x,fval] = fminsearch(sumf,)
Subscript indices must either be real positive integers or logicals.
Error in @(x)sumf(x)+fitfun(x)
Error in @(x)sumf(x)+fitfun(x)
Error in @(x)sumf(x)+fitfun(x)
Error in @(x)sumf(x)+fitfun(x)
Error in @(x)sumf(x)+fitfun(x)
Error in @(x)sumf(x)+fitfun(x)
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
>>
Thank you

サインインしてコメントする。

### 採用された回答

Greig 2015 年 2 月 18 日
The easiest way would be to set it up your function as something like...
sumf = @(x) sum(energy - laa .* (1-exp(-x)) )
Then call fminsearch. This also saves on the loop, but make sure that your dimensions of "energy" and "laa" are consistent.
Also, depending on your function values, you may want to minimise the sum of the squares. If the sum is <0 then you will end up far from zero.
##### 3 件のコメント表示非表示 2 件の古いコメント
Oleksandr Oleksandras 2015 年 3 月 2 日
yes it works fine

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!