This problem is written in MATLAB Given an integer N, it is required to compute
ID: 3693805 • Letter: T
Question
This problem is written in MATLAB
Given an integer N, it is required to compute the sum of the numbers from 1 to N. The sum can be computed directly or analytically by the following formula:
S = 1 + 2 + 3 + · · · + N = 1 2 N(N + 1) (1)
a. Let N = 20. Compute the sum by the following 5 methods, using: (1) a for-loop, (2) a conventional while-loop, (3) a forever while-loop, (4) the vectorized sum function, and, (5) the above analytical formula.
b. Write a function, say, fsum.m, that employs a switch structure and evaluates the sum by any of the above 5 methods, with method 5 being the default method. It must have the following usage, where m is the method number, m = 1, 2, 3, 4, 5: S = fsum(N,m);
c. The sum S grows with increasing N. Using your function fsum and a forever-while loop determine the smallest N such that S > 106 . Repeat using a conventional while-loop. Do not use the built-in functions sum or cumsum in this problem
Explanation / Answer
Ans;
function fsum
i=1;
sum=0;
while (i < 10)
cube = i * i * I;
sum = sum + cube;
i++;
end
function fsum
while(true)
cube = i * i * I;
sum = sum + cube;
i++;
end
function fsum
for a = 10:20
sum += (a*a*a);
end
function fsum
n=15;
sum = pow(((n * (n + 1) ) / 2),2);
function fsum
for n = 1:15
V(n) = pow(((n * (n + 1) ) / 2),2);
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.