5 views (last 30 days)

Is there a build-in MATLAB function (way) that calculates one function implementation monotony against a true (correct, golden, reference) implementation ?

One way I’m thinking, is to check if the actual function and the reference have the same behavior on two successive inputs.

(successive as stepping; -+eps, next float, or with different granularity)

Consider 2 function implementation one to be measured and one reference:

f(x), f(x+1) and ref(x), ref(x+1)

Monotony in an strict way may be

Count inconsistencies or errors as

- ref(x) > ref(x+1) not implies f(x) > f(x+1)
- ref(x) < ref(x+1) not implies f(x) < f(x+1)
- ref(x) == ref(x+1) not implies f(x) == f(x+1)

Monotony relaxed

- ref(x) >= ref(x+1) not implies f(x) >= f(x+1)
- ref(x) <= ref(x+1) not implies f(x) <= f(x+1)

over some interval.

Example

x=1;

stp=1;

err_strict = (ref(x) > ref(x+stp)) == (f(x) > f(x+stp)) | ...

(ref(x) < ref(x+stp)) == (f(x) < f(x+stp)) | ...

(ref(x) == ref(x+stp)) == (f(x) == f(x+stp));

err_relaxed = (ref(x) >= ref(x+stp)) == (f(x) >= f(x+stp)) | ...

(ref(x) <= ref(x+stp)) == (f(x) <= f(x+stp));

disp(err_strict)

disp(err_relaxed)

function [y] = ref(x)

y = x+1;

end

function [y] = f(x)

y = single(x)+1;

end

---

Andrey Kiselnikov
on 26 Aug 2019

Edited: Andrey Kiselnikov
on 26 Aug 2019

Hi, the solution was provided here https://www.mathworks.com/matlabcentral/answers/373152-how-to-check-monotonity-of-a-vector

You can add the additional functionality to this code to perform your tasks.

a = 1:10;

isIncreasing = all(diff(a)) %or all(diff(a)>=0) if you want to allow 0 difference

Andrey Kiselnikov
on 26 Aug 2019

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/477569-function-implementation-for-monotony-check#comment_738982

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/477569-function-implementation-for-monotony-check#comment_738982

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/477569-function-implementation-for-monotony-check#comment_739327

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/477569-function-implementation-for-monotony-check#comment_739327

Sign in to comment.