IN MATLAB Write a script that prompts the user to enter any
IN MATLAB, Write a script that prompts the user to enter any vector.the script should then call each of the following functions (sum of tw vectors,cumsum of two vectors,compute of sine value in degrees for two vectors)with the user\'s vector as input. It should then multiply the values returned by the three functions and display the result in command window.
test result for the following vectors:[1 2 3 4],[5 10 15 20 25 30 35]
Solution
The below code will first add the elements of both the vector and return the result as a sum vector. Next, it will add all the elements in the sum vector to get the cummulative sum. Finally it will call the inbuilt sind function to compute the sine values for the elements in each vector. After this, it will multiply the sum vector with the cummulative sum value and the sine vector to get the final vectors. In the end, it displays both the results.
Let the main script be named as \"main.m\"
Contents of \"main.m\"
message1 = \'enter the elements of vector 1\';
vec1 = input(message1);
message2 = \'enter the elements of vector 2\';
vec2 = input(message2);
sum_vec = computeSum(vec1,vec2);
cumm_sum_vec = computeCummulativeSum(vec1,vec2);
[sine_val_vec1,sin_val_vec2] = computeSineValues(vec1,vec2);
res1 = sum_vec*cumm_sum_vec*transpose(sine_val_vec1);
res2 = sum_vec*cumm_sum_vec*transpose(sine_val_vec2);
disp(res1);
disp(res2);
Create another file for computing the sum of all elements of vector and rename it as computeSum.m. Please note that the name of the script file should be same as the name of the function being defined inside it.
Contents of computeSum.m
function [sum_vec]=computeSum(vec1,vec2)
if (size(vec1,2)==size(vec2,2))
disp(\'size of both vectors are not same, addition cannot be performed for two unequal sized vectors\');
break;
end if
sum_vec = vec1+vec2; % This statement will add the elements of the two vectors and return another vec of the % same size.
Create another file computeCummulativeSum.m to define the cummulative sum value after adding both the vectors
Contents of computeCummulativeSum.m
function [cumm_sum_vec] = computeCummulativeSum(vec1,vec2);
if (size(vec1,2)==size(vec2,2))
disp(\'size of both vectors are not same, addition cannot be performed for two unequal sized vectors\');
break;
end if
sum_vec = vec1+vec2;
cumm_sum_vec = 0;
for count = 1:size(sum_vec,2)
cumm_sum_vec = cumm_sum_vec + sum_vec(i); % calculates the cumm sum of the sum vector
end
Finally create another file computeSineValues.m to compute sine values of both the vectors by calling the inbuilt matlab function sind() as shown below
function [sine_val_vec1,sin_val_vec2] = computeSineValues(vec1,vec2)
sin_val_vec1 = sind(vec1); %compute the sine values when the angle is in degrees
sin_val_vec2 = sind(vec2);% compute the sine value when the angle is in degrees

