Write a function called smallelements that takes as input


Part -1:

Introduction to Programming with MATLAB

1. The function move_me is defined like this: function w = move_me(v,a). The first input argument v is a row-vector, while a is a scalar. The function moves every element of v that is equal to a to the end of the vector. For example, the command >> x = move_me([1 2 3 4],2); makes x equal to [1 3 4 2]. If a is omitted, the function moves occurrences of zeros.

2. Write a function called halfsum that takes as input an at most two-dimensional array A and computes the sum of the elements of A that are in the lower right triangular part of A, that is, elements in the counter-diagonal (going from the bottom left corner, up and to the right) and elements that are to the right of it. For example, if the input is [1 2 3; 4 5 6; 7 8 9], then the function would return 38.

3. Write a function called small_elements that takes as input an array named X that is a matrix or a vector. The function identifies those elements of X that are smaller than the product of their two indexes. For example, if the element X(2,3) is 5, then that element would be identified because 5 is smaller than 2 * 3. The output of the function gives the indexes of such elements found in column-major order. It is a matrix with exactly two columns. The first column contains the row indexes, while the second column contains the corresponding column indexes. For example, the statement indexes = small_elements([1 1; 0 4; 6 5], will make indexes equal to [2 1; 1 2; 3 2]. If no such element exists, the function returns an empty array.

4. Write a function called approximate_e that uses the following formula to compute e, Euler's number:

e = ∑k=0 1/k! = 1 + 1 + 1/2 + 1/6 + 1/24 +.....

Instead of going to infinity, the function stops at the smallest k for which the approximation differs from exp(1) (i.e., the value returned MATLAB's built-in function) by no more than the positive scalar, delta, which is the only input argument. The first output of the function is the approximate value of e, while the second is k. (Note: if your program or the grader takes a long time, you may have created an infinite loop and need to hit Ctrl-C on your keyboard.) You are not allowed to use the built-in function factorial.

5. Write a function called spiral_diag_sum that takes an odd positive integer n as an input and computes the sum of all the elements in the two diagonals of the n-by-n spiral matrix. For example, starting with the number 1 and moving to the right in a clockwise direction, a 5-by-5 spiral is formed as follows:

21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13

The sum of the red elements above is 101. Hint: the problem does not ask for the matrix itself. (Inspired by Project Euler.)

6. *** Write a function called triangle_wave that computes the sum

k=0n (-1)ksin((2k + 1)t)/(2k + 1)2

for each of 1001 values of t uniformly spaced from 0 to 4π inclusive. The input argument is a scalar non-negative integer n, and the output argument is a row vector of 1001 such sums-one sum for each value of t. You can test your function by calling it with n == 20 or greater and plotting the result and you will see why the function is called "triangle_wave".

7. *** Write a function max_product that takes v a vector and n, a positive integer, as inputs and computes the largest product of n consecutive elements of v. It returns the product and the index of the element of v that is the first term of the product. If there are multiple such products in v, the function must return the one with the smallest starting index. As an example, the following call

>> [product, ind] = max_product([1 2 2 1 3 1],3);

will assign 6 to product and 3 to ind since the max 3-term product in the input vector is 2*1*3. If v has fewer than n elements, the function returns 0 and -1, respectively.

8. *** Write a function called pendulum that is called like this: T = pendulum(L,a0), where all arguments are scalars and a0 is a positive number less than π. The function calculates the period T of a simple pendulum, which is the time required for a weight attached to a rod of length L and negligible weight to start from rest, swing with no friction under the influence of gravity from an initial angle a0, to - a0 and back to a0 again, as shown in the figure. The motion is determined by physics using the following definitions, where units [square brackets] are provided but are not needed:

798_Figure.jpg

θ = angle [radians]
ω = angular velocity [radians/s]
α = angular acceleration [radians/s2]
g = acceleration due to gravity = 9.8 [m/s2]
t = time [s]

The function starts its calculation with the pendulum angle θ equal to a0 and then calculates a sequence of decreasing pendulum angles, each at a time separated from the one before it by ?t = 1 × 10-6 s. It continues until the pendulum has passed its lowest point, at which θ = 0. The elapsed time equals T/4.

The calculation at each time step proceeds as follows: The angular acceleration α is set equal to -gsin θ⁄L . Then the angular velocity ω is increased by the product of the angular acceleration and ?t. That new angular velocity is then used to obtain a new θ by adding the product of the angular velocity and ?t to the old θ.
Here are two sample runs:

>> format long
>> T = pendulum(2, pi/2) T =
3.350344000012992
>> T = pendulum(0.22952, pi/4) T =
1.000000000000917

Part -2:

1. Write a function called integerize that takes as its input a matrix A of integers of type double, and returns the name of the "smallest" signed integer class to which A can be converted without loss of information. If no such class exists, the string 'NONE' is returned. For example, if the smallest element of A is -100 and the largest is +100, then the function would return 'int8'. As another example, if there is an element of A equal to -1e20, then the function would return 'NONE'.

2. Write a function called year2016 that returns a row-vector of struct-s whose elements correspond to the days of a month in 2016 as specified by the input argument. If the input is not an integer between 1 and 12, the function returns the empty array. Each struct should contain three fields with these (exact) field names: "month", "date", and "day" (all lower case).

- The month field must contain a string with the name of the month (first letter capitalized).
- The date field must contain a scalar specifying the day of the month.
- The day field must contain the three-letter abbreviation of the day chosen from this list: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'.
For example, here is a call of the function followed by a command that shows the seventh element of the struct array that is returned by the function:
>> m = year2016(2);
>> m(7)
ans =
month: 'February' date: 7
day: 'Sun'

3. *** A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Write a function that is called this way:
>> n = palin_product(dig,lim);

The function returns the largest palindrome smaller than lim that is the product of two dig digit numbers. If no such number exists, the function returns 0. (Inspired by Project Euler.)

4. Each number on telephone keypads, except 0 and 1, corresponds to a set of uppercase letters as shown in this list:

2 ABC, 3 DEF, 4 GHI, 5 JKL, 6 MNO, 7 PQRS, 8 TUV, 9 WXYZ

Hence, a phone-number specification can include uppercase letters and digits. Write a function called dial that takes as its input argument a string of length 16 or less that includes only these characters and returns as its output argument the telephone number as a uint64. Here is the input and output for one example of a call of the function:

Input: '1FUNDOG4YOU'
Output: 13863644968

You can assume that a phone number never starts with 0. If the input contains any illegal characters, the function returns 0. You are not allowed to use the built-in function strrep.

5. An n-by-n square logical matrix can be represented by a cell vector of n elements where the kth element corresponds to the kth row of the matrix. Each element of the cell vector is a row vector of positive integers in increasing order representing the column indexes of the logical true values in the given row of the matrix. All other elements in the given row of the logical matrix are false. Write a function called logiunpack that takes such a cell vector as its only input and returns the corresponding square logical matrix. For example, such a cell vector representation of a 100-by-100 logical matrix with the only true elements at indexes (10,20) and (10,75) would have only one non-empty element of the cell vector at index 10. That element is the vector [20 75].

6. Solve the inverse of the previous problem. That is, write a function called logipack that takes a square logical matrix as its only input argument and returns its cell vector representation as specified in the previous assignment. Note that empty array elements of the cell vector corresponding to rows with all false values must have a size of 0x0.

7. Write a function called centuries that takes a positive integer smaller than or equal to 3000 representing a year as its input and returns a string with the century the given year falls into. If the input is invalid, the function returns the empty string. Centuries are specified using roman numerals. Note that we require the shortest legal roman number.

For a complete list, refer to: https://www.romannumerals.co/roman-numerals-1-to-30. Note that a century goes from year 1 to 100, so for example, the XXth century ended on December 31st, 2000. As an example, the call
>> cent = centuries(1864);

will make cent equal to ‘XIX'.

8. Write the function find_zero that is defined like this function x = find_zero(f,x1,x2). The first input argument is special. It is a "function handle". A function handle is gotten by typing @ and the name of any function. For example, x = find_zero(@sin,-1,1) will give f the function handle for MATLAB's built-in sin function. Then, inside find_zero, the statement y = f(-1) would set y = sin(-1). Note that the @ sign is not used inside the function. Only the caller uses it. All other arguments to find_zero are scalar numbers, and x1 is less than x2.

1697_Figure1.jpg

The goal of the function is to find an x that lies in the range from x1 to x2 such that after the command, y = f(x), is executed inside the function find_zero, y is approximately zero as defined by abs(y) < 1e-10. All you know about the function f is that it has one scalar input and one scalar output, and a plot of its values crosses the x-axis exactly once between x1 and x2, as, for example, in the figure. It is the responsibility of the caller to call the function with arguments that obey these rules. Here are two sample runs:
>> find_zero(@sin,-2.5,2.3) % as shown in the figure ans =
-6.4000e-11
>> format long
>> find_zero(@cos,-2,1.3) ans =
-1.570796326871000

Note that you are not allowed to use the built-in function fzero. Hint: you may want to check the value of the function halfway between x1 and x2 and decide what to do next based on that.

Part -3:

1. Write a function called digit_counter that takes the name of a text file as input and returns the number of digits (i.e., any of the characters, 0-to-9) that the file contains. If there is a problem opening the file, the function returns -1. WARNING: if you use the ‘w' flag with fopen, as opposed to ‘r', you will overwrite the file. The grader uses your own m files to check your function, so be careful!

2. Write a function called day_counter that returns the number of Mondays that fell on the first day of the month in a given year between 1776 and 2016 inclusive where the requested year is the only input to your function and it is a positive integer scalar. Note that a leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. In a leap year, February has 29 days. You are not allowed to use the datenum built-in function. (Inspired by Project Euler.)

3. Write function called huge_add that adds together two positive integers of any length specified as strings using decimal notation. The single output argument is the result and it is a string as well. The inputs and output must contain digits only; no commas, spaces or any other characters are allowed. If any of these assumptions are violated by the input, the function returns the number -1.

4. 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. Write a function called smallest_multiple that returns a uint64, the smallest positive number that is evenly divisible by all of the numbers from 1 to n where n is a positive integer scalar and is the only input argument of the function. If the result would be greater than what can be represented as a uint64, the function returns 0. (Inspired by Project Euler.)

5. *** Write a function called maxproduct that takes a matrix A and a positive integer scalar n as inputs and computes the largest product of n adjacent elements in the same direction in A. That is, we are looking for products of consecutive elements in the same row, column, diagonal or reverse diagonal. The function must return an n-by-2 matrix containing the row and column indexes ordered first by row and then by column. If no such product exists, the function returns the empty array. For example, valid outputs for a max product of 3 neighbors in four different matrices might be [2 2; 2 3; 2 4] or [1 1; 2 1; 3 1] or [3 5; 4 4; 5 3] or [4 2; 5 3; 6 4]. If there are multiple products with the same maximum value, return the first one you find. (Inspired by Project Euler.)

6. *** If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. Write a function called number2letters that returns the number of letters needed to write down the number n in words. For example, 342 (three hundred forty two) contains 20 letters. Notice that we do not count spaces, nor do we use hyphens. The only input to the function is n, a positive integer smaller than 1000, but you do not need to check this. (Inspired by Project Euler.)

7. *** Write a function called circular_primes that finds the number of circular prime numbers smaller than n, where n is a positive integer scalar input argument. For example, the number, 197, is a circular prime because all rotations of its digits: 197, 971, and 719, are themselves prime. For instance, there are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. It is important to emphasize that rotation means circular permutation not all possible permutations.

8. *** Write a function that is called like this [E,N] = cyclotron(V). All arguments are scalars. The input argument is the voltage applied to a cyclotron (figure), which is a device that accelerates subatomic particles-in this case, positively charged isotopes of hydrogen, called "deuterons"-which spiral outward in a clockwise direction.

1970_Figure2.jpg

The cyclotron rapidly alternates the sign of the voltage difference V in units of volts between two "D"-shaped vacuum chambers (blue outlines), which are placed within a strong uniform magnetic field (not shown but perpendicular to the page). The deuteron is accelerated only as it is leaving one "D" and entering the other. While the deuteron is inside a given "D", it moves at a constant speed, and the magnetic field causes it to move on a semicircle. Each deuteron moves as follows (check the numbers in the figure): (1) It originates from a source (red dot) located at a distance s0 to the left of the center of the cyclotron, is accelerated vertically into the upper "D" and then moves on a semi-circle of radius r1 . (2) It leaves the upper "D" and is accelerated vertically downward into the lower "D" where moves with a larger radius r2. (3) It leaves the lower "D" and is accelerated vertically into the upper "D", etc, moving with ever increasing radii rn until (N) it is accelerated for the final time as it leaves the upper "D" and enters the lower "D", follows a semicircle of radius rN , and emerges from the cyclotron at the left. The formulas for the radii are as follows: r1 = √mV⁄(qB2) , m = deuteron mass = 3.344 × 10-27 kg, q = deuteron charge = 1.603 × 10-19 coulomb, and B = magnetic field strength = 1.600 tesla. For ≥ 2 , rn = √r2n-1 + 2mV⁄(qB2 ) . These expressions give the radii in units of meters, and s0 = r1⁄2 . The deuteron escapes through a window at the left that is placed so the particle cannot leave until it is more than 0.500 m to the left of the center of the cyclotron. The gap between the "D"s is exaggerated in the figure, has no effect, and can be assumed to be of zero width. The function returns energy E of the deuteron when it escapes in units of million electron volts (MeV), which equals VN × 10-6, and the number N of times the deuteron enters the "D"s.

HINT: Notice that the centers of the semicircles ≠ the center of the cyclotron.

Here is a sample run:
>> [E, N] = cyclotron(4.8e5) % depicted in figure

E = 16.32
N = 34

Request for Solution File

Ask an Expert for Answer!!
MATLAB Programming: Write a function called smallelements that takes as input
Reference No:- TGS01606039

Expected delivery within 24 Hours