%------------------------------------------------------------- %Andrew Barnard %MA 4610: Numerical Linear Algebra %Homework #2: Row Reduction and Solving Linear Systems %01/27/2003 % %RowReduction solves a linear system A.x=b using Gauss %elimination without pivoting. The Matrix A and the vector %b must be fed into the function. The ouput arguments are as %follows: % 1. Matlab built in solver solution % 2. My Algorithm (Gauss no pivot) solution % 3. Percent difference between solutions % %[MatlabSol,MyAlgorithmSol,PercentDiff]=RowReduction(A,b) % %Also the time it takes to do these computations will be %output into the command window. The first time is the %matlab solver, the second time is my algorithm time. %------------------------------------------------------------- format long g %------------------------------------------------------------- %Checks matrix-vector size matching and %Solves the Linear System with Matlab's Built in Command %and times the activity %------------------------------------------------------------- [r,c]=size(A); [r1,c1]=size(b); if c~=r1 disp('Matrix Vector Multiplication Not of Correct Size') break else end tic; MatlabSolution=A\b; toc; MatTime=toc; %------------------------------------------------------------- %My Algorithm for solving a linear system by Gauss Elimination %without pivitong (solves system and time activity) %------------------------------------------------------------- tic; N=[A,b]; for i=1:1:r N(i,:)=N(i,:)./N(i,i); for j=i+1:1:r N(j,:)=N(j,:)-N(j,i).*N(i,:); end end for i=r:-1:2 for j=i-1:-1:1 N(j,:)=N(j,:)-N(j,i).*N(i,:); end end toc; MyTime=toc; MyAlgorithmSolution=N(:,r+1); %------------------------------------------------------------ %Calculates Percent Differences between my solution and %Matlab's built in solution technique %------------------------------------------------------------ PercentDifference=abs((MatlabSolution-MyAlgorithmSolution)./MatlabSolution).*100; % Andrew Barnard % MA 4610: Numerical Linear Algebra % Homework #3 % Timing Linear System Solvers clear clc format short g % creates random square matrices of size n and uses my % row reduction program to solve the system and time the % activity for both matlab and my solution technique p=11; n=1.8; j=1; for i=2:1:p A=randn(round(n^i)); b=randn(round(n^i),1); [Mat,My,Diff,MatT,MyT]=RowReduction(A,b); MatTime(j,1)=MatT; MyTime(j,1)=MyT; Point(j,1)=n^i; j=j+1; end % displays the sizes of matrices computed and the time info [round(Point),MyTime,MatTime] % finds and fits a power function to the raw data MySlope=polyfit(log10(Point(6:p-1,1)),log10(MyTime(6:p-1,1)),1) MatSlope=polyfit(log10(Point(7:p-1,1)),log10(MatTime(7:p-1,1)),1) N=[10:1:50000]; MyFit=10^MySlope(2)*N.^MySlope(1); MatFit=10^MatSlope(2)*N.^MatSlope(1); % plots the data for my solver, matlab solver, and fit functions figure(1) loglog(Point,MyTime,'o-',Point,MatTime,'o-',N,MyFit,':',N,MatFit,':') title('Time to comupute solutions to a size n by n linear system') xlabel('Size of system (n)') ylabel('Time to compute solution (sec)') legend('My Algorithm','Matlab Solution','My Fit','Matlab Fit') axis([1,10e4,10e-3,10e2]) % solves the interpolated power function to find the size matrices % that will take 2 minutes to solve using my solution method and % matlabs built in solver MySize=(120/(10^MySlope(2)))^(1/MySlope(1)) MatSize=(30/(10^MatSlope(2)))^(1/MatSlope(1)) % uses my solver to solve the size matrix that should take 2 minutes A=randn(round(MySize)); b=randn(round(MySize),1); [Mat,My,Diff,MatT,MyT]=RowReduction(A,b); MyTime2min=MyT % uses matlat solver to solve the size matrix that should take 2 minutes A=randn(round(MatSize)); b=randn(round(MatSize),1); tic MatSol=A\b; toc MatTime2Min=toc