Linear Equation in Solver

puneet28

New Member
Joined
Feb 24, 2016
Messages
2
Hi all.

A newbie here.

I am trying to solve a linear equation using solver tool in MS Excel. Following is the equation

Rhvtq95.png

a0 to a6 are the constants which I need to figure out and related R^2 value for them like given below

bKHRLkN.png


Can somebody guide me the steps ?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Looking at your example, you are going to have to do variable transformation. I will show you how to setup your sheet (it will be done with a few screenshots as I am not sure how wide this can go) followed by macro and results.

Sheet setup example:

Excel 2012
ABCDEFGH
1Number of X VariablesYX1X2X3X4X5X6
2662027213.5680
3Max Results75029164990
42595130224981
578034253901
695029224990
791130194970
872029234791
995028154950
1088028144851
1197031194821
1249025123811
13Start70034163.5871
1475027134920
15End78028194890
1689031254970
1787026103811
1879124144910
1985130183971
2057025132.5461
2181025153.5801
2276027183800
2388027174890
2483028214940
2597127243711
2660027123971
2784127183851
2887126183501
2995133293901
3064027154940
3180026164810
3293035244990
3391127213.5960
3496032304901
3585030284961
3694027193911
3770030234991
3880130224970
3960028184830
4065028153841
4182030244990
4265026144981
4365131152.5861
4484028122861
4570026154970
4665023204960
4778028274990
4870031223.5761
4982028194791
5089033214921
5172024184970
5290032234961
5380020124601
5499128224921
5583134183681
5689033234980
5775030234891
5892129234921
5970016164650
6095030234951
6170030213851
6260026122661
6392129244970
6491026212.5851
6575127104951
6660026172.5751
6778025234991
6894131283.5950
6967018194500
7078029263731
7190029234970
7278030244960
7379029143821
7475029174990
7575130194940
7681133153940
7785029224881
7875029173920
7988127264950
8095028264990
8185028214990
Sheet1
<p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br />


Excel 2012
JKLMNOP
1Transform YTransform X1Transform X2Transform X3Transform X4Transform X5Transform X6
2YX1X2X3X4X5X6
31/Y1/X11/X21/X31/X41/X51/X6
4SQRT(Y)SQRT(X1)SQRT(X2)SQRT(X3)SQRT(X4)SQRT(X5)SQRT(X6)
5Y^(1/3)X1^(1/3)X2^(1/3)X3^(1/3)X4^(1/3)X5^(1/3)X6^(1/3)
6LN(Y)LN(X1)LN(X2)LN(X3)LN(X4)LN(X5)LN(X6)
7Y^2X1^2X2^2X3^2X4^2X5^2X6^2
8
Sheet1
<p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br />


Excel 2012
RSTUVWXYZAAABACADAEAFAG
1FRInterceptSlope1Slope2Slope3Slope4Slope 5Slope 6Transform YTransform X1Transform X2Transform X3Transform X4Transform X5Transform X6
2
Sheet1
<p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br />

Here is the macro:
Code:
Option Explicit
Option Base 1
Function ExFx(ByVal sFx As String, ByRef x() As Variant, _
ByRef y() As Variant, _
ByVal i As Integer, ByVal NumIVars As Integer) As Double
Dim J As Integer
' replace Xnn starting with the higher indices just in case there
' are more than 9 variables.
sFx = UCase(sFx)
For J = NumIVars To 1 Step -1
sFx = Replace(sFx, "X" & J, "(" & x(i, J) & ")")
Next J
sFx = Replace(sFx, "Y", "(" & y(i, 1) & ")")
ExFx = Evaluate(sFx)
End Function
Sub BestMLR2()
Const MAX_ERRORS As Double = 1000000# ' initial max error value
Dim ErrorCounter As Double, MaxErrors As Double, sMaxErr As String
Dim NumIndpVars As Integer ' number of independent variables
Dim TN As Integer ' total number of variables = NIV+1
Dim N As Integer ' number of data points
Dim MaxTrans As Integer ' max transformations
Dim MaxResults As Integer ' max results
Dim Col1 As Integer, Col2 As Integer, Col3 As Integer
Dim Col4 As Integer, Col5 As Integer
Dim i As Integer, J As Integer, K As Integer
Dim M1 As Integer, M As Integer
Dim VarIdx As Integer, Low As Integer, Hi As Integer
Dim TransfMat() As String, sFx As String
Dim CurrentTransf() As String, CountTransf() As Integer
Dim NumTransf() As Integer ' number of transformations
Dim y() As Variant, x() As Variant
Dim Yt() As Variant, Xt() As Variant
Dim vRegResultsMat As Variant
Dim F As Double, Rsqr As Double, xval As Double
Dim fMaxCount As Double, fCount As Double, fMilestone As Double
Dim dt1 As Date, dt2 As Date
Dim answer As Integer
Dim answer2 As Integer
Dim myValue As Variant
answer = MsgBox("Are the number of X Variables correct?", vbYesNo + vbQuestion, _
    "Check Number of X Variables")
