Advertisement
makispaiktis

Combinatorics - C(n,k) plots

Mar 25th, 2022 (edited)
1,733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.03 KB | None | 0 0
  1. clc
  2. clear all
  3.  
  4. % MAIN FUNCTION
  5. LIMIT = 20;
  6. FAC = all_factorials(LIMIT);
  7. a = 7;
  8. b = 12;
  9. n_list = a:b;
  10. for i = 1:length(n_list)
  11.     n = n_list(i);
  12.     [x, y] = plotting(n, LIMIT, FAC);
  13.     disp("n = " + num2str(n));
  14.     disp(num2str(y));
  15.     display(' ');
  16.     hold on
  17. end
  18. title("Combinations k from n");
  19. xlabel('k');
  20. ylabel('C(n,k)');
  21. titles_list = [];
  22. for i = a:b
  23.     titles_list = [titles_list "n = " + num2str(i)];
  24. end
  25. legend(titles_list);
  26.  
  27.  
  28. % Auxiliary Functions
  29. function factorials_list = all_factorials(n)
  30.     factorials_list = zeros(1, n);
  31.     factorials_list(1) = 1;
  32.     factorials_list(2) = 1;
  33.     factorials_list(3) = 2;
  34.     for i = 4 : n
  35.         factorials_list(i) = (i-1) * factorials_list(i-1);
  36.     end
  37. end
  38.  
  39. function y = c(n, k, FAC)
  40.     y = FAC(n+1) / (FAC(k+1) * FAC(n-k+1));
  41. end
  42.  
  43. function [x, y] = plotting(n, LIMIT, FAC)
  44.     x = 0:n;
  45.     y = zeros(1, n+1);
  46.     if n > LIMIT
  47.         y = -1;
  48.         return;
  49.     end
  50.     for i = 0:n
  51.         y(i+1) = c(n, i, FAC);
  52.     end
  53.     plot(x, y);
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement