is it possible to differentiate max function?

Hi,
Is it possible with the basic matlab functions to numerically differentiate a max function? This is what I tried, but did not work: 'Error using symengine, Input arguments must be convertible to floating-point numbers.'
r_par = 100;
R = 3;
P = @(x1,x2) r_par/2*(max(0,x1^2 + x2^2 - R^2))^2
gP = gradient(P, [x1, x2]);
gF = matlabFunction(gP);
x = [1 2];
x1 = x(1);
x2 = x(2);
ans = P(x1,x2)

Respuestas (1)

Walter Roberson
Walter Roberson el 3 de Dic. de 2018
Editada: Walter Roberson el 3 de Dic. de 2018

1 voto

max() is not defined for symbolic . recode in terms of piecewise
Max = @(aa,bb) piecewise(aa>bb,aa,bb)
Note that matlabFunction can only handle piecewise if you tell it to write to aa file and even then the result will not be vectorized .

6 comentarios

Alec Jacobson
Alec Jacobson el 17 de Feb. de 2023
Nice!
btw, is there a reason why symbolic doesn't handle max/min? It seems to handle abs fine.
What would you expect this code to return for M?
syms x y
M = max(x, y)
Which is larger, x or y? Does that question have an answer in the general case?
John D'Errico
John D'Errico el 17 de Feb. de 2023
Editada: John D'Errico el 17 de Feb. de 2023
abs is well posed in a complex setting. So if the argument x is complex, abs(x) is fully defined.
Is that true of max? Not really. There is not a simple ordering that can be imposed on complex numbers.
Admittedly, MATLAB does define max for complex inputs when they are doubles. Max seems to use the magnitude of the number as the test, whereas I recall that an inequality applies to the real part.
x = 1 + 5i
x = 1.0000 + 5.0000i
y = 2-i
y = 2.0000 - 1.0000i
x < y
ans = logical
1
max(x,y)
ans = 1.0000 + 5.0000i
However, you need to recognize the ambiguity this leaves. We see above that x<y, yet max(x,y)==x. It would be my guess they correctly decided to leave the symbolic TB max-less.
Steven Lord
Steven Lord el 17 de Feb. de 2023
See the description of the ComparisonMethod comparison parameter introduced in release R2021b on the max documentation page. The 'auto' value for that parameter (which is the default) is "For a numeric input array A, compare elements by real(A) when A is real, and by abs(A) when A is complex." But you can change that with the 'real' or 'abs' values.
Alec Jacobson
Alec Jacobson el 17 de Feb. de 2023
Editada: Alec Jacobson el 17 de Feb. de 2023
yeah, I wouldn't mind needing to specify the ComparisonMethod or if it determined that based on assumptions to deal with real vs. complex input (e.g., through an error for complex).
Meanwhile, the frustrating thing I'm finding about piecewise is that it gets confused with float inputs:
a = 1;
b = 2;
piecewise(a<b,a,b)
Maybe it's only defined for symbolic? That makes it difficult to prototype mixing numeric and symbolic code (which I see as a huge advantage of matlab vs. maple etc.)
In the time since I wrote the original answer, MATLAB added a symbolic max() and min() as place-holders that wait until the question is decidable.
syms aa bb
max(aa, bb)
ans = 
So now you can
syms x1 x2
r_par = 100;
R = 3;
P = @(x1,x2) r_par/2*(max(0,x1^2 + x2^2 - R^2))^2
P = function_handle with value:
@(x1,x2)r_par/2*(max(0,x1^2+x2^2-R^2))^2
But you cannot call gradient() because that looks for numeric inputs, so you call diff()
gP = [diff(P, x1), diff(P, x2)]
gP = 
and then you can see that matlabFunction gets confused and thinks that x1 and x2 are the stride counts in numeric diff calls...
matlabFunction(gP)
ans = function_handle with value:
@(x1,x2)[diff(max([0.0,x1.^2+x2.^2-9.0],[],2,'omitnan'),x1).*max([0.0,x1.^2+x2.^2-9.0],[],2,'omitnan').*1.0e+2,diff(max([0.0,x1.^2+x2.^2-9.0],[],2,'omitnan'),x2).*max([0.0,x1.^2+x2.^2-9.0],[],2,'omitnan').*1.0e+2]

Iniciar sesión para comentar.

Productos

Versión

R2018a

Preguntada:

el 3 de Dic. de 2018

Editada:

el 17 de Feb. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by