piecewise function with N conditions

4 Ansichten (letzte 30 Tage)
Matthieu
Matthieu am 10 Feb. 2023
Kommentiert: Walter Roberson am 12 Feb. 2023
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 Kommentare
Matt J
Matt J am 11 Feb. 2023
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 am 11 Feb. 2023
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Feb. 2023
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 Kommentare
Walter Roberson
Walter Roberson am 12 Feb. 2023
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 am 12 Feb. 2023
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 = 

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by