# x=sym(rand(round(randn))); min(x); %proposed method for min

Discussion in 'MATLAB' started by John Creighton, Jun 12, 2004.

1. ### John CreightonGuest

Bellow is the method I propose MATLAB should adopt for min:

Not that this will not work in MATLAB 6.1.0.450 (R12.1) if Y is not
empty because in this MATLAB you can not have objects of type sym that
are greater then 2 dimensions. When mathworks changes this (if they
haven't already, it should work in all cases).

function Z=min(X,Y,I)
% MIN Smallest component.
% For vectors, MIN(X) is the smallest element in X. For matrices,
% MIN(X) is a row vector containing the minimum element from each
% column. For N-D arrays, MIN(X) operates along the first
% non-singleton dimension.
%
% MIN(X,Y) returns an array the same size as X and Y with the
% smallest elements taken from X or Y. Either one can be a scalar.
%
% Example: If X = [2 8 4 then min(X,[],1) is [2 3 4],
% 7 3 9]
%
% min(X,[],2) is [2 and min(X,5) is [2 5 4
% 3], 5 3 5].
%
% See also MAX, MEDIAN, MEAN, SORT.
%
% Overloaded methods
% help SYM/min.m
%
% Note that future versions well check to see if x and y are real and
if they
% are not the magnitude will be taken
if (nargin>1)&(length(Y)>0);
Z=min(cat(length(size(X)),X,Y),[],length(size(X)));
else
S=size(X)
if nargin~=3;
I=find(S>1);
if length(I)==0; I=1; else I=I(1); end
end
S2=S; S2(I)=1; Z=sym(zeros(S2));

DI1=prod(S(1 I-1)));
DI2=prod(S(1 I)));
i=1; j=1;
while j<=prod(S2)
Z(j)=minI(X(i I1 i+DI1*(S(I)-1))));
if (mod(i-1,DI2)+1)==DI1;
i=i+S(I);
end
if DI1~=1; i=i+1; end
j=j+1
end
end
function y=minI(x);
y=cell(1,2*length(x)-1);
for i=1:length(x)
y(2*i-1)={char(x(i))};
end
y(2:2:length(y))={','};
y=sym(maple(['min(' y{:} ')']));

John Creighton, Jun 12, 2004
1. ### Advertisements

2. ### Steven LordGuest

*snip*

This is for sym objects from the Symbolic Math Toolbox ... okay, but what
would you expect to be the output of these calls to MIN (before running the
function on these inputs, please), does your function return the output you
expect, and (most importantly) do you believe most people would expect the
same thing you do? [The last part of that question is the kicker; I almost
guarantee not everyone will expect the same thing you do, and I'm sure if we
implemented this we'd hear from that segment of the population that
doesn't.]

syms x y
min([x y])
min([x x^2])
min([x sin(x)])
min([x tan(x)])

Steven Lord, Jun 14, 2004
1. ### Advertisements

3. ### John CreightonGuest

*snip*
I know it won't give good results because I only considered the cases
where x and y are real.
I do believe most people would expect the same output I would if I had
implemented all the cases.
Maybe this is true. I am just a student studying my masters in
electrical engineering. So I do not have experience working with a
software design team or dealing with suggestions or bug reporting from
the general public. But I suppose there are two main issues. The first
issue is to try to make it as consistent as possible with the original
MATLAB min function. The second issue is to thoroughly document any
differences, so the public knows what to expect.

For now I'll do the first two. If you want I will do more later.
For all the following calls I would expect the symbolic variable
returned to be one dimensional with the following string
representation.

%If we make the assumptions
% additionally(x,real); additionally(y,real) ;
%I'd expect
min(x,y)
%If we make either assumptions:
% additionally (0 < abs(x-conjugate(x))); additionally(0 <
abs(x-conjugate(x)));
%I'd expect
min(abs(x),abs(y)); % Not yet implemented
%Otherwise, I'd expect :
PIECEWISE([real, (0 < abs(x- conjugate (x))) or (0 < abs(y- conjugate
(y)))],[ min(abs(x),abs(y)),min(x,y)]) %Not yet implemented
% Note. In MATLAB 6.1.0.450 (R12.1) I had some problem with piecewise
functions.

%With the assumptions
%addionally(x,real), I'd expect
min(x,x^2)
%unless x has the assumption
%additionally(abs(x)>=1) then I'd expect
x
%Otherwise I'd expect:
PIECEWISE([real, (0 < abs(x- conjugate (x))) or (0 < abs(y- conjugate
(y)))],[ min(abs(x),abs(x^2)),min(x,x^2)]) %Not yet implemented

Obviously I haven't implemented all these cases yet. One could check
to see if all of the variables were real and if not throw a not yet
implemented error. I don't know if mathworks considers that good or
bad practice. I don't really have much incentive now to work out all
the cases in my code (although I might do it later anyway). I
implemented the first case because I thought I might want to find the
describing function for a limiter symbolically.

John Creighton, Jun 14, 2004
4. ### Steven LordGuest

*snip*

I understand. One of the big sticking points I see with implementing the
relational operators or the min/max functions for symbolic objects is that
while MATLAB's truth system (logicals) is two-valued, true and false,
Maple's is three-valued -- true, false, and FAIL. It's the FAIL case that
throws the monkey wrench into things and would cause the most
discussion/confusion/frustration if we implemented these functions for sym
objects.

Anyway, I've passed this along as an enhancement request to the Symbolic
Math Toolbox developer.

Steven Lord, Jun 14, 2004

Ask a Question

## Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.