Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a non-negative integer value a. The script should then evaluates the sum Pn i=1 i a using a running sum for the case when n = 5. More specifically, use a counter for i that is used in the output fprintf as well as in the running sum. Feel free to write some code and just repeat it 5 times in your script. Sample output: Enter a non-negative integer value for a: 7 i is 1, Sum of iˆ7 so far is 1 i is 2, Sum of iˆ7 so far is 129 i is 3, Sum of iˆ7 so far is 2316 i is 4, Sum of iˆ7 so far is 18700 i is 5, Sum of iˆ7 so far is 96825 Enter a non-negative integer value for a: 1 i is 1, Sum of iˆ1 so far is 1 i is 2, Sum of iˆ1 so far is 3 i is 3, Sum of iˆ1 so far is 6 i is 4, Sum of iˆ1 so far is 10 i is 5, Sum of iˆ1 so far is 15
Code:
clc;clear all;
a=input('Enter a non-negative integer value for a:');
x=0;
for i=1:1:5
x=x+i^a;
fprintf('i is %d , Sum of iˆ%d so far is %d \n',i,a,x)
endfor
Command window:(output)
Enter a non-negative integer value for a:7
i is 1 , Sum of iˆ7 so far is 1
i is 2 , Sum of iˆ7 so far is 129
i is 3 , Sum of iˆ7 so far is 2316
i is 4 , Sum of iˆ7 so far is 18700
i is 5 , Sum of iˆ7 so far is 96825
Enter a non-negative integer value for a:1
i is 1 , Sum of iˆ1 so far is 1
i is 2 , Sum of iˆ1 so far is 3
i is 3 , Sum of iˆ1 so far is 6
i is 4 , Sum of iˆ1 so far is 10
i is 5 , Sum of iˆ1 so far is 15
Get Answers For Free
Most questions answered within 1 hours.