i need it in Matlab! Basic programming (10) Using no special built-in functions,
ID: 3911308 • Letter: I
Question
i need it in Matlab!
Basic programming (10) Using no special built-in functions, write Matlab code to add (summate) every third number, starting with 1, under 100. That is, you are finding the result of 1 + 4 + ... + 97, but without typing out each individual number. Hint: you may use a loop. Show your code, including the result, with comments in your report.
Vector operations (10) Rewrite your solution to Question 2 by creating an array and performing a vector operation on the array. Your solution should be written using 1-5 lines of code. You may use built-in functions. Show your code, including the result, with comments in your report.
Explanation / Answer
Please find the required code in MATLAB:
%==== BASIC PROGRAMMING ====
total=0; % Summation variable initialization
i=1;
while i<100 % loop check to keep the 'i' count under 100
total = total + i;
i=i+3; % increment of 3 to get third number
end
% ==== VECTOR OPERATIONS ====
vec = 1:3:99 ; % Forming a vector from 1 to 99 (as asked to be under 100 i.e. <100) selecting every 3rd number
total_vec = sum(vec); % sum is an inbuilt function
Hope this helps! ******* Please Thumbs Up *******
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.