% ECE 1250 Lect 7 %------------------------------------------------------------------------------- % Solving Simultaneous Equations %------------------------------------------------------------------------------- % We can solve simultaneous equations using Matlab¨, as in the following % example. % Suppose our simultaneous eqns are as follows: % -3x + y + z = 0 % 2x - y + 2z = 5 % 2x + 2y + 6z = 13 % We put the coefficients for the left sides of the equations in matrix A. A = [-3, 1, 1; 2, -1, 2; 2, 2, 6] A = -3 1 1 2 -1 2 2 2 6 % The rights sides of the equations go into vector y. yvec = [0; 5; 13] yvec = 0 5 13 % We solve for x, y, and z by multiplying the y vector by the inverse of A. xyzvec = inv(A)*yvec xyzvec = 0.6786 0.1429 1.8929 % We can take a 'pseudoinverse' of a non-square matrix. B = [1,2;3,5;4,10] B = 1 2 3 5 4 10 pinv(B) ans = 0.1429 0.9714 -0.5143 -0.0476 -0.3905 0.3048 % If we take the pseudoinverse of matrix A from earlier, we can compare % it to the regular inverse of A, and we will find that they are the same. pinv(A) ans = -0.3571 -0.1429 0.1071 -0.2857 -0.7143 0.2857 0.2143 0.2857 0.0357 inv(A) ans = -0.3571 -0.1429 0.1071 -0.2857 -0.7143 0.2857 0.2143 0.2857 0.0357 % Another way to find the inverse is to use the ^ operator. A^-1 ans = -0.3571 -0.1429 0.1071 -0.2857 -0.7143 0.2857 0.2143 0.2857 0.0357 % Note that the inverse matrix is Not the same as taking the inverse of each % element in the matrix. 1./A ans = -0.3333 1.0000 1.0000 0.5000 -1.0000 0.5000 0.5000 0.5000 0.1667 % We can tell if a set of simultanious equations is inconsistent and perhaps % unsolvable by taking the determinant of matrix A. det(A) ans = 28 % If det(A) = 0, then A is singular, and we may not be % able to solve problem. %------------------------------------------------------------------------------- % Fitting Lines or other Functions to Data %------------------------------------------------------------------------------- % Least squares for curve fits of data pts. % Given (x,y) data, find the best straight % line fit. >> x = 1:5 x = 1 2 3 4 5 >> y = [-1, 3, 7, 10, 11] y = -1 3 7 10 11 >> plot(x,y,'bx') >> a = polyfit(x,y,1) a = 3.1000 -3.3000 % n = 1 in polyfit gives line fit. % a(1) = slope, a(2) = y intercept. >> help polyfit polyfit Fit polynomial to data. P = polyfit(X,Y,N) finds the coefficients of a polynomial P(X) of degree N that fits the data Y best in a least-squares sense. P is a row vector of length N+1 containing the polynomial coefficients in descending powers, P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1). [P,S] = polyfit(X,Y,N) returns the polynomial coefficients P and a structure S for use with POLYVAL to obtain error estimates for predictions. S contains fields for the triangular factor (R) from a QR decomposition of the Vandermonde matrix of X, the degrees of freedom (df), and the norm of the residuals (normr). If the data Y are random, an estimate of the covariance matrix of P is (Rinv*Rinv')*normr^2/df, where Rinv is the inverse of R. [P,S,MU] = polyfit(X,Y,N) finds the coefficients of a polynomial in XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X). This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm. Warning messages result if N is >= length(X), if X has repeated, or nearly repeated, points, or if X might need centering and scaling. Class support for inputs X,Y: float: double, single See also poly, polyval, roots, lscov. Reference page in Help browser doc polyfit % Recall the parameters of our line fit. >> a a = 3.1000 -3.3000 >> hold on % superimpose plots >> x x = 1 2 3 4 5 >> yfit = a(1)*x + a(2)*1 yfit = -0.2000 2.9000 6.0000 9.1000 12.2000 >> plot(x,yfit,'r-') >> b = polyfit(x,y,2) b = -0.5000 6.1000 -6.8000 >> yfit2 = a(2)*x.^2 + a(1)*x + a(2) yfit2 = -3.5000 -10.3000 -23.7000 -43.7000 -70.3000 >> plot(x,yfit2,'g-') >> yfit2 = b(1)*x.^2 + b(2)*x + b(3) yfit2 = -1.2000 3.4000 7.0000 9.6000 11.2000 >> plot(x,yfit2,'g-') >> hold off >> plot(x,y,'bx') >> hold on >> plot(x,yfit,'r-') >> plot(x,yfit2,'g-') %------------------------------------------------------------------------------- % Comparison Operators %------------------------------------------------------------------------------- % We define two matrices for comparisons. A = [1,2;3,5] A = 1 2 3 5 B = [1, 0; -1, 5] B = 1 0 -1 5 % Matlab¨ uses 0's to mean False and 1's to mean True in comparisons. % Internally, Matlab¨ thesef these 0's and 1's as "logical" variables. Thus, % these 0's and 1's are not the same as integer 0's and 1's. Logical 0's and % 1's, for example, may be used to index arrays to pick out values satisfying % specified logical comparisons. It is possible to convert integers to % logicals by applying the logical() function to them. Likewise, if we add % a number to logical values, Matlab¨ converts the logicals to numbers, and % the result is numerical. A==B % Testing equality. Note that we use two equal signs for a comparison. ans = 1 0 0 1 % The comparison compares the matrics element-by-element. A>=B ans = 1 1 1 1 A=>B A=>B | Error: Unexpected MATLAB operator. % The correct syntax for "greater than or equal to" is to put the > sign first. % What is the inequality operator? Is it the same as in the C language? A!=B A!=B | Error: Unexpected MATLAB operator. % The answer is "no". The correct operator for "not" is the tilde: ~ A~=B ans = 0 1 1 0 A>B ans = 0 1 1 0 %------------------------------------------------------------------------------- % The "any" and "all" functions. %------------------------------------------------------------------------------- % After performing a comparison, the comparison results may be used to extract % values that satisfy the comparison from an array. (See the Advanced Indexing % handout for more information.) If, however, we are merely testing to see if % a condition holds, we probably wish to know if any or all of the comparison % values were True = 1. The "any" and "all" operators do just this. Like % other functions, they operate on columns of arrays unless there is only one % row, in which case they operate on the row. any(A>B) ans = 1 1 any(any(A>B)) ans = 1 all(A>B) ans = 0 0 all(all(A>B)) ans = 0 diary off