필터 지우기
필터 지우기

piecewise function with N conditions

조회 수: 2 (최근 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개)

카테고리

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