1- MatLab - Cody Solutions Find the sum of the following arithmetic series: 5,9,
ID: 3662343 • Letter: 1
Question
1- MatLab - Cody Solutions
Find the sum of the following arithmetic series: 5,9, 13, 17 You will be supplied a random integer n which is guaranteed to be the last element of the series Create a vector such that its ith element corresponds to the ith element of the series. Use the in-built colon operator to create this vector. Assign the vector to the variable V. This answer is checked by the Cody system. Find the sum of this series using an in-built function that operates on vectors and assign it the variable S. This answer is checked by the Cody system. Find the number of elements in the vector V using an in-built command and assign it to the variable L. This answer is checked by the Cody system. Use L, n, and the MATLAB function linspace to create a vector equal in value to V and assign it to U. This answer is checked by the Cody system.Explanation / Answer
Good Wishes,
The random number supplied by the user on prompt "Please enter a random number:" is stored in 'n'
n= input('Please enter a random integer n')
Then I created a vector V such that ith element corresponds to the ith element of the series. This is done using in-built colon operator to create this vector. The starting element of the series is 5 as given in the problem statement and the difference between each element in the series is 4, and last element is 'n'
V= 5:4:n
Next I found out the sum of the series using in-built function sum() that operates on vectors and assigned the result to S
S= sum(V)
Then I found the number of elements in the vector V using in-built command length() and assigned the result to L
L= length(V)
Then i used L,n and MATLAB function linspace to create a vector U equal in value to V
U= linspace(5,n,L)
Now the complete code is as follows.
Complete Code in MATLAB:
n= input('Please enter a random integer n')
V= 5:4:n
S= sum(V)
L= length(V)
U= linspace(5,n,L)
Sample Output:
Please enter a random number:
21
V=
5 9 13 17 21
S= 65
L= 5
U=
5 9 13 17 21
Hope this is clear. Please provide your valuable comments.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.