Exercise 4 Solution

Exercise 4 Solution
a = input('Enter a: ');
if a0, error('a must be positive'); end
c = input('Enter c: ');
if c0, error('c must be positive'); end
if c < a, error('c must be greater than a'); end
fprintf('The value of b is %g\n', findb(a,c));
function b = findb(a,c)
% Calculates b from a and c
b = sqrt(c^2 - a^2);
end
Mathematical Background:

This solution implements the Pythagorean theorem: a² + b² = c²

Rearranged to solve for b: b = √(c² - a²)

Constraints:

  • a > 0 (positive length)
  • c > 0 (positive length)
  • c > a (hypotenuse must be longer than other sides)
  • c² ≥ a² (to avoid imaginary numbers)
Error Handling:

The script uses MATLAB's error() function to:

  • Stop execution immediately when invalid input is detected
  • Display meaningful error messages to the user
  • Ensure mathematical validity before calculations


For more details, please contact me here.