Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use MATLAB no screenshots or pics just code and use pseudecode 8. Computes the r

ID: 3731449 • Letter: U

Question

Use MATLAB

no screenshots or pics just code

and use pseudecode

8. Computes the running sum. For element j, the running sum is the sum of the elements from 1 to j, inclusive. (You can check your answer with the built-in function cumsum) 9. Computes the sine of the values in the vector (assume they are in degrees) 10. Write a script that prompts the user to enter any vector. The script should then call each of the functions from questions 7-9 with the users vector as input. It should then multiply the values returned by all three functions and display the result to the command window.

Explanation / Answer

NOTE: SUBPART 7 IS NOT VISIBLE IN THE QUESTION POSTED SO I'M ONLY ANSWERING SUBPARTS 8, 9 AND 10. COMMUNICATE FURTHER IN THE COMMENTS WITH THE SUBPART 7 IF YOU WANT.

Below is the function for calculating running sum of a vector. This function is stored in a separate .m script file named as runningSum.m. The name of the function is also runningSum. If you want to use another function name then modify the function name inside the script file and then rename the script file to match the name of the function inside it.

------------------------------------runningSum.m----------------------------------------

The code below will be inside the    runningSum.m     script file.

function cum_sum = runningSum(vec, index)
%---------------------------------------------------------------
%runningSum     Creates a vector containing cumulative sum of the elements in vec (the vector of elements provided)
%
%   cum_sum = runngingSum(vec, index)    Returns vector of cumulative sum of elements of the vector 'vec' upto 'index' elements
%                                        from the beginning
%
%---------------------------------------------------------------
if size(vec,2) > 0         %check if its an empty vector
    cum_sum = [vec(1)];
    if size(vec,2) > 1        %check if number of elements is greater than 1, if not, return the element as it is
      for i=2:index
        cum_sum = [cum_sum,cum_sum(i-1)+vec(i)];     %appending new values to the initial vector
      end
    end
else
    display("error")
end
end

Below is the function for calculating sine of elements of a vector. Same as above, this code is in a separate file name vecSine.m, matching the function name, i.e., vecSine. To modify this follow the same procedure as explained for the previous function.

-------------------------------------------------vecSine.m---------------------------------------------------------

The code below will be inside the    vecSine.m         script file.

function vec_sine = vecSine(vec, index)
%-----------------------------------------------------------------------------------------
%vecSine      Calculates the sine of the elements of the given vector
%
%   vec_sine = vecSine(vec, index)      Returns a new vector containing the sine of elements
%                                       of vector 'vec' upto 'index' elements from the beginning assuming
%                                       values in the vector are in degrees
%
%-----------------------------------------------------------------------------------------
if size(vec,2) > 0
    vec_sine = [sin(deg2rad(vec(1)))];    %using deg2rad because the values are in degrees
    if size(vec,2) > 1
      for i=2:index
        vec_sine = [vec_sine, sin(deg2rad(vec(i)))];    %appending the new values to the initial vector
      end
    end
else
    display("error")
end
end

Finally the script that calls both these independent functions and takes a vector as user input. This script is inside a script file name      userInput.m . You can modify its name as you like, it won't affect anything.

NOTE:   This script asks for the size of the vector from the user, i.e, it is not interactive and once user gives the input the vector will be fixed in size. Notify me if you want an interactive version of this script.

-----------------------------------------------------userInput.m--------------------------------------------

The code below will be inside the scirpt file name      userInput.m    .

%Take the number of elements in the vector from the user
num = input("Enter the number of elements in the vector :");

%Initialize a vector of size given by the user with zeros
user_input_vec = zeros(1,num);

%Take individual inputs in the array
for i=1:num
user_input_vec(i) = input("Enter the element: ");
end

display("Running sum of the vector is : ");

%Call runningSum function and display running sum vector
display(runningSum(user_input_vec,num));

display(" Sine of individual elements of the vector is: ");

%Call vecSine function and display the sines of vector elements
display(vecSine(user_input_vec,num));

display(" Product of running sum vector and the sine vector is: ");

%Calculate the product of the runningSum vector and the sine vector and display the result vector
display(runningSum(user_input_vec,num).*vecSine(user_input_vec,num));

Now to run this program, all the above scripts need to be in the same directory. In MATLAB, 'cd' into the directory where you have put these files and on the command window type 'userInput' or if you have given the third script any other name, type that name and the script execution will start.

Notify me if there is any problems with these scripts.    : )

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote