Homework Matlab
Homework 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
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.
2
Fitzpatrick and Ledeczi Introduction to Programming with MATLAB Vanderbilt University
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, a0 a0
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:
= 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
sin 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:
3
Fitzpatrick and Ledeczi Introduction to Programming with MATLAB Vanderbilt University