Python Forum
fsolve 3 equations with 3 unknowns. Can't get an answer.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fsolve 3 equations with 3 unknowns. Can't get an answer.
#1
Hello. Im new to the forums here and new to python. Forgive me if i do something wrong.

I have been working on this fairly simple code and can't seem to get it to print correct answers.

It's basically 3 equations (a,b,c) and three unknowns (x,y,z). The unknowns are obviously angles. I know the equations work because i have used them in matlab and arduino. Again, i'm really new to python... basically just started yesterday. Let me know what i messed up or if im going about this wrong.

Thanks.



from scipy.optimize import fsolve 
import math 
import numpy as np

d=0
h=6
j=6
k=4

def equations(p): 
    x=p[0]
    y=p[1]
    z=p[2]
    a=-math.cos(math.radians(x))*d*(math.cos(math.radians(y))*math.sin(math.radians(z))+math.cos(math.radians(z))*math.sin(math.radians(y)))+math.cos(math.radians(x))*(math.cos(math.radians(y))*j+k*(math.cos(math.radians(y))*math.sin(math.radians(z))+math.cos(math.radians(z))*math.sin(math.radians(y))));
    b=-math.sin(math.radians(x))*d*(math.cos(math.radians(y))*math.sin(math.radians(z))+math.cos(math.radians(z))*math.sin(math.radians(y)))+math.sin(math.radians(x))*(math.cos(math.radians(y))*j-k*(math.cos(math.radians(y))*math.sin(math.radians(z))+math.cos(math.radians(z))*math.sin(math.radians(y))));
    c=math.sin(math.radians(y))*j+k*(math.cos(math.radians(y))*math.cos(math.radians(z))-math.sin(math.radians(y))*math.sin(math.radians(z)))+d*(math.cos(math.radians(y))*math.cos(math.radians(z))-math.sin(math.radians(y))*math.sin(math.radians(z)))+h;
    return (a,b,c)



p = fsolve(equations,(10,0,6))

w=round(p[0]),round(p[1]),round(p[2])
print w

Also the answers i constantly get are (0,-51,-58). I have no clue why and it also doesnt change when I change input values.
Reply
#2
Posting again because no one has replied.

Hello. Im new to the forums here and new to python. Forgive me if i do something wrong.

I have been working on this fairly simple code and can't seem to get it to print correct answers.

It's basically 3 equations (a,b,c) and three unknowns (x,y,z). The unknowns are obviously angles. I know the equations work because i have used them in matlab and arduino. Again, i'm really new to python... basically just started yesterday. Let me know what i messed up or if im going about this wrong.

Thanks.



from scipy.optimize import fsolve 
import math
import numpy as np

d=0
h=6
j=6
k=4

def equations(p):
x=p[0]
y=p[1]
z=p[2]
a=-math.cos(math.radians(x))*d*(math.cos(math.radians(y))*math.sin(math.radians(z))+math.cos(math.radians(z))*math.sin(math.radians(y)))+math.cos(math.radians(x))*(math.cos(math.radians(y))*j+k*(math.cos(math.radians(y))*math.sin(math.radians(z))+math.cos(math.radians(z))*math.sin(math.radians(y))));
b=-math.sin(math.radians(x))*d*(math.cos(math.radians(y))*math.sin(math.radians(z))+math.cos(math.radians(z))*math.sin(math.radians(y)))+math.sin(math.radians(x))*(math.cos(math.radians(y))*j-k*(math.cos(math.radians(y))*math.sin(math.radians(z))+math.cos(math.radians(z))*math.sin(math.radians(y))));
c=math.sin(math.radians(y))*j+k*(math.cos(math.radians(y))*math.cos(math.radians(z))-math.sin(math.radians(y))*math.sin(math.radians(z)))+d*(math.cos(math.radians(y))*math.cos(math.radians(z))-math.sin(math.radians(y))*math.sin(math.radians(z)))+h;
return (a,b,c)



p = fsolve(equations,(10,0,6))

w=round(p[0]),round(p[1]),round(p[2])
print w [/python]
Also the answers i constantly get are (0,-51,-58). I have no clue why and it also doesnt change when I change input values.
Reply
#3
I've merged these threads. Please don't post duplicates. If no one is replying, "bump" the thread by replying to it, and if you still don't get an answer, it usually means you need to either do more research or simplify / elaborate on your problem more. (In this case, you say changing the input doesn't change the output, but you didn't provide samples for us to work against. The monstrous equations also made me glance over your thread before, but more on that below.)

I started digging in but I'm about to have to go so apologies for not being more helpful than this: take a look at the documentation for fsolve. I'm not already familiar with it, but it appears that you're providing start values, not really an input, so it actually makes sense to me that providing different inputs produces the same output (unless I'm misunderstanding, which I totally might be, but please elaborate on that). What you might want to do is try using fsolve on a simpler function (e.g. x=y or a simple quadratic or something), and see if you can reproduce your concern that way. It's often a valuable debugging skill to reproduce the issue more simply, resolve that one, and apply that knowledge to the original problem.
Reply
#4
(Mar-17-2018, 07:36 PM)micseydel Wrote: I've merged these threads. Please don't post duplicates. If no one is replying, "bump" the thread by replying to it, and if you still don't get an answer, it usually means you need to either do more research or simplify / elaborate on your problem more. (In this case, you say changing the input doesn't change the output, but you didn't provide samples for us to work against. The monstrous equations also made me glance over your thread before, but more on that below.)

I started digging in but I'm about to have to go so apologies for not being more helpful than this: take a look at the documentation for fsolve. I'm not already familiar with it, but it appears that you're providing start values, not really an input, so it actually makes sense to me that providing different inputs produces the same output (unless I'm misunderstanding, which I totally might be, but please elaborate on that). What you might want to do is try using fsolve on a simpler function (e.g. x=y or a simple quadratic or something), and see if you can reproduce your concern that way. It's often a valuable debugging skill to reproduce the issue more simply, resolve that one, and apply that knowledge to the original problem.

Ok sorry, i didn't know i could bump it up by replying.

I have gotten it to work with simple equations and have done some digging into fsolve. Everything except for the equations in my code above worked with simple equations. I've even tried other fsolve codes with my equations with no luck.

Basically im trying to say something like this:

x=cos(a)+sin(b)
y=2*sin(a)-sin(b)

then set x= some value and y= some value and return values for a and b.
Reply
#5
Could you provide a pair of sample input and output that you expect to work?
Reply
#6
input:
a=6
b=0
c=10
output:
x,y,z=0,0,0

input2:
a=0
b=0
c=16
output:
x=90
y=90
z=-90

let me know if you need anymore
Reply
#7
Bump...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Solve a system of non-linear equations in Python (scipy.optimize.fsolve) drudox 7 22,631 Aug-18-2018, 02:27 AM
Last Post: scidam
  class for ODE, scipy how to switch from fsolve to newton or newton_krylov drudox 0 2,325 Aug-16-2018, 05:12 PM
Last Post: drudox

Forum Jump:

User Panel Messages

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