Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
374 changes: 374 additions & 0 deletions HW3.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,374 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.6\n",
"\n",
"To prove ||.|| is a norm, we have to show that ||.|| satisfies the following three conditions:\n",
"\n",
"1) $ ||x|| \\geq 0 $ and $ ||x|| = 0 $ only if $ x = 0 $ \n",
"\n",
"2) $ ||x+y|| \\leq ||x|| + ||y|| $ (Triangle Inequality)\n",
"\n",
"3) $ || {\\alpha} x || = ||\\alpha||\\cdot ||x|| $\n",
"\n",
"Proof:\n",
"\n",
"1) $||y|| =1 $ so only if $ x= 0 $, $||x||' = 0$ and $ ||x||' $ is a kind of norm so $ ||x||' \\geq 0 $ \n",
"\n",
"\n",
"2) $||x+z||'= \\sup_{||y||=1}{| y^*(x+z)|}= \\sup_{||y||=1}{| y^*x + y^*z|} \\leq \\sup_{||y||=1}{|y^*x|} + \\sup_{||y||=1}{|y^*z|} = ||x||' + ||z||' $ Therefore,\n",
"\n",
"$||x+z||' \\leq ||x||'+||z||' $ (Triangle Inequality is satisfied.)\n",
"\n",
"3) For any scalars $\\alpha \\in C $, $ ||\\alpha x ||'=\\sup_{||y||=1}{|y^*(\\alpha x)|}= \\sup_{||y||=1}{|\\alpha| \\cdot |y^*x|} = |\\alpha| \\cdot \\sup_{||y||=1}{|y^*x|} = |\\alpha| \\cdot ||x||' $\n",
"\n",
"4.5\n",
"\n",
"$A*A$ is real and symmetric. It means that it has a complete set of orthongonal eigenvectors : $v_1,v_2,v_3,...v_n$. $V=[v_1,v_2,v_3,...,v_n] $ and $\\lambda_i$ is the corresponding eigenvalue to $v_i$. We assume $ \\lambda_1 \\geq \\lambda_2 \\geq \\lambda_3 \\geq ... \\geq \\lambda_n$. If the rank of A is $r$ then $\\lambda_{r+1} = \\lambda_{r+2}= ... = \\lambda_{n} = 0 $\n",
"\n",
"So $null(A^*A)=null(A)$.\n",
"\n",
"Let us define $ \\sigma_i = \\sqrt{\\lambda_i} $ and $u_i = \\frac{Av_i}{\\sigma_i} $ where $(i=1,2,...,r)$. Then $ {u_1,u_2,...,u_r} $ is an orthogonal set of vectors.\n",
"\n",
"$u_i^* u_j = \\frac{(Av_i)^* (Av_j)}{\\sigma_i \\sigma_j} = \\frac{ v_i^*(A^* A)v_j }{\\sqrt{\\lambda_i \\lambda_j}} = \\frac{ \\lambda_j v_i^* v_j }{ \\sqrt {\\lambda_i \\lambda_j } } = \\delta_{ij} $\n",
"\n",
"$U^*AV = [u_1,u_2,...,u_m]^* A [v_1,v_2,...,v_n] = (u_i^* A v_j)$\n",
"\n",
"$ (u_i^* A v_j) = $\n",
"$ \\begin {cases}\n",
" 0 & j > r \\\\\n",
" \\sigma_i \\delta_{ij} & j\\leq r\n",
" \\end {cases} $\n",
" \n",
" It shows that $ U^* A V $ is a diagonal matrix. So A has a real SVD.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"function Svd = SVD (A)\n",
" \n",
" ATA=A.'*A;\n",
" Trace = ATA(1,1)+ATA(2,2);\n",
" Determinant= det(ATA);\n",
" \n",
" L1= (1/2)*(Trace+ sqrt((Trace^2)-(4*Determinant)));\n",
" L2= (1/2)*(Trace- sqrt((Trace^2)-(4*Determinant)));\n",
" \n",
" SingVal1=sqrt(L1);\n",
" SingVal2=sqrt(L2);\n",
" \n",
" %To be able to write singular values in a descending order\n",
" if(SingVal1<SingVal2) \n",
" Temp=SingVal1;\n",
" SingVal1=SingVal2;\n",
" SingVal2=Temp;\n",
" endif\n",
" \n",
" \n",
" %To find eigenvectors associated with eigenvalues\n",
" \n",
" %First eigenvector\n",
" DeltaMatrix1=[(ATA(1,1)-L1),ATA(1,2);ATA(2,1),(ATA(2,2)-L1)];\n",
" \n",
" PV1=[-(DeltaMatrix1(1,2));(DeltaMatrix1(1,1))];\n",
" \n",
" Abs1=abs(PV1(1,1));\n",
" Abs2=abs(PV1(2,1));\n",
" Range=-1;\n",
" \n",
" if(Abs1<Abs2)\n",
" Range=Abs2;\n",
" else\n",
" Range=Abs1;\n",
" endif \n",
" \n",
" X=PV1(1,1);\n",
" Y=PV1(2,1);\n",
" \n",
" Divisor1=1;\n",
" for i= 2:Range\n",
" if(mod(int16(X),i)==0 && mod(int16(Y),i)==0)\n",
" Divisor1=i;\n",
" endif\n",
" endfor \n",
" \n",
" \n",
" V1=[PV1(1,1)/Divisor1;PV1(2,1)/Divisor1];\n",
" \n",
" Length1= sqrt(V1(1,1)^2+V1(2,1)^2);\n",
" \n",
" %First Eigenvector is VV1\n",
" VV1=[V1(1,1)/Length1;V1(2,1)/Length1];\n",
" \n",
" \n",
" %Finding Second Eigenvector\n",
" \n",
" DeltaMatrix2=[(ATA(1,1)-L2),ATA(1,2);ATA(2,1),(ATA(2,2)-L2)];\n",
" \n",
" PV2=[-(DeltaMatrix2(1,2));(DeltaMatrix2(1,1))];\n",
" \n",
" Abs1=abs(PV2(1,1));\n",
" Abs2=abs(PV2(2,1));\n",
" Range=-1;\n",
" \n",
" if(Abs1<Abs2)\n",
" Range=Abs2;\n",
" else\n",
" Range=Abs1;\n",
" endif \n",
" \n",
" X=PV2(1,1);\n",
" Y=PV2(2,1);\n",
" \n",
" Divisor2=1;\n",
" for i= 2:Range\n",
" if(mod(int16(X),i)==0 && mod(int16(Y),i)==0)\n",
" Divisor2=i;\n",
" endif\n",
" endfor \n",
" \n",
" V2=[PV2(1,1)/Divisor2;PV2(2,1)/Divisor2];\n",
" \n",
" Length2= sqrt(V2(1,1)^2+V2(2,1)^2);\n",
" \n",
" %Second Eigenvector is VV2\n",
" VV2=[V2(1,1)/Length2;V2(2,1)/Length2];\n",
" \n",
" \n",
" \n",
" %Building V Matrix. V matrix is eigenvectors of A.'*A.\n",
" VV1=VV1.';\n",
" VV2=VV2.';\n",
" V=[VV1;VV2];\n",
" \n",
" disp(\"Part a\");\n",
" V=V.'\n",
" \n",
" % A=U.SigmaMatrix.V* ===> U=A.V.(SigmaMatrix)^-1\n",
" \n",
" SigmaMatrix=[SingVal1,0;0,SingVal2];\n",
" InverseSigmaMatrix=inv(SigmaMatrix);\n",
" U=A*V*InverseSigmaMatrix\n",
" \n",
" %Svd=U*SigmaMatrix*V.'\n",
" \n",
" \n",
" \n",
" disp(\"Part c\")\n",
" \n",
" FirstRowSum= A(1,1)+A(1,2);\n",
" SecondRowSum= A(2,1)+A(2,2);\n",
" \n",
" FirstColumnSum= A(1,1)+A(2,1);\n",
" SecondColumnSum= A(1,2)+A(2,2);\n",
" \n",
" %To find 1-norm of A by Example 3.3 on textbook\n",
" \n",
" if(FirstColumnSum > SecondColumnSum)\n",
" MaxColumnSum=FirstColumnSum;\n",
" else\n",
" MaxColumnSum=SecondColumnSum;\n",
" endif\n",
" \n",
" %To find infinity norm of A by Example 3.4 on textbook\n",
" if(FirstRowSum>SecondRowSum)\n",
" MaxRowSum=FirstRowSum;\n",
" else\n",
" MaxRowSum=SecondRowSum;\n",
" endif\n",
" \n",
" \n",
" disp(\"The value of 1-norm of A equals to maximum column sum by Example 3.3 on textbook: \"),disp(MaxColumnSum)\n",
" disp(\"The value of 2-norm of A is maximum singular value : \"), disp(SingVal1)\n",
" disp(\"The value of Frobenius norm of A is square root of sum of squares of singular values: \"),disp(sqrt((SingVal1)^2+(SingVal2)^2))\n",
" disp(\"The value of infinity norm of A equals to maximum row sum by Example 3.4 on textbook: \"),disp(MaxRowSum)\n",
" disp(\"\")\n",
" disp(\"Part d\")\n",
" \n",
" disp(\"A=U*SigmaMatrix*V.' so Inv(A)= Inv(V.')*Inv(SigmaMatrix)*Inv(U)=V*Inv(SigmaMatrix)*U.'\")\n",
" disp(\"\")\n",
" InverseOfA=V*inv(SigmaMatrix)*U.'\n",
" \n",
" \n",
" disp(\"Part e\")\n",
" \n",
" Trace = A(1,1)+A(2,2);\n",
" Determinant= det(A);\n",
" \n",
" FirstEigenValueOfA= (1/2)*(Trace+ sqrt((Trace^2)-(4*Determinant)))\n",
" SecondEigenValueOfA= (1/2)*(Trace- sqrt((Trace^2)-(4*Determinant)))\n",
" disp(\"\")\n",
" \n",
" disp(\"Part f\")\n",
" \n",
" Mult1=FirstEigenValueOfA*SecondEigenValueOfA;\n",
" Mult2=SingVal1*SingVal2;\n",
" disp(\"Multiplication of the eigenvalues of A is \"),disp(int16(Mult1))\n",
" disp(\"Multiplication of the singular values of A is \"),disp(int16(Mult2))\n",
" disp(\"Determinant of A is\"),disp(det(A))\n",
" \n",
" disp(\"\")\n",
" disp(\"Part g\")\n",
" disp(\"The area of ellipsoid is multiplication of singular values so\"),disp(int16(Mult2))\n",
" \n",
" disp(\"Part b\");\n",
" \n",
" t=0:0.1:(2*pi);\n",
" \n",
" %Unit circle\n",
" x1=cos(t);\n",
" y1=sin(t);\n",
" \n",
" %Ellipse\n",
" \n",
" w=A*[x1;y1];\n",
" x2= w(1,:);\n",
" y2= w(2,:);\n",
" \n",
" %Right singular vectors\n",
" \n",
" v1=V(:,1);\n",
" v2=V(:,2);\n",
" \n",
" %Left singular vectors\n",
" \n",
" u1=U(:,1);\n",
" u2=U(:,2);\n",
" \n",
" %Scaled left singular vectors\n",
" \n",
" w1=SingVal1*u1;\n",
" w2=SingVal2*u2;\n",
" \n",
" %Plotting the unit cirlce and the right singular vectors\n",
" \n",
" figure(1);\n",
" axis([-1.2 1.2 -1.2 1.2]);\n",
" plot(x1,y1);\n",
" hold on;\n",
" plotv(v1, 'b');\n",
" hold on\n",
" plotv(v2, 'r');\n",
" grid on;\n",
" \n",
" %Plotting the ellipse and the scaled left singular vectors\n",
" figure(2);\n",
" axis([-3 3 -3 3]);\n",
" plot(x2,y2);\n",
" hold on;\n",
" plotv(w1,'b');\n",
" hold on;\n",
" plotv(w2,'r');\n",
" grid on;\n",
" \n",
" \n",
"endfunction"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Part a\n",
"V =\n",
"\n",
" 0.60000 0.80000\n",
" -0.80000 0.60000\n",
"\n",
"U =\n",
"\n",
" -0.70711 0.70711\n",
" -0.70711 -0.70711\n",
"\n",
"Part c\n",
"The value of 1-norm of A equals to maximum column sum by Example 3.3 on textbook: \n",
" 16\n",
"The value of 2-norm of A is maximum singular value : \n",
" 14.142\n",
"The value of Frobenius norm of A is square root of sum of squares of singular values: \n",
" 15.811\n",
"The value of infinity norm of A equals to maximum row sum by Example 3.4 on textbook: \n",
" 9\n",
"\n",
"Part d\n",
"A=U*SigmaMatrix*V.' so Inv(A)= Inv(V.')*Inv(SigmaMatrix)*Inv(U)=V*Inv(SigmaMatrix)*U.'\n",
"\n",
"InverseOfA =\n",
"\n",
" 0.050000 -0.110000\n",
" 0.100000 -0.020000\n",
"\n",
"Part e\n",
"FirstEigenValueOfA = 1.5000 + 9.8869i\n",
"SecondEigenValueOfA = 1.5000 - 9.8869i\n",
"\n",
"Part f\n",
"Multiplication of the eigenvalues of A is \n",
"100\n",
"Multiplication of the singular values of A is \n",
"100\n",
"Determinant of A is\n",
" 100\n",
"\n",
"Part g\n",
"The area of ellipsoid is multiplication of singular values so\n",
"100\n",
"Part b\n",
"error: 'plotv' undefined near line 202 column 5\n",
"error: called from:\n",
"error: SVD at line 202, column 5\n"
]
}
],
"source": [
"SVD([-2,11;-10,5])"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Octave",
"language": "octave",
"name": "octave"
},
"language_info": {
"file_extension": ".m",
"help_links": [
{
"text": "GNU Octave",
"url": "https://www.gnu.org/software/octave/support.html"
},
{
"text": "Octave Kernel",
"url": "https://github.com/Calysto/octave_kernel"
},
{
"text": "MetaKernel Magics",
"url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
}
],
"mimetype": "text/x-octave",
"name": "octave",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}