MATLAB function that fits a linear combination of exponential functions to given data
Show older comments
Hello,
I have to write a MATLAB function which looks for a linear combination of exponential functions that fits certain given data the best.
The input of the MATLAB function consists of a (n * 2)-matrix and a vector of initial guesses for the exponents. The data has to fit with the following kind of function: f(x) = C_1*e^(lambda_1 * x) + ... + C_m*e^(lambda_m * x)
So the (n * 2)-matrix has n rows and 2 columns. I suppose the left column contains the x_i and the right column contains the y_i for 1 <= i <= n. The magnitude of m depends on the length of the vector with the initial guesses (lambda_1, ..., lambda_m).
The purpose of this MATLAB function is that the function looks for a function f that is the best choice according to the least squares method. In short, a function such that S = \sum_{i=1}^n (y_i - f(x_i))^2 is minimalized.
The output of the MATLAB function exists of twee vectors with the calculated (best) choices for (lambda_1, ..., lamda_m) and (C_1, ..., C_m) en the residue (thus r_i = y_i - f(x_i)). Furthermore, the function has to make a plot of the data and the fit f.
---
I don't have a problem to realize the output, but I do have trouble with determining f. I can use the function fminsearch, so I can use that for the least squares method I guess.
I did get a hint: For fixed lambda = (lambda_1, ..., lambda_m) I should use the least squares method to find the best coefficients C_j = C_j(lambda).
So I think I have to use the initial guesses of lambda as fixed (at first) and 'only' determine the C's. But I don't have a clue how to do this.
Anyone who can help me? Thanks in advance.
Answers (1)
Image Analyst
on 15 Mar 2013
0 votes
For a fixed lambda, it's really trivial. Just use the standard least squares formula. Did the course cover that? inverse([A'A]) and so on? Just plug in some range of x and get your e^(lambda*x) array and construct your A matrix.
5 Comments
Tom
on 17 Mar 2013
Image Analyst
on 17 Mar 2013
No, I was not thinking of that. This is not a non-linear situation. Look at the equation and the unknowns. If the lambdas are no longer unknown and you have some known, specified values for them, then it turns into a linear situation. The only unknowns in that case are the C's. It doesn't matter that x is plugged into some non-linear equation. The x are not unknowns - they are known. Maybe that's what's tripping you up. The x are your observations - you know those. What matters is if the model equation is linear in the unknowns (which are the C's), which it is. Thus you can use polyfit(). You basically have y = C1*z1 + C2*z2 + .... + Cm*zm. That is an equation which is linear in the C's. You can make up the z vectors from plugging the x's into the exponential formula. Then the z's are columns in the A equation. Go back to the formula for solving a least squares set of equations http://en.wikipedia.org/wiki/Least_squares. The coefficients are solved for by this equation:
C = inverse([z' * z]) * z' * y
Your z is going to be a matrix of the x measurements
z = [e^(lambda_1 * x1), e^(lambda_2 * x1), ... e^(lambda_m * x1);...
e^(lambda_1 * x2), e^(lambda_2* x2), ... e^(lambda_m * x2);...
...and so on down until your last x
e^(lambda_1 * xn), e^(lambda_2* xn), ... e^(lambda_m * xn)]
Basically each column of z is the coefficients of the C's if you were to write out all n of your equations in a list. Your instructor should have gone over the derivation of that equation so hopefully this is not the first time you have seen this. (There is a more MATLAB-ish way of solving that like C=y/z if you want to go that route instead of the standard book formula like I showed.)
Tom
on 30 Mar 2013
Image Analyst
on 31 Mar 2013
I haven't used the fminsearch() function. If that's the way they want you do it, then I am not the best person to help you.
Tom
on 31 Mar 2013
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!