+ Reply to Thread
Results 1 to 15 of 15

Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculation

  1. #1
    Registered User
    Join Date
    11-06-2007
    Posts
    28

    Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculation

    I have this spreadsheet:
    http://www.instructables.com/id/Meas...t-of-your-car/

    It makes use of Excel Solver tool to determine the minimum values of F41 and F42 cells to minimize sum of errors in F39.

    I need to understand how to write my own code to replace the Excel Solver tool, because i want to write a javascript page and/or a phone app quich performs same calculation, but I find VBA a lot easier to understand (then I will port source to javascript and whatelse).

    Googling around I found something about the Linest() function in excel, but I don't understand if it's the function used by the solver or not.
    Anyway, if it is, I need a "pure VBA" replacement for it, and I found this:


    Please Login or Register  to view this content.
    Can this help me in what I attempting to achieve?

    But I don't understand the result in the above linked spreadsheet: I get two values,... which are what? Are they A and B in Y=Ax+B, or something else?

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    Googling around I found something about the Linest() function in excel, but I don't understand if it's the function used by the solver or not.
    Solver and LINEST() use completely different algorithms.

    I'm not quite sure how to answer your question, mostly because there are entire textbooks written about numerical methods, and I don't feel like I could or should write a new textbook in a single internet forum post. My suggestions here are probably going to leave you with more questions than answers, but hopefully enough direction to let you research what you need to know.

    As a mostly self-taught programmer, one of the most useful books I came across was an old (70's era?) text by Peter Stark, I think it was, explaining numerical methods programming. All of his examples were in FORTRAN, but the programming language didn't matter. He explained the math behind the programming fairly well, so that the code was easy to understand, and, therefore, easy to adapt to other programming languages. If you are truly serious about learning this kind of programming, I would probably suggest that you find a similar "intro to numerical methods" text.

    To understand the algorithms behind Solver, I would probably suggest you start with the basic idea of "root-finding" or "finding the zeros" of an equation. http://en.wikipedia.org/wiki/Root-finding_algorithm
    I don't fully understand the default GRG non-linear, though my impression is that it is basically a robust extension of the basic but popular Newton-Raphson method. This tutorial (http://www.cs.utah.edu/~zachary/isp/...ot/Newton.html) seems to be a decent introduction to the NR algorithm, showing how it works, and also when it will not work. In terms of understanding how Solver solves problems (at least the default algorithm), I would suggest you start by first understanding how to program your own root finding problems.

    Linest uses a different set of numerical methods -- specifically the technique known generically as "linear least-squares regression." The best first place to start to really understand these methods is probably an intro to stats text where there should be a chapter or two discussing least squares regression. From there, you should learn the true definition of "linear" (hint: it will come from linear algebra and it means a lot more than "straight lines". For example, all polynomials are "linear" functions). Along the way, you should learn enough matrix algebra to express your linear regression problem as a single, simple (if matrix algebra is ever simple) matrix equation. The algorithms for linear least squares are then the algorithms needed to solve this matrix equation (specifically, how to take the inverse of a matrix).

    I, too, am having trouble backing out the math used in the specific spreadsheet in question. I so often find that I first need to understand the math before I can program the solution. The key to understanding this particular problem will be to understand the sequence of calculations from C23 to E37. I would suggest that you start in C23, write out the formulas used in getting to C24 in an algebraic notation. An understanding of the physics behind the problem would probably also be useful.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    11-06-2007
    Posts
    28

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    Thanks.
    I know quite well the physics behind that worksheet, as I am studying it since some years.
    The formula implemented by the worksheet is:
    Fd = 0.5 ro * A* Cx v^2 + m * g *Crr

    This is the drag force due to Air Drag and wheels rolling; this is stored in column D.
    In column E it is stored the acceleration due to this force, which is F/m, i.e. Di/B19
    In column C is stored the resulting speed which comes from previous speed minus speed variation due to deceleratio (v1 = v0-a*t)
    The error is calculated between logged and calculated speed.

    I don't think I need to study a whole book about statistics, I think I just need to know which is the formula to use to calculate A, B and C coefficients in Y=ax^2+bx+c given some values for x and y (or maybe in Y=ax+b , I still didn't figure out which is the result given by Solver).

    The above formula (v1=v0-a*t) can be expanded as follows:

    I figured out how to simulate least square calculation without both Solver and linest: for a 2nd grade polynomial, it's "just" a system of three equations with 3 unknown terms:
    http://mathworld.wolfram.com/LeastSq...olynomial.html

    For the above speadsheet, I put the calculations in cells U19 and following

    U19: 7 (number of samples)
    V19: =SOMMA(A23;A25;A27;A29;A31;A33;A35)
    W19: =SOMMA(A23^2;A25^2;A27^2;A29^2;A31^2;A33^2;A35^2)
    X19: =SOMMA(B23;B25;B27;B29;B31;B33;B35)

    U20: =SOMMA(A23;A25;A27;A29;A31;A33;A35)
    V20: =SOMMA(A23^2;A25^2;A27^2;A29^2;A31^2;A33^2;A35^2)
    W20: =SOMMA(A23^3;A25^3;A27^3;A29^3;A31^3;A33^3;A35^3)
    X20: =SOMMA(B23;B25;B27;B29;B31;B33;B35)

    U21: =SOMMA(A23^2;A25^2;A27^2;A29^2;A31^2;A33^2;A35^2)
    V21: =SOMMA(A23^3;A25^3;A27^3;A29^3;A31^3;A33^3;A35^3)
    W21: =SOMMA(A23^4;A25^4;A27^4;A29^4;A31^4;A33^4;A35^4)
    X21: =SOMMA(A23^2*B23;A25^2*B25;A27^2*B27;A29^2*B29;A31^2*B31;A33^2*B33;A35^2*B35)

    Applying Sarrus method:
    V23: =U19*V20*W21+V19*W20*U21+U20*V21*W19-(U21*V20*W19+U20*V19*W21+W20*V21*U19)
    V24: =X19*V20*W21+V19*W20*X21+X20*V21*W19-(X21*V20*W19+X20*V19*W21+W20*V21*X19)
    V25: =U19*X20*W21+X19*W20*U21+U20*X21*W19-(U21*X20*W19+U20*X19*W21+W20*X21*U19)
    V26: =U19*V20*X21+V19*X20*U21+U20*V21*X19-(U21*V20*X19+U20*V19*X21+X20*V21*U19)

    So I got these 3 values:
    V28: =V24/V23
    V29: =V25/V23
    V30: =V26/V23

    Results match quite good with coefficients found by these pages:
    http://www.xuru.org/rt/PR.asp#CopyPaste
    http://www.had2know.com/academics/qu...alculator.html

    So now I have these 3 coefficient to correlate to Cd and Crr:
    A = 19,45408951
    B= -0,288029101
    C = 0,001507385

    Cd = 0,538210351
    Crr = 0,01055849

    It looks like Crr/C = 7, which is the samples number, I wonder if it is a coincidence...

    I attach the file I'm currently working on...

  4. #4
    Registered User
    Join Date
    11-06-2007
    Posts
    28

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    The attachment:
    Attachment 299377

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    The attachment failed to attach or something, because it gives me an error

    I figured out how to simulate least square calculation without both Solver and linest: for a 2nd grade polynomial, it's "just" a system of three equations with 3 unknown terms:
    http://mathworld.wolfram.com/LeastSq...olynomial.html
    This is how LINEST() does regressions. The programming challenge is porgramming an appropriate algorithm for inverting the matrix. In the past, Excel has been rightly criticized for choosing a less stable, less accurate algorithm for the matrix inversion (it seems that the preferred algorithm is Qr decomposition). For a small matrix like this one, it probably does not matter as much what algorithm you employ for the matrix inversion.

    It looks like you have this reduced to a linear least squares regression, which means it is basically a matrix inversion problem. At this point, it seems like the problem is "simply" implementing a matrix inversion algorithm in VBA, javascript, or whatever programming language you are currently working in. I have been using Excel's built in matrix inverters (LINEST and MINVERSE) that it has been a long time since I coded my own matrix inverter, so I don't remember many of the details.

  6. #6
    Registered User
    Join Date
    11-06-2007
    Posts
    28

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    I don't think I need matrix inversion... or maybe it's what I did by calculating those coefficients...
    Anyway what is important is that now I have the formulas, which now I can implement in any language.
    But I still miss how the resulting polynomial relates to Cd and Crr :-(

    Reattaching the file...

    Drag_Coefficient.xls

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    But I still miss how the resulting polynomial relates to Cd and Crr :-(
    Seems like it should be algebra at this point. I haven't got the time to do the algebra for you, but it should look something like this:

    your regression appears to be V=A+B*t+C*t^2.

    From the physics formulas you give post 3, it appears that you have an expression like V(i)=V(i-1)*a*t where a is a function of Cd and Crr. Further manipulation/substitution should allow you to put that expression into a form like: V(i)=f(V0,Cd,Crr) + g(V0,Cd,Crr)*t + h(V0,Cd,Crr) *t^2

    Then, comparing the regression equation with this derived formula, it should be obvious that A=f(V0,Cd,Crr); B=g(V0,Cd,Crr); and C=h(V0,Cd,Crr). Solve this system of equations to get V0, Cd, and Crr from the regression coefficients.

  8. #8
    Registered User
    Join Date
    11-06-2007
    Posts
    28

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    Damn, I can't figure out it!
    I found some "plausible" results, Cd=0.381 and Crr=0.016 , but doing reverse math does no lead to original graph! :-(

    Any suggestion?

    (BTW, I think your V(i)=V(i-1)*a*t should be V(i)=V(i-1)-a*t )

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    My daughters get tired of their math teachers insisting that they "show their work." Oftentimes, though, it is difficult to really see where someone may have gone wrong unless they do show their work. Not knowing what you have done, it is hard to suggest where you have gone wrong.

    When I check my daughter's algebra homework for errors, I often do "reverse math" as you call it to check their answers. Usually, when the "reverse math" fails, it is because they made a mistake in the algebra (the rest of the time, I make a mistake in my algebra). So, my first suggestion would be to double check your algebra. Look for the usual culprits -- not carrying a negative sign through, misplacing a parenthesis, putting a multiplication sign where you should have put a minus sign (he says, sheepishly), and so on.

  10. #10
    Registered User
    Join Date
    11-06-2007
    Posts
    28

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    More than looking back at what I did, I'd need suggestions about what to do, because I don't know what I am doing!

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    I recognize your frustration, because I find that a lot of solving a problem is understanding the math behind the problem. When I understand the math, it is much easier to figure out how to program the solution.

    It seems that, up to this point, you have been working out a linear least squares solution that assumes that V can be expressed as some 2nd order polynomial in t (V=A+Bt+Ct^2) and that you can then back out Cv and Crr from A, B, and C. Are we certain that this is the correct approach?

    Going back to the formulas in post 3, here's what I came up with (I have to assume that this is not "new" to you, since you say you have studied this for some years):

    V(i)=V(i-1)-a*t (from the spreadsheet, it looks like this should be (t2-t1), which is a constant for the given data)
    V(i)-V(i-1)=-a*[t(i)-t(i-1)]
    dV/dt=-a <-- This is a differential equation. Like a lot of physics problems, I suspect that this problem, at least theoretically, starts out as a differential equation
    a=F/m and F=AV^2+B so a=1/m(AV^2+B) where I have lumped all of the "constants" (including Cv and Crr) into A and B.
    dV/dt=-1/m(Av^2+B)

    At this point, do you have enough calculus under your belt to understand this and see how to solve this diff eq? After separating variables, it looks to me like you will need an integral table you trust to get the antiderivatives then go from there. I don't expect that this equation resolves to a quadratic function like we have kind of assumed up to this point.

  12. #12
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    Another thought: dV/dt is a simple quadratic function. Would you get the correct results if you calculated dV/dt and v^2, then did a linear regression on that? Of course, any time you take a numeric derivative like that, there is a loss of accuracy, perhaps you would lose too much accuracy.

    Going back to the approach the original spreadsheet uses. It appears they are using a variation of Euler's method, assuming V0 is exact, then computing V(i) from V(i-1) as you've described above. The spreadsheet programmer probably felt that it was too difficult (or impossible) to express this as a "linear least squares" regression, so he/she opted for a non-linear least squares using Solver. In my first response, I kind of suggested that there are two possible approaches to this kind of problem. A linear least squares approach, which you have explored and I am beginning to wonder if it is the wrong approach. The other was something based on Newton's method for finding the minimum. in the objective function. At this point, I might suggest going back to the beginning and exploring a NR type approach to this problem.

  13. #13
    Registered User
    Join Date
    11-06-2007
    Posts
    28

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    Thanks to some guys on physicsforum, I did some progresses:
    qqcl.jpg

    Experimental values:
    0 19.44
    10 16.76
    20 14.31
    30 12.08
    40 10.35
    50 8.92
    60 7.55


    Equations in text form:
    1) y = 0.001505952381*x^2 - 0.2878928571* x + 19.45190476
    2) y = 19.51343152 e^(-0.01576382642 x)
    3) y = (1/B) tan(A*B*X+arctan(V*B))

    WinPlot file:
    http://jumpjack.altervista.org/CdCrr.wp2

    Third equation is the best, but using it I get values for Cd and Crr which are not convincing (Cd looks too high).
    I wonder if not recording data down to 0 m/s affects negatively the precision of results, as it looks close-to-zero data are the harder to simulate.
    It would be useful some kind of database of coastdown measurements, but I can't find anything.

  14. #14
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    I wonder if not recording data down to 0 m/s affects negatively the precision of results, as it looks close-to-zero data are the harder to simulate.
    Probably. From the graph, it shows that in extrapolating down to v=0, you are extrapolating to t~140 -- about double t at the last data point. Depending on the data and the model, this could be a sizable extrapolation. As an experimentalist, I might also point out that the data near V=0 might be the hardest to measure.

    I don't know that there are any "magical" techniques for dealing with this sort of thing. You might try putting different, more reasonable values for Cd into the model, regress a Crr based on that value, then look at how that affects your objective function and your measure of "goodness of fit" (r^2 or rmsd or whatever you are using). This kind of technique can be used to estimate an "uncertainty" in the regression parameter. You might find that lowering Cd by 20% has a minimal impact on the objective function and "goodness of fit", which would suggest that your regressed Cd cannot be more accurate than 20%. I seem to recall seeing this kind of technique formalized into a "Monte Carlo" type method that randomly assigns values to the desired parameters, measures the "goodness of fit" then repeats the process to show what range of values for the parameters give a "reasonable" fit to the data.

    At this point, I'm not sure there are any magical methods for dealing with "I have my best fit curve but the parameters are a little off" kind of problem. Sometimes, additional or more accurate data are needed. Sometimes you can resolve by estimating the error in the parameter to see if a more reasonable value for the parameter still fits the measured data adequately. I might simply say, "welcome to the sometimes very nuanced world of data regression and analysis."

  15. #15
    Registered User
    Join Date
    11-06-2007
    Posts
    28

    Re: Implementing Excel Solver in pure VBA for Least Square, curve fit, and Cx/Crr calculat

    I found some literature data to play with:

    V Vehicle 1 Vehicle 1b Vehicle 2 Vehicle 2b Vehicle 3 Vehicle 4 Vehicle 5 Vehicle 6 Vehicle 6
    120 5,88 6,28 5,64 4,75 4,88 7,06 5,91 6,08 6,47 6,68
    100 7,55 8,03 7,25 6,21 6,66 8,88 7,73 7,6 8,15 8,38
    80 9,4 10,34 9,46 8,07 8,98 10,91 10,02 9,5 10,31 10,2
    60 12,1 13,52 13,28 11,01 10,93 13,6 12,97 12,25 12,75 12,93
    40 15,19 15,08 18,03 14,6 15,92 15,07 15,48 15,76 16,38 16,24
    20 18,88 19,42 24,43 19,92 21,58 18,68 20,18 20,86 20,34 20,57

    Mass kg 1492 1566 1316 1277 1124 1962 1652 1523 1634 1724
    Frontal area: n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a

    http://api.viglink.com/api/click?for...e%203&txt=LINK

    After reading around, I changed my idea: what is difficult to model is not the part close to 0, but the part close to higher speed: this happens because at low speed the Cd coefficient has lower influece due to v^2 dependency. So low-speed part is just a stright line, which then is "bended up" as it goes left in the chart.
    I'm also beginning to think that working on acceleratoin data rather than speed data would make things easier.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Execution Speed: Implementing Polynomial Curve Fit Without Linest
    By mynci in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-14-2010, 04:22 PM
  2. Implementing modular arithmetic as constraint in Solver
    By Hababa in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-14-2009, 08:48 AM
  3. Replies: 0
    Last Post: 02-26-2007, 12:29 AM
  4. [SOLVED] Excel: Area under a curve calculation
    By Nick_C in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 08-25-2005, 11:05 AM
  5. [SOLVED] Excel Solver for Curve Fit
    By C Scull in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 04-19-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1