Write a script m-file called hw4

Assignment Help Programming Languages
Reference no: EM131104622

Write a script m-file called hw4.m. Label your solution to each exercise with a comment. Some of the exercises ask for additional script m-files that will be called from within hw4.m.

1. Write a script called 'prob1.m' that solves for the variables y, and z in terms of a user inputed x. The variables y and z are defined as follows:

2.

3. y = x - 30            when 0 < x < 100

4.   = 0.20*x + 800      when x > 100

5.   = -50               otherwise

6.

7.

8. z = -1                when x < 0

9.   =  0                when x = 0

10.        =  1                when x > 0

Your script should prompt the user to input a value of x (use the input function). Next it should compute the values of y and z (surpress the output at first). Finally it should explicitly display the values of x, y, and z.
From with the 'hw4.m' script call 'prob1.m'.
2. Write a script called 'prob2.m' that asks the user if they wish to clear the variables in the workspace. If the user responds with the strings 'y' or 'yes' then the workspace should be cleared. After the workspace is cleared the script should display the string 'Workspace cleared.' If the user responds with the strings 'n' or 'no' then the work space should not be cleared and the script should display the string 'Workspace not cleared.' If the user enters anything else, the script should say that the 'Input was not understood.' You should implement this using an if-else construction.
In the file 'hw4.m' first issue a command that lists all the variables currently in the workspace. Then execute execute 'prob2.m'. Finally, to see if your script did what it was supposed to, redisplay a list of all the variables in the workspace.
3. Write a script called 'prob3.m' that does the same thing as the previous question except that it makes use of a switch-case construct instead of an if-else.
In the file 'hw4.m' first issue a command that lists all the variables currently in the workspace. Then execute execute 'prob3.m'. Finally, to see if your script did what it was supposed to, redisplay a list of all the variables in the workspace.
4. Write a function called triple (in a file called 'triple.m'). That takes a single variable x and returns a single variable in which every element of x is multiplied by 3. Make sure to properly construct the help text for the function in the first comment block. Be sure to include an H1 line.
From within 'hw4.m' write a series of expressions that demonstrate the triple function being used a variety of input including both scalers and arrays. Also make sure to demonstrate that its help text works properly.
5. Write a function called threshold1 (in a file called 'threshold1.m'. The function takes three arguments. The first two are arbitrarily sized arrays, A and B, and the third is a postive scaler value, t. The function returns two variables, call them x and y.
The variable x should have the same elements as the variable A, except in every position where an element of A is larger than t, there should be a 0.
The variable y should have the same elements as the variable B, except in every position where an element of B is smaller than t, there should be a 0.
Be sure to properly comment your code.
From within 'hw4.m' write a series of expressions that demonstrate the threshold1 function being executed on at least two different sets of input.
6. Write a function called threshold2 (in a file called 'threshold2.m'). The function takes an arbitrary number of input variables. The first input variable, t, is required and is the threshold value. The remaining arguments are arbitrarily sized arrays. The function should return the same number of output variables as there are input arrays to threshold.
Each of the output variables should have the same elements as the corresponding input variable, except in every position where an input value is less than or equal to the threshold t, there should be a 0.
If the user doesn't specify enough output variables when calling this function, the function should generate a MATLAB error with a descriptive error message.
If the user doesn't specify enough input variables to fill all of the output variables requested, the remaining output variables should be set to NaN and a warning messaging (just using disp) should be displayed by the function.
Sample runs of this program might look like this:

>> z = 1:5;

>> a = threshold2(3,z) 

a =  0 0 0 4 5 

>> y = 1:6;

>> [a b] = threshold2(4,z,y)

 a =  0 0 0 0 5 

b =  0 0 0 0 5 6 

>> [a b c] = threhold2(4,z,y) 

a =  0 0 0 0 5 

b =  0 0 0 0 5 6

c =  NaN >> a = threshold2(4,z,y)

?? Error using ==> threshold2

Not enough output variables.

Be sure to properly comment your code.

From within 'hw4.m' write a series of expressions that demonstrate the threshold2 function being executed on at least three different sets of non-error generating input. Be sure to demonstrate the case when more ouput variables are present than corresponding input variables.

Finally, execute the threshold2 function with a threshold value plus three input variables but set it equal to only two output variables. Catch the error that your function should have generated such that the script doesn't stop executing and extract the message that was associated with that error. Use the display function to display the error message and then set all of the original output variables equal to NaN.

7.  Write a function called withdraw (in the file 'withdraw.m') that simulates withdrawing money from a bank account. The function takes a single input argument, a positive scaler double value. If the user passes a value that is not scaler or is not greater than 0, your function should throw an error.

The function should access a global variable called BALANCE. Subtract the amount of the withdrawal from the BALANCE variable. If the subtraction would take BALANCE to less than 0, leave BALANCE unchanged and throw an error. Also, if BALANCE is not yet defined, thow an error.

Finally you are limited to 5 withdrawals from your bank account. Use a persistent variable to store how many times the withdrawal function has been used. On the fifth and all subsequent calls to the function it should automatically throw an error and not perform the withdrawal.

The function should return only one value, the number of withdrawals remaining.

From the 'hw4.m' file set the global value BALANCE to some initial value. Then show some example calls to the withdraw function, followed by a display of the remaining value of BALANCE. You should demonstrate the various error conditions, but make sure to catch all errors and display the error message without forcing the script to terminate.

What you need to submit:

You need to submit these files:

·      hw4.m

·      prob1.m

·      prob2.m

·      prob3.m

·      triple.m

·      threshold1.m

·      threshold2.m

·      withdraw.m

Reference no: EM131104622

Questions Cloud

Discuss the use of arrays in java programs : Discussion Question: Discuss the use of arrays in Java programs versus the use of regular variables
Examples of information systems : 1. Using the example of a university, give examples of information systems for each of the four levels of scope shown in Figure 1 (Chapter 7). Describe three workgroup information systems that are likely to duplicate data. Explain how the characte..
Obtain a close approximation to the impact velocity v : Obtain a close approximation to the impact velocity v from the data given
Identify the augite minerals using the key provided : Identify the following minerals using the Key provided. Place the names with the appropriate number. Augite, Biotite, Calcite, Galena, Graphite, Gypsum, Halite, Hematite, Hornblende, Kaolinite, Microcline (Orthoclase), Muscovite, Plagioclase, Pyr..
Write a script m-file called hw4 : Write a script m-file called hw4.m. Label your solution to each exercise with a comment. Some of the exercises ask for additional script m-files that will be called from within hw4.m.
What circumstances are different types control statement use : Discussion Question: Under what circumstances are different types of Java control statements and loop types best utilized? Provide examples
Jurisdictional rules regarding liability : Should the jurisdictional rules governing use tax collection obligations be different from the jurisdictional rules regarding liability for income taxation? If so, in which context should the jurisdictional threshold be higher?
Leadership culture in the organization : As the new top-level executive of a medium-sized corporation, you have noticed that the leadership culture in the organization has been ineffective, which has lowered employee morale.
How do psychologists define personality : How do psychologists define personality - Provide an overview of the definition of personality.

Reviews

Write a Review

Programming Languages Questions & Answers

  Write pseudocode to calculate the total price of the items

Create a line on the output receipt that identifies the item its price and the sales tax applicable to that item. Keep a running total of item prices and taxes.

  Write program that asks user to enter five test scores

Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score.

  Rewrite the extract.pl using bioperl

Rewrite the "extract.pl" using BioPerl, including the use of Bio::SeqIO to read the genome FASTA file ("mystery_seq1.fas") and the use of Bio::Seq for obtaining coding sequences and translating sequences.

  Build a student record managing system application

Build a student record managing system application

  Program to display only unique values which user entered

Program to display only the unique values which the user entered. Give for the "worst case" in which all 20 numbers are different. use smallest possible array to solve this problem.

  Determine recursive procedure and iterative procedure

Determine the example or recursive procedure and represent it as iterative procedure. Also, select the iterative procedure which you would re-write as recursive.

  Evaluate the arithmetic expressions

Prepare a Lisp program to evaluate the arithmetic expressions - If the input is legal, your program should output the final result of the expression evaluation, as given in the above case. If the input is illegal, the result is undefined, i.e., you..

  Design a website to allow calculation of areas

Design a website to permit calculation of areas. A radio button list can be used to allow the user select what type of area to be calculated. Once the selection is done, the input text boxes should be displayed with the proper labels.

  What will the following program segments display

What will the following program segments display

  Define simple encryption application

(Simple Encryption Application) This application uses a simple technique to encrypt a number. Encryption is the process of modifying data so that only those intended to receive the data can undo the changes and view the original data.

  Explain post-conditions and description of the parameters

Write specifications for method which advances any given date by one day. Write a statement of purpose, pre- and post-conditions, and description of the parameters.

  Program calculate average number of days employee are absent

Write a program that calculates the average number of days a company's employees are absent. The program should have the following functions: a function called main that asks the user for the number of employees.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd