What would be a Matlab function with two arguments that returns a Matrix (as shown) ?
Show older comments
I would like to create a Matlab function (with two arguments, N = Number of parameter, PO = polynomial order) that returns a matrix as shown in the linked file. Desired output Matrix
3 Comments
Image Analyst
on 10 Dec 2017
I have no idea what those matrices are. It looks vaguely similar to meshgrid output, but it's not. I imagine, from the names of things it might have something to do with polyfit() and polyval(), but I can't figure it out. There is obviously some information missing, either you didn't tell us, or your instructor didn't tell you. So you either get it, or start guessing and trying to figure it out.
Salaijobhaka
on 10 Dec 2017
Edited: Walter Roberson
on 10 Dec 2017
Accepted Answer
More Answers (2)
Roger Stafford
on 16 Dec 2017
@Salaijobhaka: I finally managed to write the script I mentioned earlier in the comment above. It produces the same ordering as you described in your "Desired output Matrix" file five days ago. It wasn't quite as simple as I thought it would be. It makes important use of the 'cumsum' function for appropriate addressing. The 'round' function is supposed to help protect against round-off errors for very large numbers of rows in the resulting matrix, M.
PO = 4; N = 7; % <-- Choose PO and N
M = zeros(round(prod(N+(1:PO))/prod(1:PO)),PO);
c = 1:N+1;
M(1:N+1,1) = (0:N)';
for ip = 2:PO
s2 = repmat(c(N+1),1,N+1);
s1 = c(N+1)-[0,c(2:N+1)-1];
c = cumsum(c);
d2 = c(N+1)-[0,c(1:N)];
d1 = [d2(2:N+1)+1,1];
for in = 1:N+1
M(d1(in):d2(in),3:ip) = M(s1(in):s2(in),2:ip-1);
M(d1(in):d2(in),2) = M(s1(in):s2(in),1)-M(s1(in),1);
M(d1(in):d2(in),1) = repmat(M(s1(in)),d2(in)-d1(in)+1,1);
end
end
2 Comments
Roger Stafford
on 16 Dec 2017
@Salaijobhaka: Note: I see that unfortunately I have inadvertently interchanged N and PO, so that my N is your PO and my PO is your N, as you have defined them in your comment. I will leave the task of reversing these two variables in the script to you, since I am rather sleepy at the present moment and might make a mistake in that undertaking.
Salaijobhaka
on 16 Dec 2017
Salaijobhaka
on 11 Dec 2017
Edited: Salaijobhaka
on 11 Dec 2017
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!