MATLAB NEEDED! 3.) In mathematics, the harmonic series is the divergent infinite
ID: 3760788 • Letter: M
Question
MATLAB NEEDED!
3.) In mathematics, the harmonic series is the divergent infinite series. Write a MATLAB function M-file, HarmArrayWhile , using a WHILE LOOP to
a) Input a positive integer, last
b) Use a while loop to create & return as output an array x , that contains the harmonic sequence
c) Preallocate the x array
d)Do not allow input to be an array.
e)Do not allow input to be a a non-positive integer.
Example: if last = 5, then x would be [1, 1/2, 1/3, 1/4, 1/5]
>>HarmArrayWhile(5) ans = 1.0000 0.5000 0.3333 0.2500 0.2000
PLEASE FOLLOW ALL INSTRUCTIONS LISTED! THANK YOU.
Explanation / Answer
function x=HarmArrayWhile(last)
% check whether the arg last is scalar or not
if(numel(last)~=1)
error('Input last must be scalar');
end
% check whether the arg last non negative or floating type
if(last<0 | (last-floor(last)>0))
error('last must be positive integer');
end
% declare variable x
x=zeros(1,last);
i=0;
% while loop to create harmonic
while(i<last)
x(i+1)=1/(i+1);
i=i+1;
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.