Homework 2 – Functions Solution

$35.00 $29.00

Function Name: ​f Inputs: (double) An x value at which to evaluate the function Outputs: (double) The resulting y value Function Description: Write a function in MATLAB that will evaluate the function, f(x) , for any given x value. f (x) = 1.17√1 + x0.9 + sin(x2 − 4) 1.5e(x2 − g(x)) g(x) is another…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

Function Name:f

Inputs:

  1. (double) An x value at which to evaluate the function

Outputs:

  1. (double) The resulting y value

Function Description:

Write a function in MATLAB that will evaluate the function, f(x) , for any given x value.

f (x) = 1.171 + x0.9 + sin(x2 − 4)

1.5e(x2g(x))

g(x) is another function defined as:

g(x) = x * |x|

Notes:

  • Round your final output to 4 decimal places.

  • There are built-in MATLAB functions for square root, sine, exponentials and absolute value (sqrt(),sin(),exp(), andabs(), respectively).

  • You can use thehelp() function to look up these or any other built-in functions you may be confused about.

Homework 2 Functions

Function Name:applesAndOranges

Inputs:

  1. (double)The number of apples

  1. (double)The number of oranges

  1. (double)The number of good apples

  1. (double)The number of good oranges

Outputs:

  1. (double)The probability of randomly picking a bad apple

  1. (double)The probability of randomly picking a bad orange

Function Description:

Thisfunction should calculate the probability of randomly picking a bad apple or bad

orange from a bag. The total number of apples and oranges will be given in the first two inputs and the number of good apples and good oranges will be given as the third and fourth inputs. The output value should be expressed as a percentage, not a decimal. So if there was a 25% chance of drawing a bad apple, this would be expressed as the integer 25 rather than the decimal 0.25. Round all percentages to the nearest hundredth of a percent.

Notes:

  • The number of good apples/oranges will always be less than or equal to the number of apples/oranges.

Homework 2 Functions

Function Name:fib

Inputs:

  1. (double) Which Fibonacci number to calculate

Outputs:

  1. (double) The specified Fibonacci number

Function Description:

Youare probably familiar with the Fibonacci sequence: the sequence of numbers

(starting with 0, 1) where every number is the sum of the previous 2 numbers. From this definition, you might think in order to calculate the nthFibonacci number, you would have to first calculate all the numbers that came before it. However, this is not the case! The formula for any Fibonacci number is:

  • = ϕn−(−ϕ)n

n√5

is the golden ratio:

  • = 1+√5 2

To calculate any Fibonacci number, all you have to do is plug ‘n’ into this equation.

Thisformula is mathematically exact (no rounding would be necessary in theory).

Notes:

  • Due to MATLAB’s internal rounding of large numbers, you should round your answer to the nearest integer.

Homework 2 Functions

Function Name:sepDigits

Inputs:

  1. (double) A three-digit integer

Outputs:

  1. (double) The hundreds-place digit of the input

  1. (double) The tens-place digit of the input

  2. (double) The ones-place digit of the input

Function Description:

This function will take in a three-digit integer (integer between100 and999, inclusive) and should return each digit as a separate number. For example, if the input is472, the first output would be4, the second would be7and the third would be2.

Notes:

  • You only have to deal with three-digit positive integers.

  • Themod()andfloor() functions will do all the heavy lifting.

Homework 2 Functions

Function Name:clockHands

Inputs:

  1. (double)The current position of the hour hand

  1. (double)The current position of the minute hand

  1. (double) A positive or negative number of minutes

Outputs:

  1. (double) The position of the hour hand after the specified time

  1. (double)The position of the minute hand after the specified time

Background:

Itis not always immediately obvious where the hands of a clock will be after a certain

amount of time. It is even harder to visualize where the handswere some amount of time in the past. Luckily, this is not a very difficult problem for a computer to solve, so you will use that to your advantage.

Function Description:

Thisfunction will take in the current position of the hour hand, as an integer between 0

and11 (0 for noon/midnight), the current position of the minute hand, as an integer between0 and59 (0 for “on-the-hour”) and a positive or negative number of minutes elapsed.

Given this information, determine the new position of the clock hands. You should assume that the hour hand does not move until the next hour has begun. For example, the hour hand stays on “2” from 2:00 until 2:59 and only at 3:00 does the hour hand move to “3”.

Notes:

  • Themod() andfloor() functions will be useful.

  • As you do this problem notice the behavior ofmod() for negative inputs. This is a very important function in programming and will come up again and again in the class!

Hints:

  • One way of solving this problem involves calculating the total number of minutes after noon/midnight before and after the given minutes have elapsed.

  • Another way of solving it involves splitting the given number of minutes into a number of hours and a number of minutes.

  • Pick whichever method makes more sense to you (or come up with your own method).

Homework 2 – Functions Solution
$35.00 $29.00