0
$\begingroup$

The problem says:

The first function, call it f1(), should compute the result of f(x)= x- ln(x). The second function call it f2(), should compute the result of any mathematical formula for which value is $0$ between $x=0$ and $x=1$.

According to the question the two functions are separated but I'm suppose to use the if and else statement but I'm having difficulties getting the command window to display the result.

1 Answers 1

1

The first is easy:

function y = f1(x)
y = x - log(x);
end function

MATLAB uses $\log x$ instead of $\ln x$.

For the second, we require a function to be passed to this function f2. If you know about anonymous functions, this can be achieved as follows:

function y = f2(func(x))

%func is an anonymous function, for example
%func = @(x) x^2
%or
%func = @(x) x*sin(x) - log(x)
%x is any float.

if (x <= 0) && (x >= 1)
    y = func(x);
else
    y = 'null'
end if

end function

Please read the documentation concerning if/else statements as well as anonymous functions. Also, next time please include more details in your question as well as an attempt. I also encourage you to try to rewrite this code using your own language.