Begin Sci Comp Notes. When uploading your work, be sure to choose correct pages, without missing any, for each problem. Besides, make sure all pages are

Begin Sci Comp Notes.

When uploading your work, be sure to choose correct pages, without missing any, for each problem. Besides, make sure all pages are

Click here to Order a Custom answer to this Question from our writers. It’s fast and plagiarism-free.

Begin Sci Comp Notes.

When uploading your work, be sure to choose correct pages, without missing any, for each problem. Besides, make sure all pages are in order, no page is rotated, and none of your code or hand-written work is cut out.
According to our homework policies, you are allowed to collaborate for homework assignments in a group of up to 5 students. If you did, please list all your collaborators at the top of your homework, below your name.
Homework must be prepared using a live script (.mlx file). Submission generated using any other means (e.g., Words with MATLAB screenshots) will not be accepted.
Starting from this assignment, you will be asked to write MATLAB functions. Unlike scripts, functions can be included inside your mlx file at the very end of the document. So you no longer need to write an external m-file and print its content using the type function. For more detailed instruction, please read 

[Guide] How to Embed Functions in Live Scripts:
Method 1: Using Code Example Environment
Suppose that you have written a function as a separate function m-file, myfoo.m and now you want to include its contents within your homework live script, HW2_Kim.mlx. At the top of MATLAB window, click on Insert > Code Example. It creates a blank box. Any text typed within this box will be formatted in monospaced fonts with syntax highlighting.

The box is not an executable code block. 
If you make changes to myfoo.m, you need update the code example block accordingly (by copying and pasting).

Method 2: Using TYPE Function
Still suppose that you have a function written as a separate m-file titled myfoo.m. Then you can simply type
type myfoo
within a code block. The contents of myfoo.m will be printed out in your live script.

We have been using this to print out the contents of external script m-files. This works in the exact same way for function m-files.


If you forget to run that block after your final edit, your live script will show an outdated version of your function.
As you complete more and more assignments, your folders will be filled with more m-files and may be difficult to manage unless you are well-organized.

Method 3 (Recommended): Writing Functions at the End of Live Script
Instead of writing a separate m-file, you may just write the function at the very bottom of the document. The functions are local to the live script, and so you are able to call the function anywhere inside the live script. This way, your mlx file is fully self-contained and you no longer need to go back and forth between Editor and Live Editor. See the live script accompanying Lecture 9; all functions defined for the live script are gathered in the last section titled “Functions Used”.
This is my recommended method!
The only caveat I can think of:

Suppose Problem 1(a) asks you to write a function, and so you write it at the end of your live script. You then exports it to a pdf file and find out that Problem 1 solutions are on pp. 1–2, with the function written for 1(a) appearing on p. 7. In this case, when you upload the pdf file to Gradescope, you have to select pages 7, 1, and 2 for Problem 1, in that order.

Guide video link: Updated on September 26, 2021

Math 3607: Homework 4
Due: 10:00PM, Tuesday, September 28, 2021

TOTAL: 30 points

• Problems marked with v are to be done by hand; those marked with � are to be solved using a

• Important note. Do not use Symbolic Math Toolbox. Any work done using sym or syms will receive
NO credit.

• Another important note. Starting from this assignment, you will be asked to write MATLAB
functions. Instead of writing an external function m-file, include all your functions at the end of your
live script.

1. (Sliders moving along grooves; adapted from LM 2.1–12 and Sample HW01) The mechanical
device shown in Figure 1 consists of two grooves in which sliders slide. These sliders are
connected to a straight rod.







Figure 1: The bronze square is a piece
of metal with two grooves cut out of
it as shown. There are sliders at the
points A and B which slide in these
grooves. The slider at A can only slide
vertically, and the one at B can only
slide horizontally. There is a straight
rod attached at A and B, which ex-
tends to C. As the point C moves
around the block, it traces out a closed

(a) v Analytically, determine the curve which is traced out by C in one rotation.
Suggestion. Let px,yq be the coordinates of the point C. Express the variables x,y in

terms of L, `, and θ, where θ P r0, 2πq is the angle from the part of the horizontal
groove which is to the right of B to the rod BC.

(b) � Using the previous result, plot the trajectory of C in one rotation for ` “ 2 and L “ 7.

2. (Spiral triangles to spiral polygons; adapted from LM 5.9–7, 6.8–34) � The following script1
generates spirals using equilateral triangles as shown in the figure below.

1It is slightly modified from the code included in Lecture 9 slides. Note the introduction of a new variable d_rot,
which is accountable for the rotation of the innermost triangle.


