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

MATLAB(2016) : Eval. Multivar. Function Program Chegg may not be the best place

ID: 3011821 • Letter: M

Question

MATLAB(2016) : Eval. Multivar. Function Program

Chegg may not be the best place for coding and sorry CHEGG destroyed my formatting. The Image is what I am supposed to do, and my code below does in fact work as the problem in the image expects. However it seems to return Infinite as the answer every time and I dont know why. Also if there is a more streamline way to achieve the same code I would not be surprised and I would be open to suggestions.


fprintf(' Consider f(x,y,z)=(36xy+sqrt(5z^2+xy^3)+5)(1/(x+y+z)). ')
  
%Input X
prompt = ' Input value for x: ';
x=input(prompt);
  
%Input Y
prompt = ' Input value for y: ';
y=input(prompt);
  
%Input Z
prompt = ' Input value for z: ';
z=input(prompt);
  

%Compute Answer
ans=(36*x*y+sqrt(5*z.^2+x*y.^3)+5)*(1/(x+y+z));

%Check if answer(ANS) returns a real number
if isreal(ans) == 0;
fprintf(' ERROR: Your solution contains an imaginary number. Answer=%d Please try again. ',ans')


%If first try is imaginary, try again.
while isreal(ans) == 0;
%Input X
prompt = ' Input value for x: ';
x=input(prompt);

%Input Y
prompt = ' Input value for y: ';
y=input(prompt);

%Input Z
prompt = ' Input value for z: ';
z=input(prompt);
  
ans=(36*x*y+sqrt(5*z.^2+x*y.^3)+5)*(1/(x+y+z));


%Imaginary Error Message
fprintf(' ERROR: Your solution contains an imaginary number. Answer=%d Please try again. ',ans')
  
%If non-first try is real, display answer.
if isreal(ans)==1;
fprintf(' Your solution is a real number. Input X=%d Input Y=%d Input Y=%d Answer=%d',x,y,z,ans)
end
end
%If first try is real, display answer.
else isreal(ans) == 1;
fprintf(' Your solution is a real number. Input X=%d Input Y=%d Input Y=%d Answer=%d ',x,y,z,ans)
end

Explanation / Answer

x = input('Please enter the values of x: ');
y = input('Please enter the values of y: ');
z = input('Please enter the values of z: ');

if x + y + z == 0
   disp('Value of function is not defined');
elseif x * y * y * y + 5 * z * z < 0
   disp('Value of the function is imaginary');
else
   disp(((36 * x * y) + sqrt(x * y * y * y + 5 * z * z)) / (x + y + z));
end