Python Forum
System of 3 non-linear equations in 3 unknowns (how-to-solve?)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
System of 3 non-linear equations in 3 unknowns (how-to-solve?)
#1
This detailed screenshot http://imgbox.com/qFBxoHWd shows how I've solved, in the old past, the system by means of a MatLab function (fsolve), and I'ld like to know if a similar (short) way exists in Python.
Algorithms are listed below:
Output:
% ------ system of 3 non linear equations ---------- % function y=f(x) y=zeros(3,1); rad = pi/180; l1 = 150.210355; s1 = -38.913290; d1 = -11.185833; l2 = 180.308440; s2 = -14.878290; d2 = 14.545555; l3 = 223.495977; s3 = 20.492543; d3 = -8.679444; %--------------------------------------------------- l1 = l1 * rad; s1 = s1 * rad; t1 = tan(d1 * rad); l2 = l2 * rad; s2 = s2 * rad; t2 = tan(d2 * rad); l3 = l3 * rad; s3 = s3 * rad; t3 = tan(d3 * rad); % -------------------------------------------------- y(1)= sin(x(1)+s1)-tan(x(3)+l1)*(cos(x(1)+s1)* sin(x(2))- t1* cos(x(2))); y(2)= sin(x(1)+s2)-tan(x(3)+l2)*(cos(x(1)+s2)* sin(x(2))- t2* cos(x(2))); y(3)= sin(x(1)+s3)-tan(x(3)+l3)*(cos(x(1)+s3)* sin(x(2))- t3* cos(x(2))); endfunction rad = pi/180; [x,fval,info]=fsolve(@f,[10*rad;50*rad;0*rad]) % #3 starting values in radians la= x(1)/rad fi= x(2)/rad eps= x(3)/rad % ---------------- OUTPUT --------------------- x = 2.6180e-001 6.4577e-001 5.6800e-008 fval = 1.0311e-008 4.9066e-008 5.0822e-008

May I have some drifts to reach the goal? Thanks in advance

Edit. For short way I mean the use of an intrinsic function to avoid the classical procedure via an iterative method and related jacobian matrix.
Reply
#2
Your code would be almost the same, if you rewrote it in Python.

from math import pi, sin, tan, cos
from scipy.optimize import fsolve

def equations(x):
    rad = pi / 180;
    l1 = 150.210355; s1 = -38.913290; d1 = -11.185833;
    l2 = 180.308440; s2 = -14.878290; d2 =  14.545555;
    l3 = 223.495977; s3 =  20.492543; d3 =  -8.679444;
    l1 = l1 * rad; s1 = s1 * rad; t1 = tan(d1 * rad);
    l2 = l2 * rad; s2 = s2 * rad; t2 = tan(d2 * rad);
    l3 = l3 * rad; s3 = s3 * rad; t3 = tan(d3 * rad);
    return (sin(x[0]+s1)-tan(x[2]+l1)*(cos(x[0]+s1)* sin(x[1])- t1* cos(x[1])), 
            sin(x[0]+s2)-tan(x[2]+l2)*(cos(x[0]+s2)* sin(x[1])- t2* cos(x[1])), 
            sin(x[0]+s3)-tan(x[2]+l3)*(cos(x[0]+s3)* sin(x[1])- t3* cos(x[1]))) 

fsolve(equations, [10 * pi / 180, 50 * pi / 180, 0])
Output:
array([2.61799394e-01, 6.45771810e-01, 1.20245548e-08])
Note: It is not pythonic way to use ; to separate statements, so, it is desirable to rewrite each assignment in a new line.
Reply
#3
Wonderful solution, scidam! Just added the output of coords in degrees. Thank you very much, and cheers
# ---------- sist_3eq.py -----------
from math import pi, sin, tan, cos
from scipy.optimize import fsolve
 
def equations(x):
    rad = pi / 180;
    l1 = 150.210355; s1 = -38.913290; d1 = -11.185833;
    l2 = 180.308440; s2 = -14.878290; d2 =  14.545555;
    l3 = 223.495977; s3 =  20.492543; d3 =  -8.679444;
    l1 = l1 * rad; s1 = s1 * rad; t1 = tan(d1 * rad);
    l2 = l2 * rad; s2 = s2 * rad; t2 = tan(d2 * rad);
    l3 = l3 * rad; s3 = s3 * rad; t3 = tan(d3 * rad);
    return (sin(x[0]+s1)-tan(x[2]+l1)*(cos(x[0]+s1)* sin(x[1])- t1* cos(x[1])), 
            sin(x[0]+s2)-tan(x[2]+l2)*(cos(x[0]+s2)* sin(x[1])- t2* cos(x[1])), 
            sin(x[0]+s3)-tan(x[2]+l3)*(cos(x[0]+s3)* sin(x[1])- t3* cos(x[1]))) 
 
print(fsolve(equations, [10 * pi / 180, 50 * pi / 180, 0]))
a= fsolve(equations, [10 * pi / 180, 50 * pi / 180, 0])
print(a[0]*180/pi,a[1]*180/pi) # coords in degrees
# ------- OUTPUT ----------
# C:\Training>python sist_3eq.py
# [2.61799394e-01 6.45771810e-01 1.20245548e-08]
# 15.00000038215297 36.99999924200017
# -------------------------
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SOlving LInear Equations in Python(Symoy, NUmpy) - Coefficient Problem quest 3 1,729 Jan-30-2022, 10:53 PM
Last Post: quest
Heart how to solve complex equations in python HoangF 3 2,779 Dec-26-2021, 07:04 PM
Last Post: HoangF
Question Help with code to solve polynomials equations hiviera 1 1,785 Jul-31-2021, 01:56 AM
Last Post: hiviera
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,604 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Solve system of equations Sancho_Pansa 19 8,881 Oct-27-2020, 08:15 AM
Last Post: Sancho_Pansa
  How to solve equations, with groups of variables and or constraints? ThemePark 0 1,678 Oct-05-2020, 07:22 PM
Last Post: ThemePark
  Solve a system of linear equations with binary variables lopeslimagabriel 3 2,483 Sep-24-2020, 07:09 AM
Last Post: scidam
  How to solve difficult non-linear equations? shreeniket 3 2,377 Apr-23-2020, 01:36 AM
Last Post: shreeniket
Question Difference between Python's os.system and Perl's system command Agile741 13 6,797 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Getting a desired vector from lsqr in python when solving a linear system SJ001 0 2,422 Feb-21-2019, 04:19 PM
Last Post: SJ001

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020