0 Up votes0 Down votes

347 views3 pagesewd

Dec 25, 2017

© © All Rights Reserved

PDF, TXT or read online from Scribd

ewd

© All Rights Reserved

347 views

ewd

© All Rights Reserved

- Neuromancer
- The E-Myth Revisited: Why Most Small Businesses Don't Work and
- How Not to Be Wrong: The Power of Mathematical Thinking
- Drive: The Surprising Truth About What Motivates Us
- Chaos: Making a New Science
- The Joy of x: A Guided Tour of Math, from One to Infinity
- How to Read a Person Like a Book
- Moonwalking with Einstein: The Art and Science of Remembering Everything
- The Wright Brothers
- The Other Einstein: A Novel
- The 6th Extinction
- The Housekeeper and the Professor: A Novel
- The Power of Discipline: 7 Ways it Can Change Your Life
- The 10X Rule: The Only Difference Between Success and Failure
- A Short History of Nearly Everything
- The Kiss Quotient: A Novel
- The End of Average: How We Succeed in a World That Values Sameness
- Made to Stick: Why Some Ideas Survive and Others Die
- Algorithms to Live By: The Computer Science of Human Decisions
- The Universe in a Nutshell

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

- AFEM.AppGUploaded byKim Novis
- Geometry & MeasurementUploaded bysuja_srini
- Lab 10 Pendulum ReportUploaded byBrian Kim
- Machine Problem 5-8Uploaded byJan Abanes-Dating
- sample examUploaded byChris Thomas
- AP3114 Lecture WK3 ProgrammingUploaded byGuoXuanChan
- Algebra 2 Learning Guide for Module 10 TrigUploaded bySmart StudyMan
- Assignment 1Uploaded byHua Khien
- Mental Diseases of RobotsUploaded byAnonymous qCk2ycn
- DiscreteUploaded byPietie Pieterse
- DLL 17aUploaded byLaen Nayrb Ladner
- trial perak.pdfUploaded bykmlsnmj
- Linear 2017 - NonLinear (EDIT).xlsxUploaded byArief Fath
- 7 - College Physics Rotational MotionUploaded byMaria Do Céu
- Mathematics 7Uploaded byLeny Fantilanan Magbanua
- Unit 1 Curriculum Map: SurvivalUploaded byNomadic Edventures
- gen math diagnostic testUploaded byGladzangel Loricabv
- Circular Measure3Uploaded byCt Kursiah
- MBTChapt1001Uploaded bySai Harika
- Matlab Lab5Uploaded byda_masta_j
- FunctionsUploaded byPedroBrom
- AimUploaded byRajesh Ram
- 2016 G6NA ScienceUploaded byaellani
- UnknownUploaded bypedrito sola
- g8m4l4a8- writing and solving eqns involving anglesUploaded byapi-276774049
- pillet1997.pdfUploaded byelyesyoussef
- NCERT Solutions Class 12 Maths Chapter 5 Continuity and DifferentiabilityUploaded byVidyakul
- Vector.fusion.basics.2Uploaded byEdgar Pugal
- sect11_1.pdfUploaded byDhanishan Indoi
- Lecture-1.pdfUploaded byVasudeva Singh Dubey

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

- MG6006 Concrete PracticeUploaded byNajif Ismail
- TPD2125Uploaded byAndres Sorin
- AR-Interin-CM Final Draft ReportUploaded byosvaldo_delima5918
- Journals Rubric (2)Uploaded bygongmaozhe
- Linux Command Line ReferenceUploaded byradetabosko
- 2011 ARM Physical IP Seminar Taiwan Mickie LiuUploaded byMohith Thankachan
- p27Kip1 Signaling- Transcriptional and Post-translational RegulationUploaded byRomana Masnikosa
- Eharian PoemUploaded byMrsGame
- Software Development FolderUploaded bySabahat Hussain
- Multi-SSID Deployment ConsiderationsUploaded byFarrukhmohd
- Arun Kumar Mishra CvUploaded byarun
- EMONITOR.pdfUploaded byadolfo lalala
- Effect of Mn for Corrosion in Low Carbon SteelUploaded byPrabath Madusanka
- Lab ManualUploaded byonlyviki
- Corrosion Journal (2) APKOMUploaded byradit_azzahra7537
- Corporate OverviewUploaded byRajeev Varma
- Baseball Digest - October 2016Uploaded byAakash Banthia
- DBMS MANUAL-2017 (01-08-17)Uploaded byMahesh Tadalapur
- Find Number of ElectronsUploaded byeppalapalli
- 13 Proso Millet for Grain and ForageUploaded byGérard Menfin
- Outsourcing - A Guide for Small and Medium BusinessesUploaded byPao Shen
- ScanPal2 App Gen GuideUploaded bydatasoftbn
- 4024_s07_ms_1Uploaded bySuaidahRahim
- Order to CashUploaded byUpinder Kaur
- 06 Sema Mon18Uploaded byoscar
- 2016-06-02 Freedom of Information Law Request - Robert Newman (7-8)Uploaded byProgress Queens
- quality_policy_nestle.pdfUploaded byJonathan Kacou
- Asian Timber Certified - Advancing Sustainable Trade in AsiaUploaded byPEFC UK Ltd.
- Mangalore City Building Byelaws 2007Uploaded byUnmilan Ghosh
- fluid mechanicsUploaded byRashmiAnoop

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.