piecewise function with N conditions

4 ビュー (過去 30 日間)
Matthieu
Matthieu 2023 年 2 月 10 日
コメント済み: Walter Roberson 2023 年 2 月 12 日
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 件)

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by