Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function output_sum = task_two(start_element_value, end_element_index, constant)
- %% HOW TO
- % start_element_value - first a element
- % end_element_index - n (how many values of elements we should calculate)
- % constans - m constans added to each elemect according to formula:
- % a(n)=a(n-1)+m
- % output - vector with calculeted elements
- % element - current callculeted element
- %% MAIN CODE
- % Initializaiton of default values for arguments
- default_a1 = 1;
- default_n = 10;
- default_m = 1;
- if nargin == 0 % If a1, n, m is missing set default values for a1, n, m.
- %error('Please provide a1 value'); <- uncomment if a1 is mandatory
- % and comment three lines below
- start_element_value = default_a1;
- end_element_index = default_n;
- constant = default_m;
- end
- if nargin == 1 % If n and m is missing set default values for n and m.
- end_element_index = default_n;
- constant = default_m;
- end
- if nargin == 2 % If m is missing set default value for m.
- constant = default_m;
- end
- if nargin > 3 % If 4 or more argumentrs provided skip input parssing
- error('To many arguments to parse')
- end
- output(1,1) = start_element_value ;
- element = output(1,1); % Initialization of a1
- for step=2:1:end_element_index % calculations starts for a2 element
- element = element + constant; % a(n) = a(n-1) + m
- output(1, step) = element; % append calculated value on an index
- end
- output_sum = sum(output);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement