%Calculates the Schmidt Normalized Associated Legendre Function (SNALF) for a certain angle (theta) and degree (deg)      
%
%Initial version: Niklas Edberg, Swedish Institute of Space Physics, Uppsala, Oct 2005

function [Pnm dPnm] = snalf(theta,deg)

    for n = 1:deg
        leg = legendre(n,cos(theta),'sch'); %Here might something be wrong... 
        for m = 1:n+1
            Pnm(n,m) = leg(m);
            if n==1
                dPnm(1,1) = -sqrt(2)*sin(theta);
                dPnm(1,2) = cos(theta);
            else
                dPnm(n,m) = (n*cos(theta)*Pnm(n,m)-(n+m-1)*Pnm(n-1,m))/sqrt(1-cos(theta)^2); %The derivative of Pnm(cos(theta)) with respect to theta.
            end
        end
    end
    
    