Topic: | TeXmacs and Julia basics |
Post date: | May 15, 2024 |
Due date: | May 16, 2024 |
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:
Simple, efficient editing of mathematical content. The editor has a default text mode, and also a mathematics mode triggered by inserting an equation from the menu using Insert->Mathematics->(formula type), or the keyboard through key-strokes $, or Alt-Shift-$. Here is an example: the solution of the linear system with a symmetric matrix, , can be found by gradient descent
Sessions from other mathematical packages can be inserted directly into a document. Julia is used extensively in this course, and the menu item Insert->Session->Julia leads to creation of space within the document to execute Julia instructions. Define a matrix .
∴ |
A=[1 2 3; -1 0 1; 2 1 -2] |
(1)
Extend the Julia environment by adding a package to compute row echelon forms. This need be done only once.
∴ |
import Pkg; Pkg.add("RowEchelon"); |
Resolving package versions…
No Changes to
‘~/.julia/environments/v1.9/Project.toml‘
No Changes to
‘~/.julia/environments/v1.9/Manifest.toml‘
Load the RowEchelon package into the current session and invoke the rref function.
∴ |
using RowEchelon |
∴ |
rref(A) |
(2)
Compute the inverse of the matrix .
∴ |
inv(A) |
(3)
∴ |
Documents can readily be converted to other formats: PDF, LaTeX, HTML. All course documents, including the website are produced with TeXmacs.
Write an itemized list of ingredients in your favorite dessert recipe. (Menu->Insert->Itemize)
Tort Diplomat ingredients:
4 eggs
1 cup flour
1 cup sugar
125 ml vegetable oil
1 teaspoon baking powder
1⁄2 lemon, juice of
1⁄2 orange, zest of
1 1⁄2 teaspoons vanilla extract
The fundamental theorem of calculus states for . Apply this result for , , , . Write your answer inline.
The integral is
A matrix is a row of column vectors, , which can be expressed in terms of vector components as
Look up the definition of a Hilbert matrix and write in the above forms, both as a row of column vectors, and as components.
A Hibert matrix is a square matrix defined by
Insert a Julia session and produce a table of the squares and cubes of the first ten natural numbers.
The squares and cubes of are:
∴ |
i=1:10; [i.^1 i.^2 i.^3] |
(4)
∴ |
Insert a Julia session and define the vectors
Define vectors
∴ |
u=[1 2 3]'; |
∴ |
v=[-1 0 1]'; |
∴ |
w=[0 -1 0]'; |
∴ |
[u v w] |
(5)
∴ |
Insert a Julia session and define the vectors
Define vectors
∴ |
a=[1 2 3]; |
∴ |
b=[-1 0 1]; |
∴ |
c=[0 -1 0]; |
∴ |
[a; b; c] |
(6)
∴ |
Insert a Julia session and define the matrix
Define vectors
∴ |
u=[1 2 3]'; |
∴ |
v=[-1 0 1]'; |
∴ |
w=[0 -1 0]'; |
∴ |
X=[u v w] |
(7)
∴ |
Insert a Julia session and define the matrix
Define vectors
∴ |
a=[1 2 3]; |
∴ |
b=[-1 0 1]; |
∴ |
c=[0 -1 0]; |
∴ |
Y=[a; b; 3*c] |
(8)
∴ |
Insert a Julia session and display the Hilbert matrix .
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(4) |
(9)
∴ |
Insert a Julia session to plot the function .
Define and plot it in Fig. 2
∴ |
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"); |
∴ |
Carry out linear regression, i.e., fitting a line to data.
The following generates data by random perturbation of points on a line .
∴ |
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 .
Define matrices , , and vector
Define
∴ |
X=[]; m=size(x)[1]; o=ones(m,1); X=[o x]; N=X'*X; |
∴ |
b=X'*y; |
∴ |
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.
The coefficients are
∴ |
c = N \ b |
(10)
∴ |
Plot the original line, perturbed points and linear regression of the perturbed points.
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"); |
∴ |
Submission instructions. Save your work, and also export to PDF (menu File->Export->Pdf). In Canvas submit the files:
hw00.tm
hw00.pdf