Is the purpose of variable pitch propellers to maintain an optimal angle of attack?
What you know about variable pitch propellers is generally correct. If you make a slight change, your flight instructor would not have had any reason to object: They keep propeller blades near their optimum angle of attack across a wide speed range.

As the distance from the hub increases, the blade section gains circumferential speed while being in the same forward speed as all other sections. Therefore, the twist of the propeller blade should change linearly from the hub to the tip. If you twist the full blade at its root, you add a constant change to each cross section, so at high speed the root of the blade sees too high an angle of attack and the tip has too little angle of attack. Normally, the section at approximately 75% of propeller span is kept at its ideal angle for the best overall compromise. That is good enough for practical use.

Ideally, the propeller would spin faster with increasing flight speed, but that would require a gearbox and switching gears with the limited speed ranges of combustion engines, would deliver little thrust at the beginning of the take-off run and at high speed you would run into compressibility problems at the tips much sooner - practical high speed propellers spin more slowly than typical low-speed propellers.

If you look at the efficiency chart of a three-bladed variable pitch propeller below, you will see that efficiency peaks for one speed (given as an advance ratio in the plot below) and one pitch angle. This is when the full blade is flown at the optimum angle of attack for each section. This optimum would shift to higher speeds for less twist and lower speeds for more twist. Operating the propeller away from its design point incurs small losses since not all of the blade is exactly at its optimum angle of attack, but if you compare the overall efficiency with that of a single pitch setting, the advantage of variable pitch becomes obvious.



Efficiency chart of a variable pitch propeller. Source: McCormick B.W. Aerodynamics, Aeronautics &amp;Flight Mechanics. John Wiley &amp; Sons, Inc., 1979.