If answer = vbYes Then

dt1 = Now
ErrorCounter = 0
MaxErrors = MAX_ERRORS
NumIndpVars = [a2].Value
MaxResults = [A4].Value
TN = NumIndpVars + 1
Col1 = 2 ' first column of data
Col2 = Col1 + TN + 1 ' first column of transformations
Col3 = Col2 + TN + 1 ' first column of results
Col4 = Col3 + TN + 1 ' first column of tranformatioons
Col5 = Col4 + TN - 1 ' last column of transformations
Range(Cells(2, Col3), Cells(1 + 2 * MaxResults, Col3 + 3 * TN)).Value = ""
Range(Cells(2, Col3), Cells(1 + MaxResults, Col3 + 1 + TN)).Value = 0
MaxTrans = Range(Cells(2, Col2), Cells(1, Col2)).CurrentRegion.Rows.Count - 1
ReDim NumTransf(TN), TransfMat(TN, MaxTrans), CurrentTransf(TN)
ReDim CountTransf(TN)
fMaxCount = 1
For i = 1 To TN
J = 2
Do While Trim(Cells(J, Col2 + i - 1)) <> ""
TransfMat(i, J - 1) = Cells(J, Col2 + i - 1)
J = J + 1
Loop
NumTransf(i) = J - 2
fMaxCount = fMaxCount * NumTransf(i)
Next i
N = Range("B1").CurrentRegion.Rows.Count - 1
y = Range("B2:B" & N + 1).Value
x = Range(Cells(2, 3), Cells(N + 1, 2 + NumIndpVars)).Value
Yt = Range("B2:B" & N + 1).Value
Xt = Range(Cells(2, 3), Cells(N + 1, 2 + NumIndpVars)).Value
' set the initial transformations
For i = 1 To TN
CurrentTransf(i) = TransfMat(i, 1)
CountTransf(i) = 1
Next i
fCount = 0
fMilestone = 0.1
Do
On Error GoTo HandleErr
For i = 1 To N
DoEvents
If fCount / fMaxCount > fMilestone Then
DoEvents
Application.StatusBar = "Processed " & CStr(fMilestone * 100) & " %"
If fMilestone < 1 Then fMilestone = fMilestone + 0.05
End If
' sFx = CurrentTransf(1) '[A5].Value
Yt(i, 1) = ExFx(CurrentTransf(1), x, y, i, NumIndpVars)
For J = 1 To NumIndpVars
'sFx = CurrentTransf(J + 1) ' Range("A" & M).Value
Xt(i, J) = ExFx(CurrentTransf(J + 1), x, y, i, NumIndpVars)
Next J
Next i
' perform the regression calculations
vRegResultsMat = Application.WorksheetFunction.LinEst(Yt, Xt, True, True)
Rsqr = vRegResultsMat(3, 1)
F = vRegResultsMat(4, 1)
' check if F > F of last result
If F > Cells(MaxResults + 1, Col3) Then
xval = fCount / fMaxCount * 100
xval = CInt(100 * xval) / 100
Application.StatusBar = "Processed " & CStr(xval) & " %"
M1 = MaxResults + 1
' write new results to row M
Cells(M1, Col3) = F
Cells(M1, Col3 + 1) = Rsqr
For i = 1 To TN
Cells(M1, Col3 + i + 1) = vRegResultsMat(1, TN - i + 1)
Next i
For i = 1 To TN
Cells(M1, Col4 + i) = CurrentTransf(i)
Next i
Range(Cells(2, Col3), Cells(MaxResults + 1, Col5 + 1)).Select
Range(Cells(2,  Col3), Cells(MaxResults + 1, Col5 + 1)).Sort Key1:=Range(Cells(2,  Col3), Cells(MaxResults + 1, Col3)), Order1:=xlDescending
End If ' If F > Cells(MaxResults + 1, Col3)
GoTo Here
HandleErr:
fCount = fCount - 1
ErrorCounter = ErrorCounter + 1
If ErrorCounter > MaxErrors Then
If MsgBox("Reached maximum error limits of " & ErrorCounter & vbCrLf & _
"Want to stop the process?", vbYesNo + vbQuestion, "Confirmation requested") = vbYes Then
Exit Sub
Else
sMaxErr = InputBox("Update maximum number of errors", "Max Errors Input", MaxErrors)
If Trim(sMaxErr) = "" Then
MsgBox "User canceled calculations process", vbOKOnly + vbInformation, "End of Process"
Exit Sub
End If
MaxErrors = CDbl(sMaxErr)
ErrorCounter = 0
End If
End If
Resume Here
Here:
' ---------------------------------------------------------
' ---------------------------------------------------------
' ------------ Simulate Nested Loops ---------------------
' ---------------------------------------------------------
' ---------------------------------------------------------
For VarIdx = 1 To TN
DoEvents
If CountTransf(VarIdx) >= NumTransf(VarIdx) Then
If VarIdx < TN Then
CurrentTransf(VarIdx) = TransfMat(VarIdx, 1)
CountTransf(VarIdx) = 1
Else
Exit Do
End If
Else
CountTransf(VarIdx) = CountTransf(VarIdx) + 1
CurrentTransf(VarIdx) = TransfMat(VarIdx, CountTransf(VarIdx))
fCount = fCount + 1
Exit For
End If
Next VarIdx
Loop
On Error GoTo 0
dt2 = Now
[a13].Value = "Start"
[a14].Value = dt1
[A15].Value = "End"
[a16].Value = dt2
Application.StatusBar = "Done"
MsgBox "Start at " & CStr(dt1) & vbCrLf & _
"End at " & CStr(dt2), vbOKOnly + vbInformation, "Success!"
Else
myValue = InputBox("How many X Variables are there?", "Number of X Variables", 1)
Range("A2").Value = myValue
answer2 = MsgBox("Do you want to run BestMLR2?", vbYesNo + vbQuestion, _
    "Continue BestMLR2 Macro")
