Multiple function handles in cells, sum function handles in 1 cell

5 Ansichten (letzte 30 Tage)
Oki Almas Amalia
Oki Almas Amalia am 17 Jul. 2019
Kommentiert: Oki Almas Amalia am 17 Jul. 2019
I want to multiply functions (V_fun and EW_fun in the code below) that I have stored in 2 cells.
V_init = 0.5;
for i = 3:5 % compute sojourn time for row 1
V_fun{i} = @(x) V_init*x(i-2);
EW_fun{i} = @(x) ES(1,i)*(1+0.5*(1+SCV(1,i))*0.9*V_fun{i}*ES(1,i)/(1-0.9*V_fun{i}*ES(1,i)));
V_init = @(x) V_fun{i};
fun{i} = @(x) V_fun{i}*EW_fun{i};
end
V_init = 1;
for j = 6:8 % compute sojourn time for row 2
V_fun{j} = @(x) 0.5*(1-x(1))*V_init*x(j-2);
EW_fun{j} = @(x) ES(2,j-3)*(1+0.5*(1+SCV(2,j-3))*0.9*V_fun{j}*ES(2,j-3)/(1-0.9*V_fun{j}*ES(2,j-3)));
V_init = @(x) V_fun(j);
end
V_init = @(x) 0.5*x(1)*(1-x(2))*(1-x(5));
for i = 9:10 % compute sojourn time for row 3
V_fun{i} = @(x) V_init*x(i-2);
EW_fun{i} = @(x) ES(3,i-5)*(1+0.5*(1+SCV(1,i-5))*0.9*V_fun{i}*ES(1,i-5)/(1-0.9*V_fun{i}*ES(1,i-5)));
V_init = @(x) V_fun{i};
end
V_fun{11} = @(x) 0.5*x(1)*x(2)*(1-x(3))*(1-x(6))*(1-x(8))*x(9);
EW_fun{11} = @(x) ES(4,5)*(1+0.5*(1+SCV(4,5))*0.9*V_fun{11}*ES(4,5)/(1-0.9*V_fun{11}*ES(4,5)));
% fun{11} = @(x) V_fun{11}*EW_fun{11};
V_fun{12} = @(x) 0.5*x(1)*x(2)*x(3)*x(10);
EW_fun{12} = @(x) ES(5,5)*(1+0.5*(1+SCV(5,5))*0.9*V_fun{12}*ES(5,5)/(1-0.9*V_fun{12}*ES(5,5)));
fun = cellfun(@(g,h) @(x) g(x).*f(x),V_fun,EW_fun,'UniformOutput',false);
It runs without error. But when I call 'fun{1}(1)' for example, it gave error:
Index exceeds array bounds.
Error in trial1b>@(x)g(x).*f(x)
Further, I want to sum the elements of 'fun', I tried
S = {};
for k = 3 : numel(fun)
S = @(x) S(x) + fun{k}(x);
end
and
funMin = @(x) sum([fun{:}(x)]);
but those did not work.
How to multiply the 2 functions in the 2 cells correctly? And how to sum the element of the multiplication between these 2 functions?
  7 Kommentare
Stephen23
Stephen23 am 17 Jul. 2019
Bearbeitet: Stephen23 am 17 Jul. 2019
@Oki Almas Amalia: some tips for getting started:
1. Write a script (then convert it later to a function when you need to use it with fmincon or fminsearch): https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html
2. Use arrays and indexing and vectorization: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
3. Test each line as you write it. This does not just mean running some code, but using input values and checking that the output values are correct.
4. Read the documentation for every operator, no matter how trivial you think that operator is.
5. If you have any questions, something is not working as expected, or you are not sure about something, please ask us.
Oki Almas Amalia
Oki Almas Amalia am 17 Jul. 2019
Thank you so much for the complete answers. Thanks to you the code now works well.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by