Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a function with subscripts

Status
Not open for further replies.

dorodeus78

Computer
Apr 21, 2006
3
Hi!

I need a help, I am a beginner in matlab programming, I a m trying to write a recursive function in matlab with subscripts but I don't how I should represent it, I have just tried to represent it as follow;

Pmbar_nm = a_nm*q*t*Pmbar_n-1,m - b_nm*q^2*Pmbar_n-2,m;

where by I want the nm, n-1,m and n-2,m to be subscripts. As it is when I Run, I'm getting error message that function Pmbar_n is not defined. please tell me what to do!!

The following is the coplete code I have tried to write

function Pmbar_nm = Leg(phi,n_max,t)
%LEG copmutes the normalized associated legendre functions Pnm(t)by recurrence relation
if (nargin == 0)
phi = -6;
lambda = 39;
h = 76;
n_max = 18;
end

if n_max > 180, disp('Upper summation bound too large!'), return, end

GM = 3986004.418e8; % m^3/s^2
a_e = 6378136.46; % m
finv = 298.257223563; % flatenning

% We input geodetic coordinates phi,lambda in degrees
% We have to transform the geodetic coordinates to geocentric coordinates in radians
[X,Y,Z] = frgeod(a_e,finv,phi,lambda,h);
[lambda,phi_geoc,r] = cart2sph(X,Y,Z); % lambda and phi_geoc are now in radians
t = sin(phi_geoc); % phi_geoc is geocentric latitude

%Computing the normalized associated legendre legendre functions along the diagonal
Pbar_nm = zeros(n_max+3,1);
for n = 2:n_max
for m = 0:n
i = 2:m
u = cos (phi_geoc);
Pbar_nm = u^m*sqrt(3)*prod(sqrt((2i+1)/2i));
end
end
% building location array
loc = zeros(n_max+3,1);
for i = 1:n_max+3
loc(i)=i*(i-1)/2+1;
end

% Computing the normalisation coefficients a_nm and b_nm
a_nm = zeros((n_max+3)*(n_max+4)/2,1);
b_nm = a_nm;
for m = 1:n_max+1
for n = m+1:n_max
s = loc(n+1)+m;
a_nm(s) = sqrt(((2*n+1)*(2*n-1))/((n-m)*(n+m)));
b_nm(s) = sqrt(((2*n+1)*(n+m-1)*(n-m-1))/((2*n-3)*(n+m)*(n-m)));
end
end
%Computing the modified associated legendre function
q = a_e/r;
Pmbar_nm= zeros(n_max-4,1);
for m = n_max:-1:2
for n = n_max:-1:m
Pmbar_nm = (Pbar_nm/u^m) *10^(-280);
end
Pmbar_nm = a_nm*q*t*Pmbar_n-1,m - b_nm*q^2*Pmbar_n-2,m;
end
disp ('Computed values for the associated legendre function Pmbar_nm'),return, end

Thank you in advance for your consideration!!
 
Replies continue below

Recommended for you

If you are running an m-script, you can't put your functions in it. You must save each function to a .m file (named for the function name). Then your script will call them.

If you want to be neat and have all your functions listed in your script file, you'll have to make your script file one big function by putting a function statement as the first line.

For instance- if your script were called "test.m", the first line would be

function [output_var1, output_var2, etc] = test (input_var1, input_var2, etc)

or just

function test

This makes debugging difficult, since your script file won't store all variables after completion. It will only return what you specify in the brackets of the function declaration statement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor