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

  Create ifstream and ofstream objects called infile n outfile

Create ifstream and ofstream objects called "infile" and "outfile" respectively. Include 4 functions for initializing, copying text, counting, and writing the results to a file, respectively.

  State space search using prolog and lisp

State Space Search using Prolog and LISP - solve problem using the Prolog computer programming language

  Write program to compute salary of employee

Write a program to compute the salary of an employee according to his/her grade assigned by employer. You should declare class named CalSalary whose private data members will be employee.

  Code the next version of the cp1200 catering calculator

You are to plan and then code the next version of the CP1200 catering calculator, as described in the following information and sample output

  Invoke method to tell air conditioner object to turn on

Assume there is a reference variable myAC to an object of this class, which has already been created. Using the reference variable, invoke a method to tell the air conditioner object to turn on.

  Write application to inputs ten numbers from user

Write the application to inputs ten numbers from user, each number can be between 10 and 100, inclusive. As each number is read in determine if it is number already entered.

  Write tax program having single formula to compute tax

Write the tax program with following logic: Set variables cutoff15 and cutoff28 which depend on marital status. Then have single formula which computes tax, depending on incomes and cutoffs.

  Does loop have dependences two-dimensional arrays

Consider the following code with two-dimensional arrays. Does this loop have dependences? Can these loops be written so they are parallel?

  Program that uses a recursive function to print a string

Your program must contain a recursive function that prints the string backwards. Do not use any global variables; use appropriate parameters.

  Write a set-list of enumerated constants for week

Write a set/list of enumerated constants called week which contains days of the week. Have variable called today that is of type week. Allot value to today.

  Program to find sum-difference of two complex numbers

Program should ask user to enter two complex numbers and it automatically finds it sum, difference, coefficient, product conjugate.

  Write program to enter current value of car

Write a program that asks the user to enter the current value of a car. For a new car, it is the selling price of the car. The program should display the number of years it takes for the car's trade-in value to be below $1000.

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