Write an m-file that defines a row vector of 50 elements


  1. Write an M-file that defines a row vector of 50 elements with all ones.

  2. Add a code that replaces every element that is in an even place (for example the 2nd, 4th, 6th,...) with the number 2.

  3. Add a code that replaces every element that is in a place divisible by three (for example the 3rd, 6th, 9th,...) with the number 3.

  4. Turn in a printout of the final M-file and the output after running Parts (a) through (c).

  5. Write a function file in MATLAB that takes in one input and returns a single output. Call the function getSquared. The function should output the square of the number that it takes as input. Remember that the function must actually RETURN the squared value, and not simply display it on screen. Save the function file in some directory that you created. Turn in a printout of the function file.

  6. Write a script M-file called testSquared.m that defines a variable called xinput. Give any numerical value to xinput. From within this M-file, call the function getSquared. Store the output from the function file in a new variable called xoutput. Write a statement in the M-file that will print both the xinput and xoutput on the screen. Save the script M-file in the same folder as the function file. Turn in a printout of the M-file.

  7. Run the file testSquared.m from the command prompt. Turn in a printout of th output you get.

  8. Run the function file getSquared.m from the command prompt to calculate the square of any variable in your workspace. If you do not have one, create it in th workspace. Turn in a printout of your command prompt output.

  9. Save your getSquared.m function as getSquaredSpecial.m. Modify this function. Add a test code in your new function to determine whether the input number is greater than or equal to zero. If yes, then the function does exactly what it did before (that is, squaring). If not, then the function simply returns the value -1. Turn in a printout of your getSquaredSpecial.m file.

  10. In a similar fashion as above, create the function testSquaredSpecial.m file. Run two cases: (i) xinput is a positive number, and (ii) xinput is a negative number. Turn in a printout of the command prompt output for both cases.

  11. Write a function called sumHundred that takes an integer as the input. The function should determine whether the input is an integer between 1 and 100. If not, the function should display an appropriate error message. Turn in a printout of the function file.

  12. Test the function from the command prompt using (i) a number between 1 and 100, and (ii) a number greater than hundred. Turn in a printout of the command prompt output for both cases.

  13. You will now modify your function sumHundred. Add some logic to it using for or while loops, such that the function evaluates the sum of all integers from 1 to the input number. For instance, if the input number is 79, it should evaluate the sum of the first 79 integers. Do not use any built-in MATLAB functions. Turn in a printout of the modified function file. Run two test cases from the command prompt and turn in a printout of the results along with the M-file.

  14. Save your sumHundred function as sumEvenHundred. Add another logic code to the original function, such that now it evaluates the sum of all even integers from 1 to the input number. For instance, if the input number is 51, it should evaluate the sum of the even integers between 1 and 51. Turn in a printout of th function file and run it for two test cases from the command prompt. Turn in a printout of the results.

  15. Write a function called hiddenSum that takes two inputs and returns the sum of the two inputs. Turn in a printout of the function.

  16. Write a script M-file called testHiddenSum.m that defines two variables x and y Assign some values to these variables. Call the function hiddenSum from within this script M-file and store the result in a local variable, z. Run the script M-file and turn in a printout with the value of z.

  17. Modify the function hiddenSum such that now it DOES NOT HAVE any output arguments. Now modify your script M-file such that it calls hiddenSum using x and y as inputs and assigns the result to the variable z. You will need to further modify your function file. Submit a printout of the new function and script files and command prompt results from running your script M-file.

  18. Now modify your function hiddenSum such that it DOES NOT HAVE any input OR output arguments. Modify your script M-file such that it calls hiddenSum using x and y as inputs, and stores the result in the variable z. You will need to further modify your function file. Submit a printout of the new function and script files, and the command prompt results from running your script M-file.

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Write an m-file that defines a row vector of 50 elements
Reference No:- TGS01115214

Expected delivery within 24 Hours