m = 21; d_angle = 4.5; d_rot = 90;
th = linspace(0, 360, 4) + d_rot;
V = [cosd(th);

C = colormap(hsv(m));
s = sind(150 – abs(d_angle))/sind(30);
R = [cosd(d_angle) -sind(d_angle);

sind(d_angle) cosd(d_angle)];
hold off
for i = 1:m
if i > 1

V = s*R*V;
plot(V(1,:), V(2,:), ’Color’, C(i,:))
hold on

set(gcf, ’Color’, ’w’)
axis equal, axis off

Figure 2: Spiral triangles with
m “ 21 and θ “ 4.5˝.

(a) Write a function named spiralgon by modifying the script so that it generates spirals
using m regular n-gons for any n ě 3. Your function must be written at the end of
your homework live script (.mlx) file. Begin the function with the following header and

function V = spiralgon(n, m, d_angle, d_rot)
% SPIRALGON plots spiraling regular n-gons
% input: n = the number of vertices
% m = the number of regular n-gons
% d_angle = the degree angle between successive n-gons
% (can be positive or negative)
% d_rot = the degree angle by which the innermost n-gon
% is rotated
% output: V = the vertices of the outermost n-gon

(b) Run the statements below to generate some aesthetic shapes.

subplot(2, 2, 1), spiralgon(3, 41, 4.5, -90);
subplot(2, 2, 2), spiralgon(4, 37, -2.5, 45);
subplot(2, 2, 3), spiralgon(5, 61, 3, -90);
subplot(2, 2, 4), spiralgon(8, 91, -4, 22.5);

Note. Copy the five lines, paste them inside a single code block, and run it. This code
block must precede your function(s).

3. (Machine epsilon; adapted from LM 9.3–3(a)) � Recall that the number in the computer
which follows 1 is 1 ` eps , which can be verified in MATLAB by

>> format long


>> (1 + 0.5*eps) – 1
ans =

>> (1 + 0.51*eps) – 1
ans =


In the same manner:

(a) Verify that the number in the computer which follows 8 is 8 ` 8 eps by numerically
calculating 8 ` 4 eps and 8 ` 4.01 eps .

(b) Verify that the number in the computer which precedes 16 is 16 ´ 8 eps by numerically
calculating 16 ´ 4.01 eps and 16 ´ 4 eps .

(c) What are the numbers in the computer that precedes and follows 210 “ 1024, respec-
tively? Verify your claims in MATLAB by carrying out appropriate calculations.

Note. Begin with format long as shown in the example above. This is needed only once
before the beginning of part (a).

Note. Answer each part of the problem in a single code block. No external script needs to
be written.

4. (Catastrophic cancellation; LM 9.3–10) We revisit the function from Problem 3 of Homework
3. Consider the function

fpxq “




ex ´ 1

if x ‰ 0

1 if x “ 0,

where we are interested in exploring the catastrophic cancellation which occurs as x Ñ 0
since ex Ñ 1 as x Ñ 0.

(a) v Use the Taylor series expansion of ex to prove that f is continuous at 0.
(b) � Now calculate fpxq numerically for x “ 10´k where k P Nr1, 20s in three slightly

different ways:
i. Calculate fpxq as written.
ii. Calculate it as

f1pxq “
ex ´ 1
log ex

, for x ‰ 0.

(You and I know that analytically f1pxq ” fpxq for all nonzero x – but MATLAB

iii. MATLAB has a function which analytically subtracts 1 from the exponential to avoid
catastrophic cancellation before the result is calculated numerically. So define the
function f2pxq to be the same as fpxq except that ex ´ 1 is replaced by expm1(x).

Tabulate the results using disp or fprintf. The table should have four columns with
the first being x, the second using fpxq, the third using f1pxq, and the fourth using f2pxq,
with all shown to full accuracy. Do it as efficiently as you can, without using a loop.
Note. Write your code for this part in a single code block. No external script needs to

be written.


(c) v Comment on the results obtained in the previous part. Explain why certain methods
work well while others do not.

5. (Inverting hyperbolic cosine; FNC 1.3.6) The function

x “ coshptq “
et ` e´t


can be inverted to yield a formula for acoshpxq:

t “ log

x ´

x2 ´ 1

. (‹)

In MATLAB, let t=-4:-4:-16 and x=cosh(t).

(a) v� Find the condition number of the problem fpxq “ acoshpxq by hand. (You may
use Equation (‹), or look up a formula for f 1 in a calculus book.) Then evaluate κf at
the elements of x in MATLAB.

(b) � Evaluate the right-hand side of Equation (‹) using x to approximate t. Record the
accuracy of the answers (by displaying absolute and/or relative errors), and explain.
(Warning: Use format long to get enough digits or use fprintf with a suitable

(c) � An alternate formula for acoshpxq is

t “ ´2 log


x ` 1



x ´ 1


. (:)

Apply Equation (:) to x and record the accuracy as in part (b). Comment on your

(d) v Based on your experiments, which of the formulas (‹) and (:) is unstable? What is
the problem with that formula?

Note. Write your code for each of parts (a), (b), and (c) in a single code block. No external
script needs to be written.


Looking for this or a Similar Assignment? Click below to Place your Order