Last active
October 1, 2015 05:37
-
-
Save dmadisetti/030af961745808c5024f to your computer and use it in GitHub Desktop.
Linear Algebra Bonus Question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % The Question in Question- | |
| % Find a polynomial p(t) such that p(n) and the derivative p'(n) | |
| % are both equal to n for n = 1, 2, 3, 4, and 5. | |
| % 'degree' is the largest n we want this to be true for | |
| % Loses precision around degree = 8 | |
| function ans = FancyFunction(degree) | |
| % Max power we're raising a particular degree to. | |
| % There are degree * 2 equations for any solution. | |
| % As 0 based, - 1 | |
| order = degree * 2 - 1; | |
| % Construct matrix so polynomial of degree is evaluated at each n | |
| % and polynomial derivative of degree is evaluated at each n | |
| equations = vertcat(... | |
| (1:degree)'.^(0:order),... % Polynomial equations | |
| horzcat(zeros(degree,1),(1:degree)'.^(0:order - 1).*(1:order))... % Polynomial derivative equations | |
| ) | |
| % build solutions so that f(n) = n and f'(n) = n | |
| solutions = vertcat((1:degree)',(1:degree)') | |
| % Find and Flip solution so that matrix is usable in tests | |
| ans = flipud(inv(equations)*solutions)'; | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % For details on polyval, polyder (Shorthand for Polynomial Valuation and Polynominal Derivative) see Octave API | |
| function valid = TestFancyFunction() | |
| % We only care about degree 5 | |
| % Find solution and derivative | |
| V = FancyFunction(5) | |
| dV = polyder(V) | |
| % Since precision lost, declare acceptable | |
| % error margin epsilon to accept as true | |
| epsilon = 1e-8; | |
| % Initialize valid to true. | |
| valid = 1; | |
| % If not found to be valid, valid = 0 | |
| for i = 1:5 | |
| valid = abs(polyval(V,i) - i) < epsilon && ... | |
| abs(polyval(dV,i) - i) < epsilon && ... | |
| valid | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| octave:1> TestFancyFunction | |
| equations = | |
| 1 1 1 1 1 1 1 1 1 1 | |
| 1 2 4 8 16 32 64 128 256 512 | |
| 1 3 9 27 81 243 729 2187 6561 19683 | |
| 1 4 16 64 256 1024 4096 16384 65536 262144 | |
| 1 5 25 125 625 3125 15625 78125 390625 1953125 | |
| 0 1 2 3 4 5 6 7 8 9 | |
| 0 1 4 12 32 80 192 448 1024 2304 | |
| 0 1 6 27 108 405 1458 5103 17496 59049 | |
| 0 1 8 48 256 1280 6144 28672 131072 589824 | |
| 0 1 10 75 500 3125 18750 109375 625000 3515625 | |
| solutions = | |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| V = | |
| 2.4306e-01 -6.4931e+00 7.4792e+01 -4.8621e+02 1.9599e+03 -5.0626e+03 8.3494e+03 -8.4447e+03 4.7367e+03 -1.1200e+03 | |
| dV = | |
| 2.1875e+00 -5.1944e+01 5.2354e+02 -2.9173e+03 9.7993e+03 -2.0250e+04 2.5048e+04 -1.6889e+04 4.7367e+03 | |
| valid = 1 | |
| valid = 1 | |
| valid = 1 | |
| valid = 1 | |
| valid = 1 | |
| ans = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment