Question

Create a new function file and write a function s= SUM_EVEn_ ubitname(V) that takes a vector...

Create a new function file and write a function

s= SUM_EVEn_ ubitname(V)

that takes a vector V of arbitrary length and determines the sum of the even element and assign it to s.

your function calculations must do the following:

* Use a loop that iterates through every element of V and adds s only even element and skips all other elements.

* Contain the appropriate H1 and help text lines.

* Note: A number is even, if round(number/2) is equal to the original number/2.

Test your function in the command window using

>> SUM_EVEN_ubitname([1:15])

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

CODE: MATLAB Programming Language

%function to sum even elements.
function S = SUM_EVEN_ubitname(v)
%storing length of vector v.
n = length(v);
%initializing S to 0.
S = 0;
%iterating vector v.
for i = 1:n
    %checking if element at ith position
    %in vector is even or not.
    if(round(v(i)/2) == v(i)/2)
      %if even then adding element
      %to S.
      S = S + v(i);
    end %end of if
end %end of for
end %end of function

============================================================================

SCREENSHOT OF THE CODE:

SUM_EVEN_ubitname.m 1 function to sum even elements. 2 function S = SUM_EVEN_ubitname (v) 3 storing length of vector v. 4 n =

============================================================================

OUTPUT:

Command Window >> SUM_EVEN_ubitname ([1:15]) ans = 56

Thank you.

Add a comment
Know the answer?
Add Answer to:
Create a new function file and write a function s= SUM_EVEn_ ubitname(V) that takes a vector...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as...

    Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as output, c, returns the number of zeros at the beginning of v (number of zero elements before any non-zero element). For example, for input (-5, 5, 11] the output would be 0. If the input is [0, 0, 3, 0, 0, 0], the output would be 2. If the input is [0, 0, 0, 0, 7, 4) the output would be 4. 5) Write...

  • Write a MATLAB function file with the function handle [S, Tm] = principal (Sxy) which takes...

    Write a MATLAB function file with the function handle [S, Tm] = principal (Sxy) which takes a matrix input Sxy in the form xx Txy Txz] [Exz Tyz Ozz) and returns the principal stresses in vector form to the variable S and the maximum shear stress in scalar form to the variable Tm. You may find the roots function useful in your calculations. To verify that your function is working correctly, you can use the stress state and answer provided...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Create a function that takes a list and an integer v, and returns True if v...

    Create a function that takes a list and an integer v, and returns True if v is in the list (False otherwise). The function should be efficient and stop searching as soon as possible. •The main program must generate a very large list with random elements, call the function to search a value and display the result. Add in the function a variable Nsteps to count the number of steps used by the algorithm (number of times the loop is...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as t...

    Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as the input a provided string for the sample name (sname) and number for strength (strgth) - open a text file named mytensiledata.txt, with permission to read or append the file. - Make sure to capture any error in opening the file - Write sname and strgth to the file - Close the file...

  • Please use MATLAB to solve this, thanks 7. Write a function M-file that will compute the...

    Please use MATLAB to solve this, thanks 7. Write a function M-file that will compute the equivalent resistance for a series or parallel combination of an arbitrary number of resistors. Your function should: accept as input an arbitrary number of resistor values, stored in a vector ask the user to input 'p' if the resistors are in parallel; ask the user to input 's' if the resistors are in series; output the equivalent resistance for the series or parallel combination...

  • Write the python function time to midnight() that takes the name of a file containing multiple...

    Write the python function time to midnight() that takes the name of a file containing multiple lines of text giving time in HH:MM:SS HRS or HH:MM:SS AM or HH:MM:SS PM format one per line. The HRS notation indicates CSE 101 – Summer 2019 Lab Assignment #9 2 that the time has been given in 24-hour time format. Each line tells the current time of the day. The function reads the file line by line and calculates the number of seconds...

  • Task 3: Creating a Simple jQuery Application 1. Launch HTML-Kit. 2. Create a new HTML file...

    Task 3: Creating a Simple jQuery Application 1. Launch HTML-Kit. 2. Create a new HTML file and save it as nnLab8.htm. 3. Add the following HTML to the file: <!DOCTYPE HTML> <html> <head> <title>Hello World - jQuery Style</title> </head> <body> <div id="first"></div> <div id="second"></div> <a href="#" id="link">Click Me!</a><br /> <span id="greeting"></span> </body> </html> 4. Add the following<script> element to the<head> section. NOTE: Use the jQuery version number that matches the file name of the file you downloaded in Step 1....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT