Ask Your Question
0

Solving Sytem of Equations with Trig

asked 2018-11-25 05:11:01 +0100

jonny_d gravatar image

updated 2018-11-26 00:17:36 +0100

Just trying to solve a system with approximate results, but I am getting an error.

Code:

Fs, Na, Nb, theta = var('Fs Na Nb theta')

eq1 = 0 == Fs + 0.16Na - 10.0sin(theta)

eq2 = 0 == Na - 10.0*cos(theta)

eq3 = 0 == 0.26Nb - 6.0sin(theta) - Fs

eq4 = 0 == Nb - 6.0*cos(theta)

solns = solve([eq1,eq2,eq3,eq4],Fs, Na, Nb, theta, solution_dict=True)

[[s[Fs].n(30), s[Na].n(30), s[Nb].n(30), s[theta].n(30)] for s in solns]

Error:

KeyError Traceback (most recent call last) <ipython-input-27-8201f1ef9ddf> in <module>() ----> 1 [[s[Fs].n(Integer(30)), s[Na].n(Integer(30)), s[Nb].n(Integer(30)), s[theta].n(Integer(30))] for s in solns]

KeyError: Fs

Any advice?? I am new to Sage.

EDIT: Fixed the code I posted so it is more readable. Still looking for help on this. Thanks!

edit retag flag offensive close merge delete

Comments

1

To display blocks of code or error messages, skip a line above and below, and do one of the following (all give the same result):

  • indent all code lines with 4 spaces
  • select all code lines and click the "code" button (the icon with '101 010')
  • select all code lines and hit ctrl-K

For instance, typing

If we define `f` by

    def f(x, y, z):
        return x * y * z

then `f(2, 3, 5)` returns `30` but `f(2*3*5)` gives:

    TypeError: f() takes exactly 3 arguments (1 given)

produces:

If we define f by

def f(x, y, z):
    return x * y * z

then f(2, 3, 5) returns 30 but f(2*3*5) gives:

TypeError: f() takes exactly 3 arguments (1 given)

Please edit your question to do that.

slelievre gravatar imageslelievre ( 2018-11-26 11:21:19 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-11-26 15:16:15 +0100

dan_fulea gravatar image

Note that solve is usually used to solve algebraic equations. (The examples of the doc string of solve also contain some trigonometric equations, but mixing them with more variables... I also try it in some specific non-algebraic cases, but do not hope to be successful...)

To get the system solved i am using two variables $s=\sin\theta$, $c=\cos\theta$ instead of the one variable $\theta$. This moves the system into the algebraich world. The code

# i am using s, c for sin(theta), cos(theta)...
Fs, Na, Nb, s, c = var('Fs Na Nb s c')

eq1 = 0 ==  Fs + 0.16*Na           - 10.0*s
eq2 = 0 ==            Na           - 10.0*c
eq3 = 0 == -Fs           + 0.26*Nb -  6.0*s
eq4 = 0 ==                      Nb -  6.0*c
eq5 = 1 == s^2 + c^2

solns = solve( [eq1, eq2, eq3, eq4, eq5]
               , Fs, Na, Nb, s, c
               , solution_dict=True)

for sol in solns:
    for key in sol:
        print "%2s = %s" % ( key, sol[key].n(30) )
    print

then delivers

 c = 0.98104950
 s = 0.19375728
Nb = 5.8862970
Na = 9.8104950
Fs = 0.36789356

 c = -0.98104950
 s = -0.19375728
Nb = -5.8862970
Na = -9.8104950
Fs = -0.36789356

(Getting $\theta$ now should be one more code line.)

edit flag offensive delete link more

Comments

Good solution. Thanks!

jonny_d gravatar imagejonny_d ( 2019-02-08 23:46:03 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-11-25 05:11:01 +0100

Seen: 245 times

Last updated: Nov 26 '18