How to define a piecewise anonymous function

Hello everyone,
the example code
syms x
continuous_function = x^2+x;
matlabFunction(continuous_function,'Vars',x)
gives me the anonymous function
@(x)x+x.^2
I would like a similar result for piecewise functions. However, the code
syms x
piecewise_function = piecewise( 0<x<1, x, 'otherwiseVal', 0 );
matlabFunction(piecewise_function,'Vars',x)
does not work.
Thanks for any help!

 Respuesta aceptada

Star Strider
Star Strider el 31 de Mayo de 2019
You need to use ‘logical indexing’:
piecewise_function = @(x) (x.^2+x) .* ((0<x) & (x<1));
x = linspace(-1, 2);
figure
plot(x, piecewise_function(x))
producing:
How to define a piecewise anonymous function - 2019 05 31.png

11 comentarios

Thank you!
Do I understand you correctly that there is no function that converts
piecewise_function = piecewise( 0<x<1, x^2+x, 'otherwiseVal', 0 );
to
@(x) (x.^2+x) .* ((0<x) & (x<1))
Yes.
This code:
syms x
piecewise_function = piecewise( 0<x<1, x^2+x, 'otherwiseVal', 0 );
matlabFunction(piecewise_function)
throws this error:
Error using symengine
Unable to generate code for piecewise for use in anonymous functions.
(and some others).
The construction I use here appears to be the only way. Various such expressions can be combined in one function to create piecewise functions across various intervals, for example:
piecewise_function = @(x) (x.^2+x) .* ((0<x) & (x<1)) + (x+sqrt(x)).*((x>=1) & (x<1.5));
So all is not lost.
Stephan
Stephan el 31 de Mayo de 2019
Thank you for this useful remark!
Star Strider
Star Strider el 31 de Mayo de 2019
As always, my pleasure!
ElPerroVerde
ElPerroVerde el 16 de Mayo de 2020
THANK YOU SO MUCH!!!!!!!!!!!! I've been looking for something like this for months!!!
Star Strider
Star Strider el 16 de Mayo de 2020
ElPerroVerde — My pleasure!
A Vote would be appreciated!
.
Roy Goodman
Roy Goodman el 29 de Oct. de 2025
How can we modify this to take the value NaN outside the interval [0 1]?
Roy Goodman
Roy Goodman el 29 de Oct. de 2025
MATLAB Copilot returns code that fails!
Torsten
Torsten el 29 de Oct. de 2025
Editada: Torsten el 29 de Oct. de 2025
piecewise_function = @(x) (x.^2+x) + 0./((0<=x) & (x<=1));
x = linspace(-1, 2);
figure
plot(x, piecewise_function(x))
syms x
f = piecewise((0<=x) & (x<=1),x.^2+x,nan)
f = 
piecewise_function = matlabFunction(f, 'file', 'test_piecewise.m')
piecewise_function = function_handle with value:
@test_piecewise
dbtype test_piecewise
1 function f = test_piecewise(x) 2 %TEST_PIECEWISE 3 % F = TEST_PIECEWISE(X) 4 5 % This function was generated by the Symbolic Math Toolbox version 25.2. 6 % 29-Oct-2025 22:14:42 7 8 t2 = ((0.0 <= x) & (x <= 1.0)); 9 if ~all(cellfun(@isscalar,{t2})) 10 error(message('symbolic:sym:matlabFunction:ConditionsMustBeScalar')); 11 end 12 if (t2) 13 f = x+x.^2; 14 else 15 f = NaN; 16 end 17 end
x = linspace(-1, 2);
plot(x, arrayfun(piecewise_function, x))
xline([-1 2])
Roy Goodman
Roy Goodman el 30 de Oct. de 2025
Thanks!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 16 de Mayo de 2020

0 votos

If you use matlabFunction with 'file' option, then it will convert piecewise() into if/else in the generated code.
Note: because it uses if/else instead of logical indexing, the generated code will not be vectorized on that variable.

Preguntada:

el 31 de Mayo de 2019

Comentada:

el 30 de Oct. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by