MATLAB Answers

# Recursion Help ... Again...

2 views (last 30 days)
Andrew on 11 Feb 2011
Hi again everyone, I am still trying to fix my recursion... If anyone could help me with these conditions that would be great I seem to keep on getting infinite recursions.
My codes are below:
Main:
close all
clear all
clc
theta = [-pi:pi/100:pi]';
plotValues = cos(theta);
lamda = [0:(2*pi)/100:(2*pi)]';
set1 = pnm(1,1,theta,1);
set2 = pnm(3,1,theta,1);
set3 = pnm(5,1,theta,1);
set4 = pnm(7,1,theta,1);
hold on
plot(plotValues,set1,'g');
plot(plotValues,set2,'r');
plot(plotValues,set3,'m');
plot(plotValues,set4,'b');
axis([-1 1 -4 4]);
legend('n=1; m=1','n=3; m=1', 'n=5; m=1', 'n=7; m=1')
hold off
%set1non = pnm(1,1,theta,0);
%set2non = pnm(3,1,theta,0);
%set3non = pnm(5,1,theta,0);
%set4non = pnm(7,1,theta,0);
%hold off
%hold on
%figure
%plot(plotValues,set1non,'g');
%plot(plotValues,set2non,'r');
%plot(plotValues,set3non,'m');
%plot(plotValues,set4non,'b');
%axis([-1 1 -4 4]);
%legend('n=1; m=1','n=3; m=1', 'n=5; m=1', 'n=7; m=1')
RnmZ = pnm(4,0,theta,1)*cos(0*lamda);
SnmZ = pnm(4,0,theta,1)*sin(0*lamda);
PNM:
function [ values ] = pnm( n,m,theta,norm )
%What to do...
if ((n ~= 0 && m ~= 0) || (n~=0 && m==0) || (n==0 && m~=0))
if (norm == 0 )
if (m >= 2)
pnmCurrentn = 2*(m-1)*((cos(theta)/sqrt(1-cos(theta)^2)))*pnm(n,m-1,theta,norm)-(n-m+2)*(n+m-1)*pnm(n,m-2,theta,norm);
values = pnmCurrentn;
elseif (norm == 0 && m == 0)
pn = -((n-1)/n)*pnm(n-2,0,theta,norm)+((2*n-1)/n)*(cos(theta))*pnm(n-1,0,theta,norm);
values = pn;
elseif ( n >= m+2)
pnmCurrentn = ((2*n-1)/(n-m)).*cos(theta).*pnm(n-1,m,theta,norm)-((n+m-1)/(n-m))*pnm(n-2,m,theta,norm);
values = pnmCurrentn;
elseif (n==1 && m==1)
values = sin(theta);
elseif ( n==2 && m ==1 )
values = 3.*sin(theta).*cos(theta);
end
elseif ( norm == 1)
if ( n == 1 && m == 1)
w11 = sqrt(3);
values = w11*sqrt((1-cos(theta).^2))*pnm(m-1,m-1,theta,norm);
elseif (( n==0 && m==0))
values = 1;
elseif ( n == m)
wmm = sqrt((2*m-1)/(2*m));
values = wmm.*(1-cos(theta).^2).^(1/2)*pnm(m-1,m-1,theta,norm);
elseif (n~=m || (n~=0 && m==0))
wnm = sqrt(((2*n+1)*(2*n-1))/((n+m)*(n-m)));
wnmB = sqrt(((2*(n-1)+1)*(2*(n-1)-1))/(((n-1)+m)*((n-1)-m)));
values = wnm*(cos(theta).*pnm(n-1,m,theta,norm)-inv(wnmB)*pnm(n-2,m,theta,norm));
end
end
elseif ((n == 0 && m == 0))
if (norm ~= 1)
values = 1;
elseif (norm == 1)
values = 1;
end
end
end

#### 0 Comments

Sign in to comment.

### Accepted Answer

Amin Bashi on 11 Feb 2011
check this codes
elseif (norm == 0 && m == 0)
pn = -((n-1)/n)*pnm(n-2,0,theta,norm)+((2*n-1)/n)*(cos(theta))*pnm(n-1,0,theta,norm);
values = pn;
these not terminate bcos n always decrease. define a termination condition for n!

#### 2 Comments

Andrew on 11 Feb 2011
Actually, currently I'm trying to fix the section of elseif(norm == 1) I will keep that in mind when I begin doing the section with normalization I would like to finish the normalization part which I've been stuck on for a while though. If you have any information my code produces an error
Andrew on 11 Feb 2011
at this part:
elseif (n~=m || (n~=0 && m==0))
wnm = sqrt(((2*n+1)*(2*n-1))/((n+m)*(n-m)));
wnmB = sqrt(((2*(n-1)+1)*(2*(n-1)-1))/(((n-1)+m)*((n-1)-m)));
values = wnm*(cos(theta).*pnm(n-1,m,theta,norm)-inv(wnmB)*pnm(n-2,m,theta,norm));
end

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!