347 views

Original Title: hw6

Uploaded by jaimin

- Transformations Processing 2D
- Math Review Handout
- gtw ap ni
- Andrzej Roslanowski and Saharon Shelah- Partition Theorems from Creatures and Idempotent Ultrafilters
- 06 Mathematics.pdf
- Aequationes paper, grievance procedures.
- Shifting a Graph AVF
- 6 multimedia lesson idea 2016
- Sales & Distribution - Basic Functions - Partner Determination
- tpack-lesson-plan-1
- Math Review Handout
- Opera
- Machine Problem 5-8
- Neuro Fuzzy Expert System & Matlab
- u4 - functions rules tables graphs mapping1
- 1.1 Functions and Relation
- 2008 Mathematical Methods (CAS) Exam Assessment Report Exam 1
- Year 9 Text Book.pdf
- Possible UPCAT Coverage.pdf
- 1-5

You are on page 1of 3

Lesson 6

• Unless otherwise indicated, you may assume that each function will be given the correct number of inputs

and that those inputs have the correct dimensions. For example, if the input is stated to be three row

vectors of four elements each, your function is not required to determine whether the input consists of

three two-dimensional arrays, each with one row and four columns.

• Unless otherwise indicated, your function should not print anything to the Command Window, but your

function will not be counted incorrect if it does.

• Note that you are not required to use the suggested names of input variables and output variables, but you

must use the specified function names.

• Also, read the instructions on the web page on how to test your functions with the auto-grader program

provided and what to submit to Coursera to get credit.

• Note that starred problems, marked by ***, are harder than usual, so do not get discouraged if you have

difficulty solving them.

• You need MATLAB r2012a or newer or MATLAB Online to run the grader! Older versions are not supported.

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], then

the function would return 21.

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:

∞

1 1 1 1

𝑒𝑒 = � =1+1+ + + +⋯

𝑘𝑘! 2 6 24

𝑘𝑘=0

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

𝑛𝑛

(−1)𝑘𝑘 sin�(2𝑘𝑘 + 1)𝑡𝑡�

�

(2𝑘𝑘 + 1)2

𝑘𝑘=0

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.

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:

>> T = pendulum(2, pi/2)

T =

3.350344000012992

>> T = pendulum(0.22952, pi/4)

T =

1.000000000000917

3

Fitzpatrick and Ledeczi Introduction to Programming with MATLAB Vanderbilt University

- Transformations Processing 2DUploaded byIván Fuentes
- Math Review HandoutUploaded byDavid Reaver
- gtw ap niUploaded byRizka Amelia
- Andrzej Roslanowski and Saharon Shelah- Partition Theorems from Creatures and Idempotent UltrafiltersUploaded byUwem356
- 06 Mathematics.pdfUploaded bySubhraJyotiBorah
- Aequationes paper, grievance procedures.Uploaded bytradutora
- Shifting a Graph AVFUploaded bysound05
- 6 multimedia lesson idea 2016Uploaded byapi-346660186
- Sales & Distribution - Basic Functions - Partner DeterminationUploaded byAnvit Sah
- tpack-lesson-plan-1Uploaded byapi-253433937
- Math Review HandoutUploaded byDavid Reaver
- OperaUploaded byLuis Fernandez
- Machine Problem 5-8Uploaded byJan Abanes-Dating
- Neuro Fuzzy Expert System & MatlabUploaded byHouw Liong The
- u4 - functions rules tables graphs mapping1Uploaded byapi-297021169
- 1.1 Functions and RelationUploaded byVon A. Damirez
- 2008 Mathematical Methods (CAS) Exam Assessment Report Exam 1Uploaded bypinkangel2868_142411
- Year 9 Text Book.pdfUploaded byJoe Idfc
- Possible UPCAT Coverage.pdfUploaded bySherly Tibal
- 1-5Uploaded bysarasmile2009
- Fundamentals II Sheet 2 With Answer (1)Uploaded byButhaina Jabir
- Matlab CourseUploaded by_Leonel_
- Computer ProjectUploaded bysurya
- pratt mm2keyassignmentUploaded byapi-388646942
- artifact2discoverylessonplanUploaded byapi-301047801
- 3D GeometryUploaded bybmkale
- 5 blog lesson idea 2016Uploaded byapi-346731299
- Circular MeasureUploaded bysewcin
- Functions VideosUploaded bymomathtchr
- settheoryUploaded bybaigbest

