1

So I am trying to write a script that solves for an unknown angle. The equation will be of the form of:

P*sin(theta)+Q*cos(theta)=G

Where P, Q, and G are known values and theta needs to be found. I was wondering if there was a way to do this with numpy or math modules. I am really new to python so trying to figure out what modules are out there and how to use them.

From googling around I heard about fsolve but unsure how to use it. Is there a library for solving for unknowns?

khelwood
59.8k14 gold badges91 silver badges116 bronze badges
asked Sep 10, 2024 at 20:02
5
  • 3
    if P, Q, and G are known, just solve it directly? You have one function, and one unknown: rewrite your function to an expression of theta? And if you don't know, there's always wolframalpha and friends. Commented Sep 10, 2024 at 20:08
  • Try theta=math.asin(G/math.sqrt(P**2+Q**2))-math.atan2(Q,P) Commented Sep 10, 2024 at 20:14
  • @lastchance inline code is single backticks, not triple backticks (or double backticks if the code itself has single backticks in it). Also, if you're going to give them an actual formula instead of letting them do the math, write an answer and explain why you're giving them that formula. Commented Sep 10, 2024 at 20:15
  • Thank-you, @Mike 'Pomax' Kamermans. I still manage to fluff some of my responses here. I've written out a cursory answer. Commented Sep 10, 2024 at 20:34
  • 1
    Recall the trig identity P * sin(θ) + Q * cos(θ) = √(P² + Q²) * sin(θ + atan2(Q, P)). Commented Sep 10, 2024 at 21:05

2 Answers 2

2

OK, as requested. The aim is to write the LHS as A.sin(theta+phi). Then we would be able to solve from

sin(theta+phi)=G/A

or

theta=asin(G/A)-phi ( exists provided abs(G) <= abs(A) )

So you need to find A and phi such that

P.sin(theta)+Q.cos(theta) = A.sin(theta+phi)
 = A[ sin(theta)cos(phi)+cos(theta)sin(phi) ]

Equate coefficients of cos(theta) and sin(theta):

A.sin(phi)=Q, A.cos(phi)=P

Square and add:

A2=P2+Q2

Divide:

tan(phi)=Q/P ( phi=atan2(Q,P) gives correct quadrant )

In code, with a check:

import math
def get_theta( P, Q, G ):
 ''' Returns (one value of) theta such that P.sin(theta) + Q.cos(theta) = G'''
 return math.asin( G / math.sqrt( P ** 2 + Q ** 2 ) ) - math.atan2( Q, P )
P, Q, G = 4, 3, 2
theta = get_theta( P, Q, G )
print( "theta = ", theta, " radians, or ", theta * 180 / math.pi, "degrees" )
print( "Check: LHS = ", P * math.sin( theta ) + Q * math.cos( theta ) )

Output:

theta = -0.2319842627257963 radians, or -13.291719167642189 degrees
Check: LHS = 2.0000000000000004

Important notes:

(1) It is your responsibility to check there IS a solution ( sqrt(P2+Q2)>=abs(G) )

(2) If there is one solution then there are an infinity of others; n.pi+(-1)nasin(G/A)-phi

answered Sep 10, 2024 at 20:31
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome thanks for answering I will give this a try!
0

Have you tried to use Scipy?

import numpy as np
from scipy.optimize import fsolve
#Define P, Q, G
def equation(theta):
 return P * np.sin(theta) + Q * np.cos(theta) - G
result = fsolve(equation, 0)
print(result)
answered Sep 10, 2024 at 20:18

1 Comment

This is what I was looking for, thank you. I will read up on the documentation for Scipy. Why is it that we have to specify that we only want to import fsolve from scipy? is it for optimization/speed benefits?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.