piecewise function with N conditions

5 次查看(过去 30 天)
I'm trying to achieve the construction of a second maximum logic function f using symbolic expressions/functions : I want f(X) to yield the 2nd highest input Xi with X = [X1, ... , XN] .
my problem resides in the N-dependant number of conditions (I figured i couldn't use "piecewise()" ?) and the fact I'm having trouble using "maxk()" in symbolic expressions/functions.
Also, I'm using symbolic functions because I need the partiate derivatives of f.
Even if you don't have the answer, ideas and leads are very welcome !
  2 个评论
Matt J
Matt J 2023-2-11
It's a bit hard to imagine why this would be useful, since you already know the result it would lead to is going to have a highly complex symbolic form at best. The point of using symbolic math is to try to arrive at simple expressions for things, because if it is not simple, non-symbolic processing is always going to be more efficient.
Walter Roberson
Walter Roberson 2023-2-11
The derivative of a min() or max() function is not continuous. Indeed, on discrete inputs, the derivative is not defined. You would need to have the inputs be expressions in free variables for a derivative to have any meaning.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-2-11
For whatever good it will do you (probably not much)
N = 5;
syms f(x) [N 1] %f will be a function
F = formula(f); %F will be an array not a function
P = perms(1:N);
FP = F(P);
pieces = [arrayfun(@(IDX) fold(@le, FP(IDX,:)), 1:size(FP,1), 'uniform', 0);
num2cell(FP(:,end-1)).'];
second_largest = piecewise(pieces{:});
dsecond = diff(second_largest, x)
dsecond = 
  3 个评论
Walter Roberson
Walter Roberson 2023-2-12
It would be possible to combine the cases together so that their was only N cases for N different variables. This way is easier though.
Walter Roberson
Walter Roberson 2023-2-12
I realized that we do not need to impose a total ordering, so the expression can be much smaller.
I think you might need an "otherwise" clause. As long as you are comparing functions or expressions with free variables, then a lot of the time f1(x) <= f2(x) might not be known, or might be difficult to prove.
N = 5;
syms f(x) [N 1] %f will be a function
F = formula(f); %F will be an array not a function
parts = cell(2,N-1,N);
for NMidx = 1 : N
nominal_max = F(NMidx);
second_candidates = setdiff(1 : N, NMidx);
for M2idx = 1 : N - 1
second_best_idx = second_candidates(M2idx);
second_best = F(second_best_idx);
smaller_candidates = F(setdiff(second_candidates, second_best_idx));
parts{1, M2idx, NMidx} = second_best <= nominal_max & fold(@and, second_best >= smaller_candidates);
parts{2, M2idx, NMidx} = second_best;
end
end
sb_piecewise = piecewise(parts{:});
dsecond = diff(sb_piecewise, x)
dsecond = 

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by