Undefined operator '-' for input arguments of type 'cell'.
Show older comments
Hello I am trying to optimize a function and I defined the objective function as follow:
Tp=Scope{1,1}.values(2:36,7);
T1= Scope{1,1}.values(2:36,9);
To= cell(35,1);
To{1}=70;
for i=2:36
To{i} = @(k) To{i-1} + (k)*(T1(i)- To{i-1} )
end
obj=(sum(Tp-To).^2)
end
Yet when running the function I keep getting the error Undefined operator '-' for input arguments of type 'cell'. Can you please help
Accepted Answer
More Answers (2)
Bob Thompson
on 31 Oct 2018
Assuming that the error occurs on this line:
obj=(sum(Tp-To).^2)
then the issue is exactly as the error describes. Matlab does not know how to perform a subtraction on a cell array, as cells can contain anything from scalars to character strings. You need to specify which cell contents you want to subtract.
6 Comments
Walter Roberson
on 31 Oct 2018
Edited: Walter Roberson
on 31 Oct 2018
Also
To{i} = @(k) To{i-1} + (k)*(T1(i)- To{i-1} )
When i is 2, the To{i-1} refers to 70 which is ok, and a function handle gets stored in To{2}. But when i becomes 3 then the i-1 lookup pulls back the function handle and tries to add the function handle to something.
When you are trying to optimize a function then using lots of layers of anonymous functions will be inefficient. More efficient would typically be to use the symbolic toolbox to construct the expression and then use matlabFunction, possibly with the File option so that optimization is used.
You could also consider using memoization.
lily genius
on 31 Oct 2018
Bob Thompson
on 31 Oct 2018
My best suggestion would be to solve the function within the loop, rather than leaving To{i} as a function handle.
lily genius
on 31 Oct 2018
Edited: lily genius
on 31 Oct 2018
Bob Thompson
on 31 Oct 2018
Edited: Bob Thompson
on 31 Oct 2018
Are you trying to set To as a series of different equations then, and Obj is another series of combined equation? If so, then I would suggest trying to build these as strings, rather than functions, and then converting the completed string into a function. I'm not sure if it's possible to convert a string into a function, you would have to do some research on that, but I don't know that you can combine or reactively generate a function using another function.
Walter Roberson
on 31 Oct 2018
str2fun() to convert character vector to anonymous function.
syms k;
Tp = sym('Tp', [36, 1]);
T1 = sym('T1_', [36, 1]);
To = zeros(36, 1, 'sym');
tic
To(1) = 70;
for i=2:36; To(i) = simplify(To(i-1) + (k)*(T1(i)- To(i-1) )); end
toc
tic
general_obj = simplify( sum(Tp-To).^2 );
toc
scope7 = randi([-20 20], 36, 1); %use your actual data instead
scope9 = randi([-20 20], 36, 1); %use your actual data instead
particular_obj = simplify( subs(general_obj, [Tp, T1], [scope7, scope9] ) );
dp = diff(particular_obj, k);
extrema_k = solve(dp, k);
d2p = diff(dp, k);
d2p_at_extrema = vpa(subs(d2p, extrema_k));
idx_of_mins = find(real(d2p_at_extrema) > 0 & imag(d2p_at_extrema) == 0);
k_that_give_minima = extrema_k(idx_of_mins);
1 Comment
Walter Roberson
on 28 Dec 2023
Could you point out the differences between this and the earlier posted https://www.mathworks.com/matlabcentral/answers/427184-undefined-operator-for-input-arguments-of-type-cell#comment_638520 ?
Categories
Find more on Conversion Between Symbolic and Numeric 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!