×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Numerical Solution of Trigonometric Equation
2

Numerical Solution of Trigonometric Equation

Numerical Solution of Trigonometric Equation

(OP)
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.

RE: Numerical Solution of Trigonometric Equation

A Newton-Raphson approach could be easily developed.

I2I

RE: Numerical Solution of Trigonometric Equation

if you only need to solve a few points, the excel "solver" or "goal seek" tools will do a great job at this.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.

RE: Numerical Solution of Trigonometric Equation

(OP)
Thanks everyone..never used the add-ins before.....learn something new everyday.

RE: Numerical Solution of Trigonometric Equation

For a case like this I'd manipulate the terms to express it in terms of y and cos(x) only, then solve for cos(x), and hence x.

corus

RE: Numerical Solution of Trigonometric Equation

Solving for cos(x) yields a huge equation.  I substituted C=cos(x), with sin(x)=sqrt(1-C²), in the original equation and solved for C in MathCAD and got a result 141 pages long.

RE: Numerical Solution of Trigonometric Equation

It yields a quartic equation for cos(x) which has 4 roots. MathCad might get a better solution in that form.

corus

RE: Numerical Solution of Trigonometric Equation

The substitution C=cos(x), with sin(x)=sqrt(1-C²) does reduce the original equation to the quartic equation in C:

B^2*C^4+(y^2-B^2)*C^2-2*C*y*A+A^2 = 0

But that equation does not have any simple analytical solution.  Maple didn't quite take 141 pages, but it is an ugly result that is waaaay to cumbersome to work with (unless you want to leave it in Maple or whatever symbolic program you're using).

=====================================
Eng-tips forums: The best place on the web for engineering discussions.

RE: Numerical Solution of Trigonometric Equation

(sound of hasty scrabbling in dusty tomes)

Isn't the solution space of x effectively limited to 0 to 2*pi?


Y*cos(x)-A-B*sin(x)*cos(x)=0

Doesn't look hard does it?









Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.

RE: Numerical Solution of Trigonometric Equation

Reverse superposition?

y1 = A/Cos(x) ; x = acos(A/y1)

y2 = B*sin(x) ; x = asin(y2/B)

y = y1 + y2

Three equations, three unknowns.  Should be solvable (although I can't do it).

RE: Numerical Solution of Trigonometric Equation

Here's a disgusting Excel based method.

Y*cos(x)-A-B*sin(x)*cos(x)

set up a column of x 0, 0.01 ...6.29

set up the above equation in the second column

x is a solution when that is zero.

square the second column, find its minimum (MIN)

Look up the corresponding x value with VLOOKUP

call this x0


set up a refined range for x about x0, using steps of 0.0001

shampoo, rinse and repeat to any arbitrary limit of accuracy.

I warned you it was nasty.

Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.

RE: Numerical Solution of Trigonometric Equation

Simplifying the expression to constants multiplied by powers of cos(x) gives a matrix solution in Mathcad of about 4 pages for me.

By inspection, a solution for the case y=A is cos(x)=1.

If this is maths homework then to get top marks just say that the solution is trivial. They'll think you're a genius, believe me.

corus

RE: Numerical Solution of Trigonometric Equation

MATRIX([[arctan((y*(1/6*3^(1/2)/B*....^(1/2))^(1/2))],
[arctan((y*(1/6*3^(1/2)/B*((-2*(18....)^(1/2))^(1/2))],
[arctan((y*(-1/6*3^(1/2)/B*((-2*(18....)^(1/2))^(1/2))],
[arctan((y*(-1/6*3^(1/2)/B*((-2*(18....-16*B^2*A^4+8*A^2

RE: Numerical Solution of Trigonometric Equation

Yep, as I said, trivial.



corus

RE: Numerical Solution of Trigonometric Equation

Greg - you did verify that result by hand, right?

=====================================
Eng-tips forums: The best place on the web for engineering discussions.

RE: Numerical Solution of Trigonometric Equation

Thanks


Cheers,
Joerd

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.

RE: Numerical Solution of Trigonometric Equation

As I posted earlier, I think the proper method to use is Newton-Raphson.  The math is a very straight forward iterative scheme and is commonly used for finding numerical solutions to nonlinear equations or system of equations.  I wouldn't bother trying to find an explicit form for x.

I2I

RE: Numerical Solution of Trigonometric Equation

hastily using the chtfrmla.zip file at:

http://www.bmsltd.ie/Excel/Default.htm

MAY yeild a result between -1.56 and -1.58.

use any constant for A and B.

good luck!
-pmover

RE: Numerical Solution of Trigonometric Equation

I agree on the suggestion of numerical method as I think we have proved in gory detail the analytical solution is a PITA.

Newton-Rhapson is one numerical method.  There is also excel's solver, and Maple's fsolve, and I'm sure there are plenty more out there.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.

RE: Numerical Solution of Trigonometric Equation

I'm not 100% sure, but I believe both the excel solver and fsolve employ the Newton-Raphson method.

I2I

RE: Numerical Solution of Trigonometric Equation

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.

RE: Numerical Solution of Trigonometric Equation

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.

RE: Numerical Solution of Trigonometric Equation

I think GregLocock's transporter beam blew apart and splattered his molecular matter all over the Eng-Tips site.

A numerical approach is the obvious solution.

--------------------
How much do YOU owe?
http://www.brillig.com/debt_clock/
--------------------

RE: Numerical Solution of Trigonometric Equation

GRG2 is based on Newton-Raphson iterations.

I2I

RE: Numerical Solution of Trigonometric Equation

2
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

RE: Numerical Solution of Trigonometric Equation

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

RE: Numerical Solution of Trigonometric Equation

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.

RE: Numerical Solution of Trigonometric Equation

Yank38 has one of the correct approach; then plot you values in excel and you will have your roots right from the graph.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources