Hello, I'd like to define this function. Any help please?
1 view (last 30 days)
Steven Lord on 1 Jun 2020
A couple comments:
Your function doesn't return any values to its caller. So whatever variables you define inside the function are discarded when the function finishes its execution.
This overrides whatever the user who called your function passed into your function as input.
if x >= 1 && x <= 1.5
If x must be a scalar, this would work. But if it can be a non-scalar (usually a vector or matrix) it's not going to work.
This makes u1 a function handle. Do you want to return a function handle or do you want to return a value? [This assumes you modify your code to return u1.]
if x > 1.5 && x <= 2
if x > 2
So your function isn't going to define u1 in the case where x is greater than 0 or if x is not greater than or equal to 1?
This modifies the value of x inside this workspace then the function immediately exits and discards the copy of x in this workspace.
William Alberg on 1 Jun 2020
Im not entirely sure if this is what you are looking for.
u = @(x) func(x)
function y = func(x)
if 1 <= x && x <= 1.5
y = 3*(x-1);
elseif 1.5 < x && x <= 2
y = 3-x;
y = nan;
You can also just use func(x) direcly