Series expansion with Legendre polynomials
4 次查看(过去 30 天)
显示 更早的评论
I am trying to develop a code for the calculation of the electric potential outside of a spherical shell using series expansion and legendre polynomials.
where P_i(u) is the ith order legendre polynomial. Currently, I have this code:
clear
clc
n = input('Enter number of iiterations (n): ' );
x = .5;
y = zeros(1,n);
V = 2;
for i = 1:n
j(i) = ((legendreP(i+1,1)-legendreP(i-1,1))/(2*i+1))-(2*((legendreP(i+1,0)-legendreP(i-1,0))/(2*i+1)))+((legendreP(i+1,-1)-legendreP(i-1,-1))/(2*i+1));
y(i) = ((2i+1)/2)*V*(j(i))*legendreP(i,x);
end
print(y)
but I keep getting errors whenever I try to run it and I can't figure out why?
Any help would be appreciated
0 个评论
回答(2 个)
David Goodmanson
2019-12-2
HI James,
I won't address how one might print the result, but the mistake in the algrbra is that you should use
y(i) = ((2*i+1)/2)*V*(j(i))*legendreP(i,x);
^
rather than
y(i) = ((2i+1)/2)*V*(j(i))*legendreP(i,x);
Personally I never use i, and not often j, for a for a loop index. It can get confused up with imaginary i or j, as in this case.
0 个评论
Shraddha Kajle
2020-11-24
y(i) = ((2i+1)/2)*V*(j(i))*legendreP(i,x) and use y only at end instead of print(y)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!