function FV = compound_interest(P, r, n)
% FV = COMPOUND_INTEREST(P, r, n) computes the final value
% of principal P after n periods at rate r (per period).
% Example: FV = compound_interest(100, 0.05, 10);

% --- input checks
if ~isscalar(P) || P <= 0,      error('P must be positive scalar'); end
if ~isscalar(r) || r <= -1,     error('r > -1 required');           end
if ~isscalar(n) || n < 0 || fix(n) ~= n
    error('n must be a nonnegative integer');
end

FV = P * (1 + r)^n;
end