1 | function ypred = polyvaln(polymodel,indepvar) |
---|
2 | % polyvaln: evaluates a polynomial model as a function of its variables |
---|
3 | % usage: ypred = polyvaln(polymodel,indepvar) |
---|
4 | % |
---|
5 | % arguments: (input) |
---|
6 | % indepvar - (n x p) array of independent variables as columns |
---|
7 | % n is the number of data points to evaluate |
---|
8 | % p is the dimension of the independent variable space |
---|
9 | % |
---|
10 | % IF n == 1, then I will assume there is only a |
---|
11 | % single independent variable. |
---|
12 | % |
---|
13 | % polymodel - A structure containing a regression model from polyfitn |
---|
14 | % polymodel.ModelTerms = list of terms in the model |
---|
15 | % polymodel.Coefficients = regression coefficients |
---|
16 | % |
---|
17 | % Note: A polymodel can be evaluated for any set of |
---|
18 | % values with the function polyvaln. However, if you |
---|
19 | % wish to manipulate the result symbolically using my |
---|
20 | % own sympoly tools, this structure should be converted |
---|
21 | % to a sympoly using the function polyn2sympoly. |
---|
22 | % |
---|
23 | % Arguments: (output) |
---|
24 | % ypred - nx1 vector of predictions through the model. |
---|
25 | % |
---|
26 | % |
---|
27 | % See also: polyfitn, polyfit, polyval, polyn2sympoly, sympoly |
---|
28 | % |
---|
29 | % Author: John D'Errico |
---|
30 | % Release: 1.0 |
---|
31 | % Release date: 2/19/06 |
---|
32 | |
---|
33 | % get the size of indepvar |
---|
34 | [n,p] = size(indepvar); |
---|
35 | if (n == 1) && (size(polymodel.ModelTerms,2)==1) |
---|
36 | indepvar = indepvar'; |
---|
37 | [n,p] = size(indepvar); |
---|
38 | elseif (size(polymodel.ModelTerms,2)~=p) |
---|
39 | error 'Size of indepvar array and this model are inconsistent.' |
---|
40 | end |
---|
41 | |
---|
42 | % Evaluate the model |
---|
43 | nt = size(polymodel.ModelTerms,1); |
---|
44 | ypred = zeros(n,1); |
---|
45 | for i = 1:nt |
---|
46 | t = ones(n,1); |
---|
47 | for j = 1:p |
---|
48 | t = t.*indepvar(:,j).^polymodel.ModelTerms(i,j); |
---|
49 | end |
---|
50 | ypred = ypred + t*polymodel.Coefficients(i); |
---|
51 | end |
---|
52 | |
---|
53 | |
---|