Recursion Issue

3 次查看(过去 30 天)
Andrew
Andrew 2011-2-11
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

采纳的回答

Oleg Komarov
Oleg Komarov 2011-2-11
Andrew,
1) Deleting your old questions and posting incrementing new ones with highly related content instead of editing the original post, is not a nice way to recognize others effort.
2) On line 45:
pnm(n-2,m,theta,norm)
n is decreased by 2, but you supplied n = 3 with set2 = pnm(3,1,theta,1); , i.e. on the second recursion you'll call on line 45:
pnm(-1,1,theta,norm)
which will enter an endless recursion since you don't contemplate cases with negative values of n
Oleg
  1 个评论
Andrew
Andrew 2011-2-11
Sorry about that, I'm just getting really irritated by seeing all my code. I won't do it again though.
I was wondering for the following code segment how would I be able to make it so the symbolic t can be used to input a value rather then staying symbolic. I use symbolic in order to take the derivative but I can't use it thereafter.
syms t
differ = (cos(t)^2-1)^n
differ = diff (differ);
t = theta;
values = (1/((2^n)*factorial(n)))*(1-cos(theta)^2)^(m/2)*differ

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numbers and Precision 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by