Write a script m-file called hw4m label your solution to


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

Solution Preview :

Prepared by a verified Expert
Programming Languages: Write a script m-file called hw4m label your solution to
Reference No:- TGS01482621

Now Priced at $40 (50% Discount)

Recommended (99%)

Rated (4.3/5)