Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numerical Solution of Trigonometric Equation 2

Status
Not open for further replies.

GrimesFrank

Mechanical
Sep 11, 2006
149
0
0
CA
I have a formula;
y = A[÷]cos(x) + B*sin(x)

Is there a download (VB savant I am not) that will solve for x when varying y? A & B are constants.

Thank-you for your help.
 
Replies continue below

Recommended for you

Since we are speaking of analytical as well as numerical methods: I believe there is a way for Excel and Mathcad (others too?) to work together. Mcad can input and output values to the cells of the spreadsheet.
 
Excel Solver help says:
"The Microsoft Excel Solver tool uses the Generalized Reduced Gradient (GRG2) nonlinear optimization code developed by Leon Lasdon, University of Texas at Austin, and Allan Waren, Cleveland State University."

I'm not overly concerned about the algorithm as long as I get an answer. Then I do a sanity check on the answer myself.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
LOL, some of these ideas seem like you are using a sledge hammer to pound in a thumbtack! How about this, a Newton Raphson routine you can copy and paste into an Excel macro sheet. Set up to find the root of the given equation.

Option Explicit
Sub FindRoot()
' Subroutine to find root of equation using Newton Raphson iteration
Dim Pi As Double, A As Double, B As Double, Y As Double, Ans As Double, Check As Double
Dim FirstRow As Long, FinalRow As Long, NPts As Long, I As Long
Pi = 3.14159265358979
' Figure out how many points there are to compute
FinalRow = Range("A65536").End(xlUp).Row
FirstRow = 10
NPts = FinalRow - FirstRow + 1
' constants
For I = 1 To NPts
' Constants A and B are in first and second column, respectively
A = Cells(FirstRow + I - 1, 1).Value
B = Cells(FirstRow + I - 1, 2).Value
' Target Y value for function A/cos(x)+B*sin(x) is in column 3.
Y = Cells(FirstRow + I - 1, 3).Value
' Zero out 'x'==Ans and check value, Check
Ans = 0#
Check = 0#
Ans = NRFunction(A, B, Y)
Check = Y - gfunc(Ans, A, B, Y, A, A)
' Print out answer "x" in 4th column, Check value of function is in 5th column.
' ///////////////////////////////// OUTPUT ///////////////////////////////////////
Cells(FirstRow + I - 1, 4).Value = Ans
Cells(FirstRow + I - 1, 5).Value = Check
Next I
' ///////////////////////////////// OUTPUT ///////////////////////////////////////
End Sub
Function NRFunction(A As Variant, B As Variant, C As Variant) As Double
' send in Y as the value of function
Dim g As Double, dg As Double, C1 As Double, C2 As Double
Dim tol As Double, err As Double, xnew As Double, Pi As Double
Dim iter As Long
Pi = 3.14159265358979
tol = 0.000001
' Some constants that may or may not be needed.
C1 = 0# 'if needed
C2 = 0# 'if needed
' Starting value. Depending on function, may have to be very close to the root you are seeking.
xnew = Pi / 4#
err = 10000000000#
iter = 1
Do While err > tol
g = gfunc(xnew, A, B, C, C1, C2)
dg = dgfunc(xnew, A, B, C, C1, C2)
xnew = xnew - g / dg
err = Abs(g / dg) / Abs(xnew + g / dg) 'scales the error
iter = iter + 1
Loop
NRFunction = xnew
End Function
Function gfunc(x As Variant, A As Variant, B As Variant, C As Variant, C1 As Variant, _
C2 As Variant) As Double
gfunc = C - (A / Cos(x) + B * Sin(x))
End Function
Function dgfunc(x As Variant, A As Variant, B As Variant, C As Variant, C1 As Variant, _
C2 As Variant) As Double
dgfunc = -(A * Sin(x) / (Cos(x) * Cos(x)) + B * Cos(x))
End Function
 
The problem with approximate solutions is that you need to have a reasonable knowledge of the solution before you start. With a quartic there may be 4 real solutions so which one do you aim for in an approximate method? For a quick solution I'd just graph it as Greg said earlier. As this is an engineering forum the answer you get from that will probably be gud enuf. As a genius doing this as homework, I'd stick with my previous answer.

corus
 
A quick graph of the data at 1 degree interval shows asymptotes at 90 and 270. Absolute values will be dependent on the constants. This allows a quick quess for a value of Y given a Y.
 
Status
Not open for further replies.
Back
Top