Undefined operator '-' for input arguments of type 'cell'.
이전 댓글 표시
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
채택된 답변
추가 답변 (2개)
Bob Thompson
2018년 10월 31일
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
Walter Roberson
2018년 10월 31일
편집: Walter Roberson
2018년 10월 31일
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
2018년 10월 31일
Bob Thompson
2018년 10월 31일
My best suggestion would be to solve the function within the loop, rather than leaving To{i} as a function handle.
lily genius
2018년 10월 31일
편집: lily genius
2018년 10월 31일
Bob Thompson
2018년 10월 31일
편집: Bob Thompson
2018년 10월 31일
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
2018년 10월 31일
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
Walter Roberson
2023년 12월 28일
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 ?
카테고리
도움말 센터 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!