Last week, you learned how to derive the Weighted Least Squares and the Weighted Total Least Squares solutions for finding an estimate of battery cell total capacity. You learned that the Weighted Total Least Squares solution provides excellent results, but it's not practical to implement in an embedded system. What we're going to do now is search for a special case that leads to a simpler implementation, and then after that, we will take what we learn from this special case and attempt to generalize it. In this lesson, we will look at an exact solution when we make a particular assumption. We will assume that the uncertainties on the x inputs and the y inputs to the least-squares methods are proportional to each other for every data sample. Then you will learn that the solution to this problem is simple enough that we can implement it very easily on an embedded system. With insights that we gained from this solution we can generalize it to approximate the Weighted Total Least Squares solution from last week into a method that has some nice implementation properties. So, let's begin. We're going to assume that the uncertainty on the x variable is proportional to the uncertainty on the y variable for every data sample. Specifically, we assume that the standard deviation of the errors on the x inputs are exactly proportional to the standard deviations of the errors on the y inputs, using a proportionality constant, k, that is always the same. When we make this assumption, the weighting of the Weighted Total Least Squares solution is no longer completely arbitrary. So, I will refer to this simplified set of assumptions as the TLS cost function or the Total Least Squares cost function instead of the Weighted Total Least Squares cost function. The great news is we've already done a lot of the hard work to come up with the solution that we're going to need. We've come up with a general solution to the Weighted Total Least Squares cost function, and to convert the equations that we've already developed into a solution for this simplified method, all we have to do is substitute this equation of proportionality between the standard deviations on the x inputs and the y inputs into all of the mathematical results that we've already obtained. Here you can see that I've made a substitution into the denominator on the first fraction on the right hand side of the equality sign, and I've made the same substitution in the condensed result that we had from last week to the right side of this equation. In order to optimize this simplify cost function, we must take its derivative and set that equal to zero. But we've already found the derivative of the more general version of the cost function, and to evaluate the derivative for the simplified cost function, we simply set in this proportionality of the standard deviations, and we get this particular result here. Remember that this derivative was called the Jacobian. So, we found the Jacobian of the general case last week and here is the Jacobian of the more specific case. What we need to do is to take this Jacobian and set its value equal to 0 and then solve for Q hat, and when we do that we will discover that finding an exact solution in closed form is possible without needing the kind of iteration that we talked about last week, the Newton-Raphson iteration for example, in order to optimize for Q hat. I've started this slide simply by rewriting the cost function, sorry the Jacobian from the previous slide and noting that we need to set its value to 0. If we look at the denominator we see that the first term is one that involves Q hat and k, and is not a function of the summation index variable i. So, it is something that can be pulled outside of the summation, and in fact we can multiply both sides of the expression by that denominator term and it disappears. In other words, multiply both sides by Q hat squared k squared plus one all squared, and since the right hand side equals 0, that annihilates that term. Similarly, we divide both sides of the equation by two. Then, we expand the terms in the numerator, we write out that expansion of that product and we collapse the remaining terms into a polynomial and the unknown variable Q hat. We see that the resulting equation is quadratic and Q hat with coefficients that are related primarily to the recursive values that we talked about last week. The one exception is that we need to define a new summation constant that I have named c3, which is equal to the summation of the y subscript i variable squared divided by the variances of those measurements. So, I have a quadratic cost function with coefficients that we can compute based on our measured inputs, and that is great news because we know how to solve a quadratic equation. Here is the familiar solution where I've written it in terms of the a, b and c that I identified in the middle of the slide. We simply implement this quadratic solution to find the roots of the quadratic to solve for Q hat. Here I've written the solution for Q hat in terms of the recursive quantities themselves. Remember though that a quadratic equation always has exactly two solutions, we need to know inside of our embedded system and our battery management system which one of these two roots should we compute and which one should we keep as our estimate of Q hat. A general quadratic equation could have solutions where both are positive real values or both are negative real values or maybe one is positive and one is a negative real value or even both solutions could be complex conjugates of each other. In our application we're solving for a battery cell total capacity which we recognize must be positive and real. But is it possible for this equation to come up with complex numbers? That would certainly not be good in an embedded system that needs to rely on a really robust computation. So, we need to check that. Over it, if we always get exactly two real roots, can we guarantee that which one we want? Is one negative and one positive always or are both positive? How do we figure out which route to choose? I'm going to use a technique that is taught in control systems classes to determine whether roots of a polynomial are in what we call the right half plane, which means they have positive real part, or if they're in the negative half plane, which means they have negative real part, and we're going to apply that to this problem. The technique is called the Routh test, and it involves building up a matrix that we call the Routh array. If you have never seen this technique before that's perfectly fine, we are going to come up with a conclusion that you can still use with confidence, but if you have seen this method before I present to you some of the steps so that you can understand and have confidence in this particular solution. First, I use the standard method of populating this matrix that we call the Routh array using the coefficients of the polynomial from the previous slide. Then once we have populated this Routh array we use the Routh test, and the test looks at the first column, the leftmost column of the array, the one that starts with k squared c2,n, and we scan this column from top to bottom or even from bottom to top it doesn't matter, and we count the number of times that the sign of the element changes from positive to negative or from negative to positive, and the number of sign changes tells you how many roots are in the right half of the complex plane versus the left half of the complex plane. When we do this using the definitions for the recursive parameters where some of them are always squared so they're always positive for example, we can find that there is always exactly one sign change in the left column of this Routh array, and so there will be exactly one root in the right half of the complex plane. How does that help us? The Routh test has told us there's going to be exactly one root of our polynomial in the right half plane, and therefore the other root must either be in the left half plane or on the imaginary axis. The fundamental theorem of algebra says that, if I'm trying to find the roots of a polynomial where the coefficients of the polynomial are real values as they are in our case, the roots must either be all real or they must be all complex conjugates of each other or they must be a combination of those two. I can't have a single complex number that comes up without its conjugate value. We've determined that these roots must be in different halves of the complex plane and so, it's impossible for them to be complex conjugates of each other. So, both of the solutions to this equation must be real, and since one is in the right half plane and the other is in the left half plane, one will be positive and the other will be negative or 0. Since total capacity is always a positive real number, this makes our job easy. All we need to do is choose the larger root of the quadratic equation, which will be the larger real number, and that will be our solution. So, bottom line. If you followed this description of the Routh test, if that brings up some information of knowledge from your past, that's really great. If you haven't, that's still fine. I hope that you can accept from this explanation that this Routh method has determined the sign of the radical in the numerator of the quadratic equation, then it must always be positive it can never be the negative solution. So, to implement the simplified Total Least Squares method we must continuously update the c1, c2 and c3 variables, then we must compute the estimate of the total capacity using this quadratic equation here every time new information becomes available to us. So, in summary you already knew that the Weighted Least Squares solution that we looked at first in this course is biased, and in general the Weighted Total Least Squares solution is not biased, but is computationally inefficient and should not be implemented on a battery management system. In this lesson we assumed that the uncertainties of the measurement errors are proportional for all measurements, and that has allowed us to simplify the Weighted Total Least Squares general solution into something that I call the Total Least Squares solution or the TLS solution, and the good news is that this new cost function can be solved in terms of recursively computed quantities. As we proceed through the remainder of this course, please note that the notation that I use I call this specific condition the TLS problem, and that is distinct from the earlier more general problem last week that I call the WTLS or the Weighted Total Least Squares problem. That brings us to the end of this derivation of the TLS solution, and the next lesson I want to summarize this solution in some more specific detail, and that should make it more clear in how we can go about implementing it on a battery management system