## How to return a vector output when a vector input is given to a function_handle representing a constant function?

I want to define a function handle that represents the constant function , pass an n-length vector input, and get out an n-length vector whose values are . But there’s a catch. My code is like:

% Usual Case

syms x; syms t;

u = symfun(x^2,x);

u_x = diff(u,x);

u_x = matlabFunction(u_x);

u_x([0 1 2])

ans = [0 2 4];

So, I input a vector, and I get back a vector, which I would consider the "expected" behavior. But it might happen that the input function happens to have a constant derivative, like

% Exceptional Case

syms x; syms t;

u = symfun(x,x);

u_x = diff(u,x);

u_x = matlabFunction(u_x);

u_x([0 1 2])

ans = 1

Now, I put in a vector and suddenly MATLAB returns a scalar.

So if I only ever had the exceptional case, I would just do something like u_x(0) * ones(1,3) to get my desired vector of constants. But actually, I almost never have the exceptional case; most of the time the input function does not have a constant derivative, and so inputing a vector into the differentiated function returns a vector already.

What I want is for MATLAB to do what I would consider the obvious thing: If I input a vector, I get back a vector, even if the function_handle represents a constant function…how do I do?

Thanks!I want to define a function handle that represents the constant function , pass an n-length vector input, and get out an n-length vector whose values are . But there’s a catch. My code is like:

% Usual Case

syms x; syms t;

u = symfun(x^2,x);

u_x = diff(u,x);

u_x = matlabFunction(u_x);

u_x([0 1 2])

ans = [0 2 4];

So, I input a vector, and I get back a vector, which I would consider the "expected" behavior. But it might happen that the input function happens to have a constant derivative, like

% Exceptional Case

syms x; syms t;

u = symfun(x,x);

u_x = diff(u,x);

u_x = matlabFunction(u_x);

u_x([0 1 2])

ans = 1

Now, I put in a vector and suddenly MATLAB returns a scalar.

So if I only ever had the exceptional case, I would just do something like u_x(0) * ones(1,3) to get my desired vector of constants. But actually, I almost never have the exceptional case; most of the time the input function does not have a constant derivative, and so inputing a vector into the differentiated function returns a vector already.

What I want is for MATLAB to do what I would consider the obvious thing: If I input a vector, I get back a vector, even if the function_handle represents a constant function…how do I do?

Thanks! I want to define a function handle that represents the constant function , pass an n-length vector input, and get out an n-length vector whose values are . But there’s a catch. My code is like:

% Usual Case

syms x; syms t;

u = symfun(x^2,x);

u_x = diff(u,x);

u_x = matlabFunction(u_x);

u_x([0 1 2])

ans = [0 2 4];

So, I input a vector, and I get back a vector, which I would consider the "expected" behavior. But it might happen that the input function happens to have a constant derivative, like

% Exceptional Case

syms x; syms t;

u = symfun(x,x);

u_x = diff(u,x);

u_x = matlabFunction(u_x);

u_x([0 1 2])

ans = 1

Now, I put in a vector and suddenly MATLAB returns a scalar.

So if I only ever had the exceptional case, I would just do something like u_x(0) * ones(1,3) to get my desired vector of constants. But actually, I almost never have the exceptional case; most of the time the input function does not have a constant derivative, and so inputing a vector into the differentiated function returns a vector already.

What I want is for MATLAB to do what I would consider the obvious thing: If I input a vector, I get back a vector, even if the function_handle represents a constant function…how do I do?

Thanks! pde, symbolicfunctions, functionhandles MATLAB Answers — New Questions