- rushilUploaded byjaimin
- Seperation of Olefins (Autosaved)Uploaded byjaimin
- 5791966Uploaded byjaimin
- 5791966Uploaded byjaimin
- hw7.pdfUploaded byGeorgeSidiras
- hw7.pdfUploaded byGeorgeSidiras
- Mix Sulphuric Acid (Autosaved)Uploaded byjaimin
- 65472800_1455334277210Uploaded bySwapnil Tripathi
- hw6Uploaded byjaimin
- ET_1402801050(18-21)-Grp no4Uploaded byjaimin
- Research Report Reliance IndustriesUploaded byjaimin
- Coursera XVDHNNHD8WEPUploaded byjaimin
- Uptowork Com Mycv JaiminpandyaUploaded byjaimin
- handout1Uploaded byjaimin
- ProbabilityUploaded byTarun Pandey
- Seperation of Olefins (Autosaved)Uploaded byjaimin
- Ce TicketUploaded byjaimin
- Abstract Guidelines ICCI 2017 (3)Uploaded byjaimin
- Review PaperUploaded byjaimin
- Instrumental 2Uploaded byjaimin
- Das Mita 200912 PhdUploaded byjaimin
- Readme.txtUploaded byjaimin
- ElectrochemistryUploaded byjaimin
- Phase RuleUploaded byjaimin
- Toolbox Equations FlowserveUploaded bydhanu_aqua
- 07 Chapter 2Uploaded byjaimin
- Module 5Uploaded bysmartcomsci
- Smith-Chapter 16.pdfUploaded byjaimin
- lecture3.pdfUploaded byjaimin

- Introduction to Molecular Dynamics Simulations. Prabal MaitiUploaded byCornersouth
- Dynamic Slope CoupledUploaded byjohntptorres
- 1-6 practice_bUploaded byStanley
- An Application of Robust Feedback Linearization to a Ball and Beam Control ProblemUploaded byE Daniel Reyes
- Machine Learning: Reproducing Kernel Hilbert SpacesUploaded byAde-Bello Abdul-Jelili
- Lecture-23 (Shear Force Diagram & Bending Moment Diagram)Uploaded byrajmaljat
- Module 4-2 Principal Components Analysis PPTUploaded byRubayat Islam
- Splines AlternateUploaded byharris
- THE CAM SYNTHESIS WITH MAXIMAL EFFICIENCY, Florian Ion PETRESCU, Relly Victoria PETRESCU, Constantin OCNARESCUUploaded byPetrescu Florian
- M5L30Uploaded byabimana
- Multivariable Functional Interpolation and Adaptive NetworksUploaded byBruno Farina
- stratigys for intergrationUploaded bytmkzppp
- Mathematics SSC II Paper IIUploaded bySoniyaKanwalG
- Meshfree Chapter 6Uploaded byZenPhi
- Extremum Seeking for Constrained InputsUploaded byBedadipta Bain
- reportUploaded byapi-303634380
- A First Course in Machine Learning - ErrataUploaded bysynchronicity27
- MathematicsUploaded byela
- SPMA07Uploaded byAnonymous 1aqlkZ
- Solve Linear Programming Problems - MATLAB Linprog - MathWorks IndiaUploaded bysharmi
- AQA-MPC2-QP-Jan13.pdfUploaded byZa Afghan Yum
- GACE Mathematics Notations, Definitions, And FormulasUploaded bylolhaha321
- 9783319610207-c2Uploaded bypreethu02
- ch25[1]Uploaded bysmprabu24317
- B Spline TheoryUploaded byLim Andrew
- Topology General & Algebraic (2009) - (Malestrom).pdfUploaded byratchagar a
- 3_1_solving_linear_equatns.pdfUploaded bytarek mahmoud
- lectfiniteDifference.pdfUploaded byGodwin Larry
- Enhanced Collaborative Optimization Application to an Anaytic Test Problem and Aircraft DesignUploaded byAnonymous vWokZszg
- All Sub QuesUploaded byMohammed Salman Khalander