You're doing it while q < 1. But when you get to a situation where q >= 1, it quits the loop. However, you're left with the values that are too big. You need to index the numbers and then when you bail out, take the prior one. Here's one way of many:
loopCounter = 2;
A(1) = 0; % Initialize
x(1) = A*rect((t-Tp/2)/Tp);
q(1) = trapz(x.^2)*periodo;
while (q<1)
A(loopCounter) = A(loopCounter-1) + 0.1;
x(loopCounter) = A(loopCounter-1)*rect((t-Tp/2)/Tp);
q(loopCounter) = trapz(x(loopCounter-1).^2)*periodo;
if q(loopCounter) >= 1
break; % Don't increment loop counter.
end
loopCounter = loopCounter + 1;
end
finalA = A(loopCounter - 1);
finalx = x(loopCounter - 1);
finalq = q(loopCounter - 1);