If answer2 = vbYes Then
Call BestMLR2
Else
End If
End If
Application.StatusBar = False

End Sub

After macro runs:

Excel 2012
RSTUVWXYZAAABACADAEAFAG
1FRInterceptSlope1Slope2Slope3Slope4Slope 5Slope 6Transform YTransform X1Transform X2Transform X3Transform X4Transform X5Transform X6
27.2054740.371955.9149290.408230.0209510.0465010.2188180.0062760.141817SQRT(Y)X1X2X3X4X5X6
37.2054740.371955.9149290.408230.0209510.0465010.2188180.0062760.141817SQRT(Y)SQRT(X1)X2X3X4X5X6
47.2054740.371955.9149290.408230.0209510.0465010.2188180.0062760.141817SQRT(Y)X1^(1/3)X2X3X4X5X6
57.2054740.371955.9149290.408230.0209510.0465010.2188180.0062760.141817SQRT(Y)X1^2X2X3X4X5X6
67.2041640.3719086.1849880.4085090.0003770.0464150.2152080.006620.143814SQRT(Y)X1X2^2X3X4X5X6
77.2041640.3719086.1849880.4085090.0003770.0464150.2152080.006620.143814SQRT(Y)SQRT(X1)X2^2X3X4X5X6
87.2041640.3719086.1849880.4085090.0003770.0464150.2152080.006620.143814SQRT(Y)X1^(1/3)X2^2X3X4X5X6
97.2041640.3719086.1849880.4085090.0003770.0464150.2152080.006620.143814SQRT(Y)X1^2X2^2X3X4X5X6
107.2020330.3718395.3719270.4083070.2145780.046670.2202740.0061410.141239SQRT(Y)X1SQRT(X2)X3X4X5X6
117.2020330.3718395.3719270.4083070.2145780.046670.2202740.0061410.141239SQRT(Y)SQRT(X1)SQRT(X2)X3X4X5X6
127.2020330.3718395.3719270.4083070.2145780.046670.2202740.0061410.141239SQRT(Y)X1^(1/3)SQRT(X2)X3X4X5X6
137.2020330.3718395.3719270.4083070.2145780.046670.2202740.0061410.141239SQRT(Y)X1^2SQRT(X2)X3X4X5X6
147.2014430.371823.3218220.1319020.0068790.014980.0718050.0020270.045015Y^(1/3)X1X2X3X4X5X6
157.2014430.371823.3218220.1319020.0068790.014980.0718050.0020270.045015Y^(1/3)SQRT(X1)X2X3X4X5X6
167.2014430.371823.3218220.1319020.0068790.014980.0718050.0020270.045015Y^(1/3)X1^(1/3)X2X3X4X5X6
177.2014430.371823.3218220.1319020.0068790.014980.0718050.0020270.045015Y^(1/3)X1^2X2X3X4X5X6
187.2010420.3718073.4104370.1319860.0001240.0149470.0706410.0021380.045649Y^(1/3)X1X2^2X3X4X5X6
197.2010420.3718073.4104370.1319860.0001240.0149470.0706410.0021380.045649Y^(1/3)SQRT(X1)X2^2X3X4X5X6
207.2010420.3718073.4104370.1319860.0001240.0149470.0706410.0021380.045649Y^(1/3)X1^(1/3)X2^2X3X4X5X6
217.2010420.3718073.4104370.1319860.0001240.0149470.0706410.0021380.045649Y^(1/3)X1^2X2^2X3X4X5X6
227.2001560.3717784.8321750.4083720.5520030.0467470.2206680.0061050.141134SQRT(Y)X1X2^(1/3)X3X4X5X6
237.2001560.3717784.8321750.4083720.5520030.0467470.2206680.0061050.141134SQRT(Y)SQRT(X1)X2^(1/3)X3X4X5X6
247.2001560.3717784.8321750.4083720.5520030.0467470.2206680.0061050.141134SQRT(Y)X1^(1/3)X2^(1/3)X3X4X5X6
257.2001560.3717784.8321750.4083720.5520030.0467470.2206680.0061050.141134SQRT(Y)X1^2X2^(1/3)X3X4X5X6
267.1973490.3716873.143960.1319330.0703490.0150380.0722670.0019840.044838Y^(1/3)X1SQRT(X2)X3X4X5X6
Sheet1
<p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br />

