Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB The function f(x) = ln(x) is defined over the real numbers (R^1) for x el

ID: 3849988 • Letter: M

Question

MATLAB

The function f(x) = ln(x) is defined over the real numbers (R^1) for x elementof (0, infinity) (though MATLAB will report ln(0) as -Inf). Use an if selection structure to check in the inputted value of x is in the domain of f(x) according to these three cases: Case 1: If the inputted x is not in the domain of f(x) (i.e. if x elementof (-infinity, 0]), use the error command to print an error message to the Command Window. Case 2: If the inputted x is on the domain (0, 1), use the warning command to print a warning to the Command Window stating that the value of f(x) will be less than 0. Case 3: If the inputted is in the domain [1, infinity) then calculate and print to the Command Window the value of f(x) NB: The symbol elementof is a mathematical symbol that can be interpreted in English as "is an element of" For example, if x elementof [0, 5] then x is defined within (or "belongs to") that range of numbers. You will likely recall from calculus that parentheses indicate a specific interval does not include the end-point (a so called "open interval"), while brackets indicate that the interval does include the end point (a so-called "closed interval").

Explanation / Answer

Matlab code:

clear all;
fprintf('Please Enter the input value ');
x = input('');
if(x <= 0)
    fprintf('Error, input value is not in domain of ln(x) ');
elseif(0 < x && x < 1)
    fprintf('Warning, ln(x) will be less than 0 for x = %d ', x);   
else
    tmp = log(x);
    fprintf('ln(x) = %d ',tmp);  
end

Sample Output: