Matlab question In this exercise, you will be creating a function trough_error t
ID: 3886959 • Letter: M
Question
Matlab question
In this exercise, you will be creating a function trough_error that will assign a message based on the input vector y. Note that we can assign a message by typing: message = 'type message here' Function Process: The initial value for y and the final value for y should be equal within a tolerance of 10^-3. If they are not, the message should say 'Warning: Trough Area is invalid since shape does not close properly'. If the final y value is within tolerance, you should then check whether any of the other values in y (excluding the initial and final values) are greater than the initial value for y. If this condition is met, the message should be 'Warning: Trough Area will be incorrect since y exceeds y0 at a certain point'. If neither of these conditions are met, the message should be 'Trough Area is valid'. Potentially Useful Functions: if, elseif, else, abs, max, end Function Template function message = trough_error(y) %insert code end Submitted file: function message = trough_error(y) %insert code endExplanation / Answer
trough_error.m file
function message = trough_error(y)
% finding tolerance < 10^-3
if(abs(y(1)-y(end)) < 1e-3*eps(min(abs(y(1)),abs(y(end)))))
message = 'Warning: Trough Area is invalid since shape does not close properly.';
% check if any element in vector y is greater than first element
elseif(length(y(y>y(1)))>1)
message = 'Warning. Trough Area will be incorrect since y exceeds y0 at a certain point.'
else
message = 'Trough Area is valid.'
end
end
main.m file
y = [5 4 3 2 1];
trough_error(y);
%sample output
%