{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 9: Linear Least Squares Problem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 9.1: Moore_Penrose_Generalized_Inverse.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 9.1\n", "//Moore-Penrose Generalized Inverse\n", "//Page no. 292\n", "clc;clear;close;\n", "\n", "AT=[3,0,3;0,3,3];\n", "A=AT'; //transpose\n", "I=inv(AT*A); //inverse\n", "disp(I,'Inverse of AT*A=',AT*A,'AT*A=',A,'A=',AT,'AT=');\n", "A#=I*AT;\n", "disp(A#,'Moore-Penrose Generalized Inverse of A=')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 9.2: Curve_Fitting.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 9.2\n", "//Curve Fitting\n", "//Page no. 293\n", "clc;clear;close;\n", "x(1)=0.25;\n", "for i=2:6\n", " x(1,i)=x(1,i-1)+0.25;\n", "end //x values\n", "y(1,1)=3.1;y(1,2)=1.7;y(1,3)=1;y(1,4)=0.68;y(1,5)=0.42;y(1,6)=0.26; //y values\n", "\n", "//construction of normal equations\n", "for i=1:6\n", " Y(1,i)=log10(y(1,i));\n", "end\n", "Ex=0;\n", "for i=1:6\n", " Ex=Ex+x(1,i);\n", "end\n", "EY=0;\n", "for i=1:6\n", " EY=EY+Y(1,i);\n", "end\n", "Ex2=0;\n", "for i=1:6\n", " Ex2=Ex2+x(1,i)^2;\n", "end\n", "ExY=0;\n", "for i=1:6\n", " ExY=ExY+x(1,i)*Y(1,i);\n", "end\n", "printf('E x(k)\t\t y(k)\t\tE Y(k)\t\tE x2(k)\t\tE x(k)*Y(k)')\n", "printf('\n----------------------------------------------------------------------------')\n", "for i=1:6\n", " printf('\n%f\t%f\t%f\t%f\t%f',x(1,i),y(1,i),Y(1,i),x(1,i)^2,x(1,i)*Y(1,i))\n", "end\n", "printf('\n----------------------------------------------------------------------------')\n", "printf('\n%f\t%f\t%f\t%f\t%f',Ex,0,EY,Ex2,ExY)\n", "printf('\n----------------------------------------------------------------------------\n\n')\n", "A=[6,Ex;Ex,Ex2]; //system of normal equations\n", "B=[EY;ExY];\n", "X=inv(A)*B;\n", "a=exp(X(1,1));\n", "b=-1*X(2,1);\n", "for i=1:2\n", " for j=1:2\n", " printf('%f ',A(i,j))\n", " end\n", " if(i==1)\n", " printf(' *')\n", " end\n", " \n", " printf('\ta%i',i);\n", " if(i==1)\n", " printf(' =')\n", " end\n", " \n", " printf('\t%f\n',B(i,1))\n", "end\n", "printf('\n\na1=%f\na2=%f\n\na=%f\nb=%f\n\n',X(1,1),X(2,1),a,b)\n", "printf('The fitted curve is:\n %fx\ny=%f e',b,a)" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 9.3: Gram_Schmidt_Orthogonalization_or_Orthonormalization_Process.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 9.3\n", "//Gram-Schmidt Orthogonalization/Orthonormalization Process\n", "//Page no. 294\n", "clc;clear;close;\n", "deff('y=f(x,a)','y=sqrt(x(1,a)^2+x(2,a)^2+x(3,a)^2+x(4,a)^2)');\n", "deff('y=f1(g,a,h,b)','y=g(1,a)*h(1,b)+g(2,a)*h(2,b)+g(3,a)*h(3,b)+g(4,a)*h(4,b)');\n", "\n", "U=[1/sqrt(3),-2/sqrt(7),1,0,0,0;0,1/sqrt(7),0,1,0,0;1/sqrt(3),1/sqrt(7),0,0,1,0;-1/sqrt(3),-1/sqrt(7),0,0,0,1];\n", "for i=1:4\n", " V(i,1)=U(i,1);\n", "end\n", "for i=1:4\n", " if(f(V,1)~=0)\n", " W(i,1)=V(i,1)/f(V,1);\n", " else\n", " W(i,1)=0;\n", " end \n", "end\n", "for j=2:6\n", " for i=1:4\n", " for l=1:4\n", " k(l,1)=0;\n", " end\n", " for l=1:j-1\n", " for m=1:4\n", " w(m,1)=W(m,l);\n", " end\n", " k=k-(f1(U,j,W,l))*w;\n", " end\n", " V(i,j)=U(i,j)+k(i,1);\n", " end\n", " for i=1:4\n", " if(j~=4)\n", " if(f(V,j)~=0)\n", " W(i,j)=V(i,j)/f(V,j);\n", " else\n", " W(i,j)=0;\n", " end \n", " else\n", " W(i,j)=0;\n", " end\n", " end\n", " \n", "end\n", "disp(U,'U=')\n", "disp('W=')\n", "printf('\n')\n", "for i=1:4\n", " for j=1:6\n", " printf('%.4f\t\t',W(i,j))\n", " end\n", " printf('\n')\n", "end\n", "disp('V=')\n", "printf('\n')\n", "for i=1:4\n", " for j=1:6\n", " printf('%.4f\t\t',V(i,j))\n", " end\n", " printf('\n')\n", "end" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 9.4: QR_Decompositio.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 9.4\n", "//QR Decomposition\n", "//Page no. 296\n", "clc;clear;close;\n", "\n", "A=[2,1,1;1,3,1;1,1,4];\n", "B=A*A';\n", "disp(B,'AT*A=')\n", "//cholesky factorization to find R\n", "R(2,1)=0;R(3,1)=0;R(3,2)=0; \n", "for i=1:3\n", " for j=1:3\n", " if(i==j)\n", " k=0;\n", " for m=1:i-1\n", " k=k+R(m,i)^2; \n", " end\n", " R(i,j)=sqrt(B(i,j)-k)\n", " end\n", " if(j>i)\n", " k=0;\n", " for m=1:i-1\n", " k=k+R(m,j)*R(m,i);\n", " end\n", " R(i,j)=(B(i,j)-k)/R(i,i)\n", " end\n", " end\n", "end\n", "//cholesky factorization end\n", "disp(R,'Upper Triangular Matrix (R)=')\n", "R_1=inv(R);\n", "disp(R_1,'Inverse of R')\n", "Q=A*R_1;\n", "disp(Q,'Orthogonal Matrix Q=')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 9.5: Vector_Computatio.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 9.5\n", "//Vector Computation\n", "//Page no. 299\n", "clc;clear;close;\n", "\n", "X=[2,3,0,1];\n", "n=X(1);\n", "for i=2:4\n", " if(n