min and max Functions Breaking After Conversion From Symbolic to MATLAB Function
Show older comments
If I have a function whose values I want to constrain between some minimum and maximum values, I can use the min and max commands. This works for both function handles and symbolic functions. However, I need to transform a symbolic function that uses min and max into a MATLAB function handle, and it is not working. For example, in the code below, I would expect to get 1, 1, 4, 5, 5, either vertically or horizontally concatenated depending on the input. But I don’t. Can someone advise me on how to get the symbolic version of the function into a MATLAB handle in a way that will produce results consistent with the symbolic (and original) version?
Thank you in advance.
t = 0:4
orig_fun = @(x) max(1, min(5, x.^2))
syms sym_fun(x)
sym_fun(x) = sym(orig_fun)
mat_fun = matlabFunction(sym_fun)
disp(' ')
disp('In a row:')
orig_fun(t)
double(sym_fun(t))
mat_fun(t)
disp(' ')
disp('In a column:')
orig_fun(t')
double(sym_fun(t'))
mat_fun(t')
Accepted Answer
More Answers (0)
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!