Consider the function, f(x, y) = (x- a)-1 +(y- b)x xy Write MATLAB which takes v
ID: 3553899 • Letter: C
Question
Consider the function, f(x, y) = (x- a)-1 +(y- b)x xy Write MATLAB which takes values for x and y from the user and evaluates the expression. The user should be able to handle either real or complex values, so the function must be defined correctly for complex numbers (for full marks this should be able to deal with vector inputs). Errors should be thrown cases which are undefined. Write a second version in which inputs and outputs are restricted the real number line. Errors should be thrown if the user tries to enter complex values.Explanation / Answer
CODE:
A)
function [Func] = f(X,Y)
% a and b are assigned a sample value
a = 1;
b=2;
for k = 1:numel(X)
Func(k) = (X(k)-a)^(-1) +(Y(k)-b)^X(k) * sqrt(X(k)*Y(k));
if Func(k) == Inf
error('Function is not defined for the given values of variables')
end
end
B)
function [Func] = f(X,Y)
% a and b are assigned a sample value
a = 1;
b=2;
for k = 1:numel(X)
if ~(isreal(X(k)) && isreal(Y(k)))
error ('Please input Real values')
end
if X(k)*Y(k) < 0
error('Product of the variable should be positive')
end
Func(k) = (X(k)-a)^(-1) +(Y(k)-b)^X(k) * sqrt(X(k)*Y(k));
if Func(k) == Inf
error('Function is not defined for the given values of variables')
end
end