Help with symbolic variables

Sorry, reposting this question again to see if any of you kind folk can help.
Hello,
I have the following to calculate associated legendre polynomials (in terms of angles: http://en.wikipedia.org/wiki/Associated_legendre_polynomials#Reparameterization_in_terms_of_angles)
%Calculating associated legendre polynomials %Using Rodriguez formula
syms z theta
m = 0;
l = 0;
for i = 1:10
Pl(i,:) = (inv((2^l)*factorial(l)))*diff(((z^2 - 1)^l),l);
Plm(i,:) = ((-1)^m)*((sin(theta))^m)*diff(Pl(i,:),m);
Plm(i,:) = subs(Plm(i,:), z, cos(theta));
l = l + 1;
end
I am struggling to find the way to replace the symbolic variable theta with a vector containing numerical values.
Any help would be greatly appreciated.
Tom

 采纳的回答

Mischa Kim
Mischa Kim 2014-3-7
编辑:Mischa Kim 2014-3-7
Tom, do you mean something like
Plm_vec(i,:) = subs(Plm(i,:),{theta},{[pi/4, pi/5, pi/6]});

6 个评论

Tom
Tom 2014-3-7
编辑:Tom 2014-3-7
I think that's along the right lines, Mischa.
If you run the code, the first few results are:
Plm(1,:) = 1
Plm(2,:) = cos(theta)
Plm(3,:) = (3*cos(theta)^2)/2 - 1/2
Later on in the program I want to substitute'theta' in these results with vectors containing numerical values.
Do you see what I mean?
Sure. Just add my code line as the last command in your for -loop. You should get 3 values for each row of Plm.
Oh I see. That actually just returns a single value...?
But also is it not possible to utilise the results outside of the for loop? As that is ideally what I would like.
Thanks for your help.
Sorry you're right, there are 3 values for each line.
Copy-paste-execute the code below in the MATLAB command window:
clear all
syms z theta
m = 0;
l = 0;
for i = 1:10
Pl(i,:) = (inv((2^l)*factorial(l)))*diff(((z^2 - 1)^l),l);
Plm(i,:) = ((-1)^m)*((sin(theta))^m)*diff(Pl(i,:),m);
Plm(i,:) = subs(Plm(i,:), z, cos(theta)); l = l + 1;
Plm_vec(i,:) = subs(Plm(i,:),{theta}, {[pi/4, pi/5, pi/6]});
end
disp(Plm_vec)
you should get a 10-by-3 array of values. And, yes, you can evaluate outside the loop. E.g., following the loop you could do
subs(Plm(3),{theta},[0:0.1:0.5])
Perfect, thanks a lot!
Sorry to keep bothering you with silly questions. But could you also explain why I am unable to use cosd and sind in the symbolic calculations?

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by