Interpret Results:

Excel 2012
AIAJ
1FormulaY
2
38.76311376.79215
Sheet1
<p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: #FFFFFF" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: #FFFFFF;border-collapse: collapse; border-color: #BBB"><thead><tr style=" background-color: #DAE7F5;color: #161120"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: #DAE7F5;color: #161120">AI3</th><td style="text-align:left">=T3+(<font color="Blue">U3*SQRT(<font color="Red">C3</font>)</font>)+(<font color="Blue">V3*D3</font>)+(<font color="Blue">W3*E3</font>)+(<font color="Blue">X3*F3</font>)+(<font color="Blue">Y3*G3</font>)+(<font color="Blue">Z3*H3</font>)</td></tr><tr><th width="10px" style=" background-color: #DAE7F5;color: #161120">AJ3</th><td style="text-align:left">=AI3^2</td></tr></tbody></table></td></tr></table><br />
 
Upvote 0
Also need to mention, in the example I gave I did not let the macro run the entire length of time. The more transformations you do the longer it takes. Hope this helps.
 
Upvote 0
One other thing, I added a couple of things to the original macro. When you run the macro it will ask if the number of x variables is correct. This was put in because if you add or remove any predictors and run the macro it will mess up. If you are using 3 x predictors change a2 to 3, etc. Also, the max results value in a4 can be changed to show how many results you want to display on the screen.
 
Upvote 0
If I give you the expected value of y for different cases, can you give me predicted values of a0 to a6 and their regression ?
 
Upvote 0
I'm not sure what else I can give you. I gave you an example of how to set up your sheet for 6 predictors and the macro. You will have to setup your sheet like I did but use your y values and your x predictors. then run the macro.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top