Homework 1

Problem 1.1

In this exercise we will explore a simple application of the Plot[..] function. Consider four functions: 
sin(x), sin(x+(ℼ/5)), sin(x+((2ℼ)/5)), sin(x+((3ℼ)/5)) defined on the interval [0,6ℼ].

Plot these functions together in the same plot, using the options PlotTheme→”Business”, PlotLegends→”Expressions”, and PlotLabel->”Graph of the sin(x) function and its translates”.

Do a second plot of these functions, but this time choose your own styling options so that the even translations sin(x+k(ℼ/5)), k=0,2 are plotted in Black color, Thick lines, whereas the odd translations k=1,3 are plotted in Blue color, and Dashed lines. Include some appropriate PlotLegends as well.

Problem 1.2

In this exercise, we will explore 3D plots and Export options. Consider the curve in the plane defined parametrically by the following two equations: 
({{{, ({{x(t)=t+α sin(t),  }, {y(t)=-t-sin(t),  }})}})t∈[-2,2].  The parameter α is an integer number between -5 and 3.

Consider first α=-5. Use RevolutionPlot3D[..], with the option RevolutionAxis→{1,0,0}, to plot the surface of revolution that results when the curve is rotated about the x-axis. Use some extra options of RevolutionPlot3D[..]  like Axes, Boxed, ColorFunction, ImageSize, to hide the coordinate axes and the 3D bounding box, to set the ImageSize to 375x375 and choose a nice Color scheme. Use Export[..] to save the result 3D graphics as a .gif file.

Take the picture obtained and include it in the Word document graphics.doc given in the archive HW1-Files.zip.

Use the command Table[...] and RevolutionPlot3D[..] to generate a list with the 3D plots corresponding to all integer values of the parameter α  between -5 and 3. Use the Export[...] command to export the list to a .gif file. You will obtain an animated gif, just as in the Lecture Notes.

Take the picture obtained in part 1 and the animated gif obtained in part 2 and include them in the HTML file webpage.html provided in the archive.

Problem 1.3

In this exercise we will approximate a periodic function of period 2ℼ  with a trigonometric series in terms of sin(nx) and cos(nx), called a Fourier Series. Some details on Fourier series are given in the auxiliary file fourier.pdf. We will not discuss the mathematical basis of Fourier Series, as this is/will be discussed in depth in other math and physics classes, but for the purpose of this homework we assume that we can write
f(x)=((Subscript[a, 0])/2)+(Underoverscript[ ∑, (  n=1), (   ∞)]) (Subscript[a, n])cos(nx) + (Subscript[b, n])sin(nx) 
     =((Subscript[a, 0])/2) + (Subscript[a, 1])cos(x) + (Subscript[b, 1])sin(x) +(Subscript[a, 2])cos(2x) +(Subscript[b, 2])sin(2x) + (Subscript[a, 3])cos(3x) + (Subscript[b, 3])sin(3x)+ ...
and that the coefficients are given by the formulas
(Subscript[a, n]) = (1/π)(∫_(-π)^π)f(x)cos(nx)dx
(Subscript[b, n]) = (1/π)(∫_(-π)^π)f(x)sin(nx)dx
This approximation is valid in the range [-π , π].

Define the coefficients (Subscript[a, n]) and (Subscript[b, n]) as functions of f and n, so that you can use the coefficients later several times. For your convenience, the definition of (Subscript[a, n]) is already given below, as a model.

Clear[x,t, n,f,a,b];
a[n_,f_]:=1/π*Integrate[f[t]*Cos[n t],{t,-π,π}]

Next declare a function myFourierApprox[n_, f_, x_ ] of the arguments n, f and x, which constructs the Fourier series approximation of the function f[x] up to and including the terms in (Subscript[a, n])cos(nx) and (Subscript[b, n])sin(nx). Use your favorite Loop Sequence (Do While, For), or use Sum to add the first n terms of the Fourier series.

Then plot the function Sinh[x] and its Fourier Series with 20 and 50 terms in different colors in the range -π ≤ x ≤ π. When using the Plot function with the usual syntax, Plot[{f1[x],...f3[x]}, {x,-π,π},...], you may need to use Evaluate[..] in the first argument, Plot[Evaluate[{f1[x],...f3[x]}], {x,-π,π},...], so that you force the evaluation of the user-defined functions in the list before the sampling for the variable x takes place. Observe that the series with more terms is a better approximation but also oscillates wildly toward the end of the region.

Consider the piecewise function h[x] = 1 when 0≤x<π, and -1 when -π<x<0, defined below.  Plot the function h against its Fourier Series with 1, 5 and 20 terms in different colors in the range -π ≤ x ≤ π.

h[x_]:=If[0<x≤Pi,1,-1]

Save your pictures as pdf or eps using Export[...] and include them in the LATEX source file fourier.tex provided in the archive. Compile the source file with pdflatex to obtain a new pdf file, fourier.pdf, which contains your pictures.

Problem 1.4

Demonstrate that two matrices do not commute in general, i.e. that AB ≠ BA, by defining two 5x5 random matrices using the function Table, computing both sides (AB and BA) and comparing the outputs using the function TrueQ[...] in Mathematica. Of course, there exist matrices which do commute so you will have to choose your examples well. This means that you may have to generate several random 5x5 matrices, before you hit a pair for which AB ≠ BA.

Use free-form input to find some Mathematica functions which help you find the dimensions of a matrix which is not necessarily square (that is, the number of columns and rows).

Write a function g[A_,B_] that takes as input two matrices A and B (not necessarily square) and checks whether the number of columns of A is the same as the number of rows of B. If the dimensions match, then the function returns the product AB, otherwise it returns the message “Error, the matrices cannot be multiplied”.  Test your function on a couple of matrices.

Problem 1.5

Mathematica has built-in routines for finding the eigenvalues and eigenvectors (Real and Complex) of a numerical square matrix. If A is an n × n matrix, then the number λ is an eigenvalue of A if there exists a non-zero vector x such that Ax=λx, where λ is a given Real or Complex number is called the eigenvalue, and x is called an eigenvector of A with corresponding eigenvalue λ. There are at most n distinct eigenvalues of the matrix A, and at most n linearly independent eigenvectors with Real or Complex entries.

The function Eigenvalues[A] returns a list with the eigenvalues of the square matrix A.

Sometimes it is necessary to know the eigenvectors as well. This can be done using the function Eigensystem[A], which finds both eigenvalues and a complete linearly independent set of eigenvectors for each eigenvalue. The output is given in the form: { list of eigenvalues, list of eigenvectors }

Generate a square nxn matrix M, whose diagonal elements (Subscript[M, ii])are equal to n-1 and the other elements (Subscript[M, ij]) i≠j are equal to 1. Test it for n=10.

Use the function Eigensystem[...] to find the eigenvalues and a set of linearly independent eigenvectors for the matrix M that you defined in part I.

Define a matrix P whose columns are the eigenvectors returned by the function Eigensystem[...].

Compute the product (P^(-1))M P where (P^(-1))is the inverse of the matrix P. What do you observe? Compare it to the Jordan canonical form obtained by using JordanDecomposition[M].