1.MATH347 Homework 0

Topic: TeXmacs and Julia basics
Post date: May 15, 2024
Due date: May 16, 2024

1.1.Background

This homework is meant to familiarize yourself with basic operations within TeXmacs, a public-domain scientific editing platform. The TeXmacs website provides several tutorials. The key features of TeXmacs that motivate adoption of the platform for this course are:

1.2.Theoretical questions

1.2.1.Text editing in TeXmacs

Problem

Write an itemized list of ingredients in your favorite dessert recipe. (Menu->Insert->Itemize)

Answer

Tort Diplomat ingredients:

Figure 1. Tort Diplomat

1.2.2.Inline mathematics

Problem

The fundamental theorem of calculus states abf(x)dx=F(b)-F(a) for F'(x)=f(x). Apply this result for a=0, b=π, f(x)=sinx, F(x)=-cosx. Write your answer inline.

Answer

The integral is 0πsin(x)dx=-cos(π)+cos(0).

1.2.3.Displayed mathematics

Problem

A matrix is a row of column vectors, 𝑨=[ 𝒂1 𝒂2 𝒂n ]m×n, which can be expressed in terms of vector components as

𝑨=[ a11 a12 a1n a21 a22 a2n am1 am2 amn ].

Look up the definition of a Hilbert matrix 𝑯 and write in the above forms, both as a row of column vectors, and as components.

Answer

A Hibert matrix is a square matrix 𝑯m×m defined by

𝑯=[hi,j]=[1i+j-1]=[ 𝒉1 𝒉2 𝒉m ],1i,jm.

1.2.4.Julia session - working with numbers

Problem

Insert a Julia session and produce a table of the squares and cubes of the first ten natural numbers.

Answer

The squares and cubes of 1,2,,10 are:

i=1:10; [i.^1 i.^2 i.^3]

[ 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 ] (4)

1.2.5.Julia session - working with column vectors

Problem

Insert a Julia session and define the vectors

𝒖=[ 1 2 3 ],𝒗=[ -1 0 1 ],𝒘=[ 0 -1 0 ].

Answer

Define vectors

u=[1 2 3]';
v=[-1 0 1]';
w=[0 -1 0]';
[u v w]

[ 1 -1 0 2 0 -1 3 1 0 ] (5)

1.2.6.Julia session - working with row vectors

Problem

Insert a Julia session and define the vectors

𝒂=[ 1 2 3 ],𝒃=[ -1 0 1 ],𝒄=[ 0 -1 0 ].

Answer

Define vectors

a=[1 2 3];
b=[-1 0 1];
c=[0 -1 0];
[a; b; c]

[ 1 2 3 -1 0 1 0 -1 0 ] (6)

1.2.7.Julia session - assembling column vectors into a matrix

Problem

Insert a Julia session and define the matrix 𝑿=[ 𝒖 𝒗 𝒘 ].

Answer

Define vectors

u=[1 2 3]';
v=[-1 0 1]';
w=[0 -1 0]';
X=[u v w]

[ 1 -1 0 2 0 -1 3 1 0 ] (7)

1.2.8.Julia session - assembling row vectors into a matrix

Problem

Insert a Julia session and define the matrix

𝒀=[ 𝒂 𝒃 3𝒄 ]

Answer

Define vectors

a=[1 2 3];
b=[-1 0 1];
c=[0 -1 0];
Y=[a; b; 3*c]

[ 1 2 3 -1 0 1 0 -3 0 ] (8)

1.2.9.Julia session - componentwise definition of a matrix

Problem

Insert a Julia session and display the Hilbert matrix 𝑯4×4.

Answer

Define a Julia function

function hilb(m)
  H=ones(m,m)
  for i=1:m
    for j=1:m
      H[i,j]=1.0/(i+j-1)
    end
  end
  return H
end

hilb

hilb(4)

[ 1.0 0.5 0.3333333333333333 0.25 0.5 0.3333333333333333 0.25 0.2 0.3333333333333333 0.25 0.2 0.16666666666666666 0.25 0.2 0.16666666666666666 0.14285714285714285 ] (9)

1.2.10.Julia session - constructing plots

Problem

Insert a Julia session to plot the function f(x)=sin(cos(x))+cos(sin(x)).

Answer

Define f and plot it in Fig. 2

Figure 2. Plot of f(x)=sin(cos(x))+cos(sin(x)).

f(x) = sin(cos(x))+cos(sin(x));
x=0:0.01:2*pi; y=f.(x);
plot(x,y)

PyCall.PyObject[PyObject <matplotlib.lines.Line2D object at 0x33308acb0>]

xlabel("x"); ylabel("f(x)"); title("Plot of function f"); grid("on");
cd(homedir()*"/courses/MATH347DS/homework/hw00");
savefig("H00Fig01.eps");

1.3.Data Science Application

Carry out linear regression, i.e., fitting a line to data.

1.3.1.Generate synthetic data

Problem

The following generates data by random perturbation of points on a line y=c0+c1x.

m=20; x=(0:m-1)/m; c0=-1; c1=1; yex=c0 .+ c1*x;
y=yex .+ 0.5*(rand(m,1) .- 0.5);
clf(); plot(x,yex,"k",x,y,"r.");
cd(homedir()*"/courses/MATH347DS/homework/hw00");
savefig("H00Fig01.eps");

Repeat for different values of m,c0,c1.

Figure 3. Perturbation of points on a line.

Answer

1.3.2.Form the normal system

Problem

Define matrices 𝑿=[ 𝟏 𝒙 ], 𝑵=𝑿T𝑿, and vector 𝒃=𝑿T𝒚

Answer

Define 𝑿

X=[]; m=size(x)[1]; o=ones(m,1); X=[o x]; N=X'*X;
b=X'*y;

1.3.3.Solve the least square problem

Problem

Solve the system 𝑵𝒄=𝒃 by use of the Octave backslash operator c=N\b. Display the coefficient vector 𝒄, and compare to the values you chose in Question 3.1. Also compute 𝒚=𝑿𝒄, using ytilde as a notation.

Answer

The coefficients are

c = N \ b

[ -1.0390506761500047 1.0876860806750384 ] (10)

1.3.4.Plot the result

Problem

Plot the original line, perturbed points and linear regression of the perturbed points.

Answer

Costruct the plots and present them in Fig. 4.

yfit = c[1] .+ c[2]*x;
clf(); plot(x,yex,"k",x,y,"r.",x,yfit,"g");
xlabel("x"); ylabel("y");
savefig("H00Fig02.eps");

Figure 4. Comparison of least squares fit to original and perturbed data.

Submission instructions. Save your work, and also export to PDF (menu File->Export->Pdf). In Canvas submit the files: