Series expansion with Legendre polynomials

4 views (last 30 days)
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

Answers (2)

David Goodmanson
David Goodmanson on 2 Dec 2019
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.

Shraddha Kajle
Shraddha Kajle on 24 Nov 2020
y(i) = ((2i+1)/2)*V*(j(i))*legendreP(i,x) and use y only at end instead of print(y)

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!