Advertisement
ForeverStrong

Wypok_Task_Two

May 1st, 2020
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.57 KB | None | 0 0
  1. function output_sum = task_two(start_element_value, end_element_index, constant)
  2. %% HOW TO
  3. %     start_element_value - first a element
  4. %     end_element_index - n (how many values of elements we should calculate)
  5. %     constans - m constans added to each elemect according to formula:
  6. %     a(n)=a(n-1)+m
  7. %     output - vector with calculeted elements
  8. %     element - current callculeted element
  9. %% MAIN CODE
  10.    
  11.     % Initializaiton of default values for arguments
  12.     default_a1 = 1;
  13.     default_n = 10;
  14.     default_m = 1;
  15.    
  16.     if nargin == 0 % If a1, n, m is missing set default values for a1, n, m.        
  17.         %error('Please provide a1 value'); <- uncomment if a1 is mandatory
  18.         % and comment three lines below
  19.         start_element_value = default_a1;
  20.         end_element_index = default_n;
  21.         constant = default_m;
  22.     end
  23.     if nargin == 1 % If n and m is missing set default values for n and m.
  24.         end_element_index = default_n;
  25.         constant = default_m;
  26.     end
  27.     if nargin == 2 % If m is missing set default value for m.
  28.         constant = default_m;
  29.     end
  30.     if nargin > 3 % If 4 or more argumentrs provided skip input parssing
  31.         error('To many arguments to parse')
  32.     end
  33.    
  34.     output(1,1) = start_element_value ;
  35.     element =  output(1,1); % Initialization of a1
  36.     for step=2:1:end_element_index % calculations starts for a2 element
  37.         element = element + constant; % a(n) = a(n-1) + m
  38.         output(1, step) = element; % append calculated value on an index
  39.     end
  40.     output_sum = sum(output);
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement