How do i define the recursive function?

645 views (last 30 days)
Yeap Jia Wei
Yeap Jia Wei on 12 May 2015
Commented: Walter Roberson on 13 Apr 2022
The first three Legendre polynomials are P0(x) = 1, P1(x) = x, and P2(x) = (3x2−1)/2. There is a general recurrence formula for Legendre polynomials, by which they are defined recursively:
(n+1)Pn+1(x)−(2n+1)xPn(x)+nPn−1(x)=0.
Define a recursive function p(n,x) to generate Legendre polynomials, given the form of P0 and P1. Use your function to compute p(2,x) for a few values of x, and compare your results with those using the analytic form of P2(x) given above.

Answers (3)

Celeste Dodge
Celeste Dodge on 17 Sep 2019
wow poeple are mean
  4 Comments
irvin rynning
irvin rynning on 13 Dec 2021
i get the sense i'm on stackoverflow, not a professional forum, with responses such as yours
Walter Roberson
Walter Roberson on 15 Dec 2021
The time and attention of the volunteers (unpaid!) is a scarce resource. Over 140 new questions are asked every day; in the March time-frame it peaks over 250 new questions every day. Some of the questions take a few seconds to answer; some of them take a couple of days of effort to answer. It is typical that any given question takes at least two days to get through, back and forths with the person who asked the question. So any given volunteer is pretty much having to pick through over 275 active questions every day.
The volunteers have to decide: do they give detailed attention to the people who are actually trying to solve MATLAB problems and are likely to benefit from mentoring? Or do they give detailed attention to the people who copy and paste homework problems and do not even attempt to solve the problem? Because I can assure you, the volunteers do not have time to do both. I am already working on over 50 different Questions every day.

Sign in to comment.


Walter Roberson
Walter Roberson on 12 May 2015
Edited: Walter Roberson on 12 Mar 2021
This is not a MATLAB question. The relevant MATLAB question is "How do I create recursive functions", and the answer to that is that you just have the function call itself. The only tricks are to make sure you call with different arguments or else you infinite loop; and to make sure you have an ending condition.
Example:
function r = myfactorial(n)
if n <= 0
r = 1;
else
r = n * myfactorial(n-1);
end
end
Everything else about your question is "Do my homework for me!"
  2 Comments
rajesh mishra
rajesh mishra on 29 Aug 2021
No offence but your answer is completly useless, what the person is asking is that how can we create array of function and evalulate that function for particular value of x and n ,
Walter Roberson
Walter Roberson on 15 Dec 2021
You are mistaken. The task is specifically to create a recursive function. It says so in the problem, "Define a recursive function p(n,x) to generate Legendre polynomials, given the form of P0 and P1. " Not an array of functions.
There are optimizations potentially available if you store functions... but in practice not really. The optimizations more come in if you can build up formulas and store them, such as is possible using the Symbolic Toolbox. Working out the formulas as pure text in order to generate functions and store them, gets a bit nasty. (Hmmm, I wonder if it can be phrased in terms of convolutions and filters ?)

Sign in to comment.


Torsten
Torsten on 12 May 2015
  2 Comments
Walter Roberson
Walter Roberson on 13 Apr 2022
Then we recommend that you learn how to use the Google search engine. The Advanced Search makes it much easier to locate specific content, and a number of features of the advanced search can be called up from the command line if you know the right codes.
For example,
recursive function site:mathworks.com
would restrict the search to mathworks.com

Sign in to comment.

Categories

Find more on Startup and Shutdown 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!