Solving General Linear Equations w/ Ecel
Matri Operations in Ecel Ecel has commands for: Multiplication (mmult) matri multiplication Transpose (transpose) transpose a matri Determinant (mdeterm) calc the determinate Inverse (minverse) generate the matri inverse Important to remember that these commands apply to an array of cells instead of to a single cell When entering the command, you must identify the entire array where the answer will be displayed!
Practice Problem Find AB = C (2 X 4) (4 X 3) =?
Ecel Matri Multiplication First enter the two matrices (for A and B):
Ecel Matri Multiplication Good practice to label the matrices:
Ecel Matri Multiplication Shading and borders help the matrices stand out: A B = C.what size matri must C be?
Ecel Matri Multiplication An array of cells for the product must be selected in this case, a 2 3 array: Select the cells directly type mmult(array1,array2). You couldn t paste the equation into the selected cells, they are in different dimensions.
Ecel Matri Multiplication The MMULT function has two arguments: the ranges of cells to be multiplied. Remember that the order of multiplication is important.
Try this out! Ecel Matri Multiplication Using the Enter key with an array command only returns an answer in a single cell. Instead, use Ctrl + Shift + Enter keys with array functions (This only works when you highlight the solution area, then put the cursor in the top function typing area )
Ecel Matri Multiplication Answer cells formatted:
Try this out! Ecel Transpose (This only works when you highlight the solution area, then put the cursor in the top function typing area ) Use Ctrl + Shift + Enter to input command
Ecel Determinant Since determinant is a scalar, select a single cell and use Enter to input command
Ecel Matri Inversion Remember that only square matrices can have inverses
Ecel Matri Inversion Ctrl + Shift + Enter to input command
Try this out: make a matri get its inverse matri multiply these two matri to get the identity matri A X A -1 = I, the identity matri:
Solving Linear Equations - Ecel Consider these 3 linear equations with unknowns, y and z: Rewrite in matri form.
MATLAB solution: >> A=[12 32-10; 0 2 3; 7 16 5]; >> b=[-30; 11; 42]; >> =A\b = 7.0000-2.0000 5.0000
Ecel Solution Enter coefficient and constant matrices: b =
Ecel Solution Label and highlight cells for matri of unknown variables: b =
Ecel Solution Ecel does not have the convenient left division operator, so we must enter = A -1 b. Enter formula to invert A matri and multiply the result by the b matri. This can be done in two steps or with nested commands: b =
Ecel Solution Apply formula to the selected array of cells by pressing Ctrl + Shift + Enter: b =
Eercises Solve the following sets of equations using MATLAB and Ecel and check the answer 0 4 4 5 2 0 10 4 2 3 z y z z y 0 4 2 5 0 2 2 3 0 23 4 3 4 2 1 4 3 2 4 3 1 4 3 2 1
Solution 1 3 2y 4z z 2 5y 4z 10 4 0 0 A = 3-2 4 B = 10 1 0 1 y 2 1 5-4 z -4 = 3.6-2.8-1.6
Solution 2 0 4 2 5 0 2 2 3 0 23 4 3 4 2 1 4 3 2 4 3 1 4 3 2 1 A = 3 1 4-1 1 B = 23 3 0 1 1 2 2 0-2 1 1 3 0 1 5 0-2 4-4 = 6.35-8.52-0.91-16.13
Other uses of A=b and left-division So far we have been given different equations for a set of variables and solved for those variables: Coefficients are known 2 1 + 3 2 = 12 4 1 3 2 = 6 2 4 1, 2 are unknown 3-3 It is common to have eperimental measurements where the data are known but the equation or the equation coefficients are unknown. 1 2 12 6
Fitting eperimental data Suppose someone is eperimenting with a temperature controller. They apply a series of voltages and measure the resulting temperature. Voltage Temperature 2 88.78 4 95.79 6 97.45 8 107.43 10 115.85
Fitting eperimental data You have reason to believe the relationship should be linear, i.e. T = c 1 V + c 2 You know T, V You want to find c 1, c 2 Notice this is the same equation as y = m + b, so we re solving for slope and intercept.
Fitting eperimental data First, let s have a look at the data: >> V = [2:2:10]; >> T = [88.78, 95.79, 97.45, 107.43, 115.85]; >> plot (V, T, * ); Not eactly a pretty straight line. Could be - The system really is not linear - The voltage wasn t eactly what you thought - The temperature measurement is not accurate enough - Etc.
Least Squares Fitting y What line best represents the data? Forcing the line through a data point is risky The best fit might not actually go through any points
Least Squares Algorithm y Least Squares minimizes the error between the line and the data points yi m i Find the line that minimizes the sum: i ( y i m i 2 ) Data point Line
Put our data into matri form Voltage Temperature 2 88.78 4 95.79 6 97.45 8 107.43 10 115.85 We have one equation, c 1 V + c 2 = T, and 5 measurements: c 1 * 2 + c 2 = 88.78 c 1 * 4 + c 2 = 95.79 c 1 * 6 + c 2 = 97.45 c 1 * 8 + c 2 = 107.43 c 1 * 10 + c 2 = 115.85 (m * + b = y) (m * V + b = T)
Put our data into matri form c 1 * 2 + c 2 = 88.78 c 1 * 4 + c 2 = 95.79 c 1 * 6 + c 2 = 97.45 c 1 * 8 + c 2 = 107.43 c 1 * 10 + c 2 = 115.85 2 1 4 1 6 1 8 1 10 1 c1 c2 = 88.78 95.79 97.45 107.43 115.85 Now it is in a familiar form, A=b
This is over-determined 2 1 4 1 6 1 8 1 10 1 c1 c2 = 88.78 95.79 97.45 107.43 115.85 Before we said we wanted equal number of equations and unknowns for a unique solution. Here we have 5 equations and 2 unknowns. This would be redundant if the points were all eactly on a line. Since they are not, this finds the best (least-squares) compromise.
This is over-determined 2 1 4 1 6 1 8 1 10 1 c1 c2 = 88.78 95.79 97.45 107.43 115.85 A b We can use the same left-division operator to solve for c1, c2. MATLAB recognizes the system is over-determined and switches to a least-squares fit algorithm. = A\b
Solve % least square fit clear all close all V=[2:2:10]; T=[88.78,95.79,97.45,107.43,115.85]; plot(v,t,'* ); A=[2 1;4 1;6 1;8 1;10 1]; TT=transpose(T); =A\TT; hold on line_vs=linspace(0,10,10); line_ts=line_vs*(1)+(2); plot(line_vs,line_ts); = 3.2890 81.3260 Our vector components are c1, c2
Least squares fit to V, T data
Other function types Other types of functions can be fit using the same technique. Consider this data t y 0.0 0.82 0.3 0.72 0.8 0.63 1.1 0.60 1.6 0.55 2.3 0.50
Fitting a non-linear function A linear fit looks like this: You decide a decaying eponential function might be better
Fitting a non-linear function The equation we want to try is y(t) = c 1 + c 2 e -t Fitting a non-linear function does not mean the system of equations is non-linear. We will solve for c1, c2. This equation is still linear with respect to c1, c2.
Put into matri form c 1 + c 2 e -0 = 0.82 c 1 + c 2 e -0.3 = 0.72 c 1 + c 2 e -0.8 = 0.63 1 e 0 1 e 0.3 1 e 0.8 1 e 1.1 1 e 1.6 1 e 2.3 c1 c2 = 0.82 0.72 0.63 0.60 0.55 0.50
Solve clear all close all A=[1 ep(0); 1 ep(-0.3); 1 ep(-0.8); 1 ep(-1.1);1 ep(-1.6); 1 ep(-2.3)]; b=[0.82;0.72;0.63;0.60;0.55;0.50]; 1=[0;0.3;0.8;1.1; 1.6; 2.3]; plot(1,b,'*');% plot the scattering dots hold on =A\b; line_=linspace(0,2.5,20); line_y=(1)+(2)*ep(-line_); plot(line_,line_y); = 0.4760 0.3413 1 e 0 1 e 0.3 1 e 0.8 1 e 1.1 1 e 1.6 1 e 2.3 c1 c2 = 0.82 0.72 0.63 0.60 0.55 0.50 y(t) = 0.476 + 0.3413 e -t A b
New fit
Summary Curve fitting is a commonly-needed operation and can be accomplished in Matlab as an A=b problem. MATLAB s left-division operator can be used to find an eact solution in the case of equal number of equations and unknowns. It can also be used to perform a least-squares fit in the case of more equations than unknowns. Be aware though that the underlying algorithm is not the same.