1.MATH547 Homework 0

Topic: Math@UNC environment
Post date: May 9, 2020
Due date: May 13, 2020

1.1.Background

This homework is meant to familiarize yourself with basic operations within the Math@UNC environment, and is meant to be worked through in 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

Answer

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

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 Hilbert matrix is defined with components hij=1i+j-1,

𝑯=[ 𝒉1 𝒉2 𝒉m ],𝑯=[ h11 h12 h1m h21 h22 h2m hm1 hm2 hmm ]=[ 11 12 1m 12 13 1m+1 1m 1m+1 12m-1 ]

1.2.4.Octave session

Problem

Insert an Octave session and use the hilb function to display the Hilbert matrix 𝑯4×4.

Answer

octave] 
H=hilb(4); disp(H)

1.00000 0.50000 0.33333 0.25000

0.50000 0.33333 0.25000 0.20000

0.33333 0.25000 0.20000 0.16667

0.25000 0.20000 0.16667 0.14286

octave] 

1.2.5.Maxima session

Problem

Insert a Maxima session, and use the integrate function to compute the definite integral from Problem 2.1. When in the Maxima session, a menu of commonly used commands appears.

Answer

;;; Loading #P"/usr/lib/ecl-16.1.3/sb-bsd-sockets.fas"

;;; Loading #P"/usr/lib/ecl-16.1.3/sockets.fas"

;;; Loading #P"/usr/lib/ecl-16.1.3/defsystem.fas"

;;; Loading #P"/usr/lib/ecl-16.1.3/cmp.fas"

(%i2) 
integrate(sin(x),x,0,%pi);

(%o2) 2

(%i3) 

1.2.6.Gnuplot session

Problem

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

Answer

GNUplot] 
plot sin(cos(x))+cos(sin(x))

GNUplot] 

1.3.Data Science Application

Topic: Use least squares to 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.

octave] 
m=500; x=(0:m-1)/m; c0=-1; c1=1; yex=c0+c1*x; y=(yex+rand(1,m)-0.5)';
octave] 
plot(x,yex,x,y,'.')
octave] 
m=500

m = 500

octave] 
x=(0:m-1)/m;
octave] 
x(1:4)

ans =

0.0000000 0.0020000 0.0040000 0.0060000

octave] 

Repeat for different values of m,c0,c1.

Answer

1.3.2.Form the normal system

Problem

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

octave] 
X=ones(m,2); X(:,2)=x(:); N=X'*X; b=X'*y;
octave] 
X=ones(m,2);
octave] 
size(X)

ans =

500 2

octave] 
 X(:,2)=x(:);
octave] 
N=X'*X;
octave] 
size(N)

ans =

2 2

octave] 
b=X'*y;
octave] 
size(b)

ans =

2 1

octave] 

Repeat the above, one instruction at a time, and display the first two rows of 𝑿,𝑵,𝒃.

Answer

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

octave] 
c=N\b;
octave] 
size(c)

ans =

2 1

octave] 
c

c =

-1.0226

1.0165

octave] 
ytilde=X*c;
octave] 

1.3.4.Plot the result

Problem

The following Octave instructions generate an Encapsulated Postscript file showing the result of the linear regression. Familiarize yourself with the syntax and purpose of each instruction. The menu item Insert->Image->Big figure has been used to insert a figure environemnt in the Answer (Figure 1). Move your cursor inside Figure 1. Use menu item Insert->Image->Link image, to link to the Encapsulated Postscript file making the image width 4in, and leaving height empty. Edit the title of your figure.

octave] 
plot(x,y,'.k',x,ytilde,'og',x,yex,'r');
title('Linear regression example');
xlabel('x'); ylabel('y,yex,ytilde');
cd /home/student/courses/MATH547ML;
print hw00Fig01.eps;
octave] 

Answer

Figure 1.

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