Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2006AE6382 Design Computing1 Advanced Matrix Applications in Matlab Now we’ll show a few relatively simple examples to illustrate some of the powerful.

Similar presentations


Presentation on theme: "Fall 2006AE6382 Design Computing1 Advanced Matrix Applications in Matlab Now we’ll show a few relatively simple examples to illustrate some of the powerful."— Presentation transcript:

1 Fall 2006AE6382 Design Computing1 Advanced Matrix Applications in Matlab Now we’ll show a few relatively simple examples to illustrate some of the powerful matrix capabilities in Matlab… MUCH MORE can be done!

2 Fall 2006AE6382 Design Computing2 Under- and Over-determined Equations Matlab’s matrix divide (/ or \) will handle under- and over- determined sets of equations with aplomb, but you do need to know what is happening! –Normal: same number of equations as unknowns (more formally, coefficient matrix and augmented matrix must have same rank) –Under-determined: fewer equations than unknowns –Over-determined: more equations than unknowns Consider a simple example of calculating the coefficients for a k th degree polynomial to pass N points –When N=k+1, we have the classic problem –when N < k+1, not enough equations (under-determined) –when N > k+1, too many equations (over-determined) How does Matlab handle this?

3 Fall 2006AE6382 Design Computing3 Matlab Solutions The solution for the polynomial coefficients is a simple set of linear equations as shown below for a 5th degree polynomial: It is a bit tedious to compute the coefficient matrix but we can write a short m-function to do this for this example… function X=xmatrix(ndeg, xdata) % X = xmatrix(ndeg, xdata) constructs matrix with powers of x % in each row (ndeg+1 columns) and each element from xdata % is in a separate row. N=length(xdata); X=zeros(N,ndeg+1); % allocate space for X for kr=1:N for kc=1:ndeg+1 X(kr,kc)=xdata(kr)^(kc-1); end X=fliplr(X);

4 Fall 2006AE6382 Design Computing4 Example – cont’d Here is the data and a “normal” fit using a polynomial that passes exactly through the points (degree+1 = number of points) >> x1=0:10:50; >> y1=rand(1,6); >> plot(x1,y1,'o') >> hold on >> X1=xmatrix(5,x1); >> a1=X1\y1'; >> x=0:2:50; >> y=polyval(a1',x); >> plot(x,y,’g’) >> xlabel('X'), ylabel('Y') >> legend('Data','Normal')

5 Fall 2006AE6382 Design Computing5 Example - cont’d Now let’s consider a cubic polynomial for the same data so we have an over-determined set of equations >> X2=xmatrix(3,x1); >> a2=X2\y1'; >> y=polyval(a2',x); >> yy=polyval(a1',x); >> plot(x,y,‘b') Solution is correct in a least square sense

6 Fall 2006AE6382 Design Computing6 Example – cont’d Finally, let’s consider fitting a 8 th degree polynomial to the same 6 data points. This is now under-determined >> X4=xmatrix(8,x1); >> a4=X4\y1'; >> y4=polyval(a4',x); >> plot(x,y,'r') >> legend('Data','Normal','Over','Under') >> plot(x,y,'r') Solution is now computed using “pseudo inverse” which finds an 8 th degree that passes through 6 points

7 Fall 2006AE6382 Design Computing7 Matrix Multiplication Matrix multiplication involves a large number of summations… This can be used instead of a FOR loop –increased computational speed of native * operation –much more complicated code to follow!

8 Fall 2006AE6382 Design Computing8 Case 1: Row * Column This yields a simple scalar If a=ones(1,N), then this simply sums up the values in the b array…

9 Fall 2006AE6382 Design Computing9 Case 2: Column * Row This is more interesting because the result is an array Note that the column vector appears in each column and the row vector appears in each row. The example below is interesting…

10 Fall 2006AE6382 Design Computing10 Case 3: Row * Matrix This is an even more useful result… This lets us run a summation over the index, k, at N different indexes A useful example is when the 1…N index in b is actually an independent variable (e.g., time) so we can compute summations at different time points at once!

11 Fall 2006AE6382 Design Computing11 Example: Fourier Series for Square Wave The Fourier Series for a square wave is >> k=1:2:121; >> t=linspace(-0.5,0.5,50); >> coef=4/pi./k; >> sinterm=sin(2*pi*k'*t); >> ft=coef*sinterm; >> plot(t,ft) >> xlabel('time'), ylabel('f(t)') Not a FOR loop in sight!


Download ppt "Fall 2006AE6382 Design Computing1 Advanced Matrix Applications in Matlab Now we’ll show a few relatively simple examples to illustrate some of the powerful."

Similar presentations


Ads by Google