Very Hard matlab HELP !!!! For questions 1-3 create a user defined function that
ID: 2326543 • Letter: V
Question
Very Hard matlab HELP !!!!
For questions 1-3 create a user defined function that takes a vector of any size as an input argument. The output should be whatever is described in the question. Each function file must use a while loop in its calculations (even if that isn’t the most efficient way to do the calculation). The built-in function length may be helpful to you. Test each function with each of the following vectors: [1 2 3 4] [5 10 15 20 25 30 35]
1) Add up all the values of the elements and returns the sum. (You can check your answer with the built- in function sum).
2) 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)
3) Computes the sine of the values in the vector (assume they are in degrees)
4) Write a script that prompts the user to enter any vector. The script should then call each of the functions from questions 5-7 with the user’s vector as input. It should then multiply the values returned by all three functions and display the result to the command window.
Explanation / Answer
Function for Question 1
% Following is the code of function file and name of the function is Q1
function [ sum ] = Q1(A)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
i=1:length(A);
sum=0;
j=1;
while j<=length(A)
sum=sum+A(j);
j=j+1;
end
end
Function for Question 2
% Following is the code of function file and name of the function is Q2
function [] = Q2(A)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
i=1:length(A);
sum=0;
j=1;
while j<=length(A)
sum=sum+A(j);
j=j+1;
cumsum(j)=sum;
end
cumsum(:,1)=[]
end
Function for Question 3
% Following is the code of function file and name of the function is Q3
function [sine] = Q3( A )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
sine=sind(A)
end
Script file for Question 4
% Following is the code of script file and name of script is Q4
A=input('Enter vector A');
a=input('Enter operation to be proformed on A i.e. 1 for sum, 2 for cumsum and 3 for sine');
if a==1
Q1(A)
end
if a==2
Q2(A)
end
if a==3
Q3(A)
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.