Hi,
I need to plot the first 5 legendre polynomials. I have managed to get the expressions by writing a code for it.
>>syms x b
for n = 1:5
a(n) = 1./((2.^(n-1)).*factorial(n-1));
b = ((x*x)-1).^(n-1);
c = diff(b,n-1);
P_n = a(n).*c
end
But now I can't figure out how to write a code to calculate its value for x varying from -1 to 1 with an increment of 0.1.
Any ideas would be highly appreciated.
Thanks
Ushnik

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 29 Jan. 2014

0 Stimmen

Change
P_n = a(n).*c
to
P(n) = a(n).*c;
Now for any given n,
xvals = -1 : 0.1 : 1;
pvals = double(subs(P(n), x, xvals));

1 Kommentar

Ushnik Mukherjee
Ushnik Mukherjee am 29 Jan. 2014
Hi Walter,
I got to thinking, and I tried out this:
xvals = -1:0.1:1;
pvals1 = double(subs(P(1), x, xvals));
pvals2 = double(subs(P(2), x, xvals));
pvals3 = double(subs(P(3), x, xvals));
pvals4 = double(subs(P(4), x, xvals));
pvals5 = double(subs(P(5), x, xvals));
I have checked the answers and it looks perfect.
Thanks a ton!
Cheers
Ushnik

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by