It is not obvious that is it illegal in Washington state. Everett WA has local ordinances against "lewd conduct" (there are versions of this at the state level and in most municipalities). Having sex and masturbation are included in the class of "lewd acts", and are also included in "sexual conduct". An activity is "obscene" if three things are true. First,
the average person, applying contemporary community standards, would
find, when considered as a whole, appeals to the prurient interest
when considered as a whole, and in the context in which it is used,
lacks serious literary, artistic, political or scientific value.
I think having sex or masturbating could pass these two tests. The third condition is that the act
explicitly depicts or describes patently offensive representations or
descriptions of... [sex, masturbation, or excretion]
The prohibition is more narrow:
A person is guilty of lewd conduct if he or she intentionally performs
any lewd act in a public place or under circumstances where such act
is likely to be observed by any member of the public.
If lewd conduct were completely illegal, you could not excrete or have sex withing the city limits. Now we have to turn to the definition of "public place":
an area generally visible to public view, and includes streets,
sidewalks, bridges, alleys, plazas, parks, driveways, parking lots,
automobiles (whether moving or not), buildings open to the general
public, including those which serve food or drink or provide
entertainment and the doorways and entrances to buildings or dwellings
and the grounds enclosing them, and businesses contained in structures
which can serve customers who remain in their vehicles, by means of a
Focusing not on the probable intent but on the words, it is primarily defined as "an area generally visible to public view". Your house qua building is probably generally visible to public view, as is a public toilet or hotel. The inside of your bedroom is probably not generally visible to public view, nor is the inside of a toilet stall. While the building is probably a public place, a closed stall within the building does not meet the definition (nor does a hotel room). It might however qualify under the clause "or under circumstances where such act is likely to be observed by any member of the public". The statute does not give a definition of "observe", but under ordinary language interpretation, observation may be seeing or hearing. Silent sex, masturbation or defecation might not qualify as being public.
Obviously, excretion in a stall of a public bathroom cannot be a lewd act, presumably because the average person does not generally consider ordinary excretion as appealing to the prurient interest: but there could be contexts where it does.
Another avenue for prosecution is the Indecent Exposure state law which is when one
intentionally makes any open and obscene exposure of his or her person
or the person of another knowing that such conduct is likely to cause
reasonable affront or alarm.
It is totally non-obvious that sex or masturbation in a toilet stall is "open". There is a slippery slope regarding quiet sex or masturbation w.r.t. knowing that the conduct is likely to cause reasonable affront or alarm.
Because the contexts where sex and masturbation are not clearly spelled out by statutory law, the matter would depend on how courts had interpreted previous cases. There have been arrests in Washington of people having bathroom sex, but I don't know if anyone has ever or recently-enough been convicted for having quiet sex out of view in a toilet stall, or similar place. There is a potentially applicable case, Seattle v. Johnson, 58 Wn. App. 64, which seems to involve public sex, and the conviction was overturned because the complaint was defective, in not including the element "that the defendant must know 'that such conduct is likely to cause reasonable affront or alarm'". It is not clear from the appeal what the act actually was – it probably was for public nudity. There is also an decision by the state appeal court division 3 (not publicly available) in Spokane v. Ismail which, in connection with a charge of public urination declares that "A toilet stall is not a public place. The center of Riverfront Park during the lunch hour is a public place", in connection with an ordinance just like the Everett one against public lewd acts.