Python - Solve three non-linear equations with fsolve - Errors
I have to solve a non linear problem with three equations. The variables of the equation are Bx, By, Bz (which I imported from an excel file), mx, my, mz (which are float numbers inputed from the user) and x,y,z (which I want to calculate).
I first imported the excel file (180,6), with headed values, using pandas.
import pandas as pd
from scipy.optimize import fsolve
# Import Data
data = pd.read_excel('Cylinder.xlsx')
dfObj = pd.DataFrame(data, columns = ['y', 'x', 'Bx', 'By', 'Bz', 'B'])
Bx = dfObj['Bx']
By = dfObj['By']
Bz = dfObj['Bz']
mx = float(input("mx= "))
my = float(input("my= "))
mz = float(input("mz= "))
After many attempts and searching / reading I end up trying two ways according to solve the non-linear equation system. In both I used fsolve from scipy.optimize.
In the first one I defined the equations and then with an initial guess I used fsolve, like the codex below, and had an error:
'''ValueError: setting an array element with a sequence.'''
'''error: Result from function call is not a proper array of floats.'''
# fsolve functions
def magnetic_field(w):
x = w[0]
y = w[1]
z = w[2]
F = [3]
F.append(0)
F[0] = Bx - (10**-7)*(mx*3*x**2/(x**2+y**2+z**2)**2.5 +
my*3*x*y/(x**2+y**2+z**2)**2.5 + mz*3*x*z/(x**2+y**2+z**2)**2.5 -
mx/(x**2+y**2+z**2)**1.5)
F.append(1)
F[1] = By - (10**-7)*(mx*3*x*y/(x**2+y**2+z**2)**2.5 +
my*3*y**2/(x**2+y**2+z**2)**2.5 + mz*3*y*z/(x**2+y**2+z**2)**2.5 -
my/(x**2+y**2+z**2)**1.5)
F.append(2)
F[2] = Bz - (10**-7)*(mx*3*x*z/(x**2+y**2+z**2)**2.5 +
my*3*y*z/(x**2+y**2+z**2)**2.5 + mz*3*z**2/(x**2+y**2+z**2)**2.5 -
mz/(x**2+y**2+z**2)**1.5)
return F
wGuess = [1,1,1,1]
w = fsolve(magnetic_field,wGuess)
print(w)
print(magnetic_field)
The second attempt was with lambda functions, in which the error is:
'''TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument ''.Shape should be (3,) but it is (3, 180).'''
def magnetic_field(f1,f2,f3,x0,y0,z0):
func = lambda x: [f1(x[0], x[1], x[2]), f2(x[0], x[1], x[2]), f3(x[0],
x[1], x[2])]
return fsolve(func,[x0,y0,z0])
f1 = lambda x, y, z : Bx - (10**-7)*(mx*3*x**2/(x**2+y**2+z**2)**2.5 +
my*3*x*y/(x**2+y**2+z**2)**2.5 + mz*3*x*z/(x**2+y**2+z**2)**2.5 -
mx/(x**2+y**2+z**2)**1.5)
f2 = lambda x, y, z : By - (10**-7)*(mx*3*x*y/(x**2+y**2+z**2)**2.5 +
my*3*y**2/(x**2+y**2+z**2)**2.5 + mz*3*y*z/(x**2+y**2+z**2)**2.5 -
my/(x**2+y**2+z**2)**1.5)
f3 = lambda x, y, z : Bz - (10**-7)*(mx*3*x*z/(x**2+y**2+z**2)**2.5 +
my*3*y*z/(x**2+y**2+z**2)**2.5 + mz*3*z**2/(x**2+y**2+z**2)**2.5 -
mz/(x**2+y**2+z**2)**1.5)
res = magnetic_field(f1,f2,f3,1,1,1)
print(res)
I am new in python and I don’t understand the errors. Where am i mistaken?
Thanks in advance.
python-3.x
add a comment |
I have to solve a non linear problem with three equations. The variables of the equation are Bx, By, Bz (which I imported from an excel file), mx, my, mz (which are float numbers inputed from the user) and x,y,z (which I want to calculate).
I first imported the excel file (180,6), with headed values, using pandas.
import pandas as pd
from scipy.optimize import fsolve
# Import Data
data = pd.read_excel('Cylinder.xlsx')
dfObj = pd.DataFrame(data, columns = ['y', 'x', 'Bx', 'By', 'Bz', 'B'])
Bx = dfObj['Bx']
By = dfObj['By']
Bz = dfObj['Bz']
mx = float(input("mx= "))
my = float(input("my= "))
mz = float(input("mz= "))
After many attempts and searching / reading I end up trying two ways according to solve the non-linear equation system. In both I used fsolve from scipy.optimize.
In the first one I defined the equations and then with an initial guess I used fsolve, like the codex below, and had an error:
'''ValueError: setting an array element with a sequence.'''
'''error: Result from function call is not a proper array of floats.'''
# fsolve functions
def magnetic_field(w):
x = w[0]
y = w[1]
z = w[2]
F = [3]
F.append(0)
F[0] = Bx - (10**-7)*(mx*3*x**2/(x**2+y**2+z**2)**2.5 +
my*3*x*y/(x**2+y**2+z**2)**2.5 + mz*3*x*z/(x**2+y**2+z**2)**2.5 -
mx/(x**2+y**2+z**2)**1.5)
F.append(1)
F[1] = By - (10**-7)*(mx*3*x*y/(x**2+y**2+z**2)**2.5 +
my*3*y**2/(x**2+y**2+z**2)**2.5 + mz*3*y*z/(x**2+y**2+z**2)**2.5 -
my/(x**2+y**2+z**2)**1.5)
F.append(2)
F[2] = Bz - (10**-7)*(mx*3*x*z/(x**2+y**2+z**2)**2.5 +
my*3*y*z/(x**2+y**2+z**2)**2.5 + mz*3*z**2/(x**2+y**2+z**2)**2.5 -
mz/(x**2+y**2+z**2)**1.5)
return F
wGuess = [1,1,1,1]
w = fsolve(magnetic_field,wGuess)
print(w)
print(magnetic_field)
The second attempt was with lambda functions, in which the error is:
'''TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument ''.Shape should be (3,) but it is (3, 180).'''
def magnetic_field(f1,f2,f3,x0,y0,z0):
func = lambda x: [f1(x[0], x[1], x[2]), f2(x[0], x[1], x[2]), f3(x[0],
x[1], x[2])]
return fsolve(func,[x0,y0,z0])
f1 = lambda x, y, z : Bx - (10**-7)*(mx*3*x**2/(x**2+y**2+z**2)**2.5 +
my*3*x*y/(x**2+y**2+z**2)**2.5 + mz*3*x*z/(x**2+y**2+z**2)**2.5 -
mx/(x**2+y**2+z**2)**1.5)
f2 = lambda x, y, z : By - (10**-7)*(mx*3*x*y/(x**2+y**2+z**2)**2.5 +
my*3*y**2/(x**2+y**2+z**2)**2.5 + mz*3*y*z/(x**2+y**2+z**2)**2.5 -
my/(x**2+y**2+z**2)**1.5)
f3 = lambda x, y, z : Bz - (10**-7)*(mx*3*x*z/(x**2+y**2+z**2)**2.5 +
my*3*y*z/(x**2+y**2+z**2)**2.5 + mz*3*z**2/(x**2+y**2+z**2)**2.5 -
mz/(x**2+y**2+z**2)**1.5)
res = magnetic_field(f1,f2,f3,1,1,1)
print(res)
I am new in python and I don’t understand the errors. Where am i mistaken?
Thanks in advance.
python-3.x
Could you provide fake values for the missing parameters? LikeBx
,f1
, ....
– Eskapp
Nov 22 at 18:53
add a comment |
I have to solve a non linear problem with three equations. The variables of the equation are Bx, By, Bz (which I imported from an excel file), mx, my, mz (which are float numbers inputed from the user) and x,y,z (which I want to calculate).
I first imported the excel file (180,6), with headed values, using pandas.
import pandas as pd
from scipy.optimize import fsolve
# Import Data
data = pd.read_excel('Cylinder.xlsx')
dfObj = pd.DataFrame(data, columns = ['y', 'x', 'Bx', 'By', 'Bz', 'B'])
Bx = dfObj['Bx']
By = dfObj['By']
Bz = dfObj['Bz']
mx = float(input("mx= "))
my = float(input("my= "))
mz = float(input("mz= "))
After many attempts and searching / reading I end up trying two ways according to solve the non-linear equation system. In both I used fsolve from scipy.optimize.
In the first one I defined the equations and then with an initial guess I used fsolve, like the codex below, and had an error:
'''ValueError: setting an array element with a sequence.'''
'''error: Result from function call is not a proper array of floats.'''
# fsolve functions
def magnetic_field(w):
x = w[0]
y = w[1]
z = w[2]
F = [3]
F.append(0)
F[0] = Bx - (10**-7)*(mx*3*x**2/(x**2+y**2+z**2)**2.5 +
my*3*x*y/(x**2+y**2+z**2)**2.5 + mz*3*x*z/(x**2+y**2+z**2)**2.5 -
mx/(x**2+y**2+z**2)**1.5)
F.append(1)
F[1] = By - (10**-7)*(mx*3*x*y/(x**2+y**2+z**2)**2.5 +
my*3*y**2/(x**2+y**2+z**2)**2.5 + mz*3*y*z/(x**2+y**2+z**2)**2.5 -
my/(x**2+y**2+z**2)**1.5)
F.append(2)
F[2] = Bz - (10**-7)*(mx*3*x*z/(x**2+y**2+z**2)**2.5 +
my*3*y*z/(x**2+y**2+z**2)**2.5 + mz*3*z**2/(x**2+y**2+z**2)**2.5 -
mz/(x**2+y**2+z**2)**1.5)
return F
wGuess = [1,1,1,1]
w = fsolve(magnetic_field,wGuess)
print(w)
print(magnetic_field)
The second attempt was with lambda functions, in which the error is:
'''TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument ''.Shape should be (3,) but it is (3, 180).'''
def magnetic_field(f1,f2,f3,x0,y0,z0):
func = lambda x: [f1(x[0], x[1], x[2]), f2(x[0], x[1], x[2]), f3(x[0],
x[1], x[2])]
return fsolve(func,[x0,y0,z0])
f1 = lambda x, y, z : Bx - (10**-7)*(mx*3*x**2/(x**2+y**2+z**2)**2.5 +
my*3*x*y/(x**2+y**2+z**2)**2.5 + mz*3*x*z/(x**2+y**2+z**2)**2.5 -
mx/(x**2+y**2+z**2)**1.5)
f2 = lambda x, y, z : By - (10**-7)*(mx*3*x*y/(x**2+y**2+z**2)**2.5 +
my*3*y**2/(x**2+y**2+z**2)**2.5 + mz*3*y*z/(x**2+y**2+z**2)**2.5 -
my/(x**2+y**2+z**2)**1.5)
f3 = lambda x, y, z : Bz - (10**-7)*(mx*3*x*z/(x**2+y**2+z**2)**2.5 +
my*3*y*z/(x**2+y**2+z**2)**2.5 + mz*3*z**2/(x**2+y**2+z**2)**2.5 -
mz/(x**2+y**2+z**2)**1.5)
res = magnetic_field(f1,f2,f3,1,1,1)
print(res)
I am new in python and I don’t understand the errors. Where am i mistaken?
Thanks in advance.
python-3.x
I have to solve a non linear problem with three equations. The variables of the equation are Bx, By, Bz (which I imported from an excel file), mx, my, mz (which are float numbers inputed from the user) and x,y,z (which I want to calculate).
I first imported the excel file (180,6), with headed values, using pandas.
import pandas as pd
from scipy.optimize import fsolve
# Import Data
data = pd.read_excel('Cylinder.xlsx')
dfObj = pd.DataFrame(data, columns = ['y', 'x', 'Bx', 'By', 'Bz', 'B'])
Bx = dfObj['Bx']
By = dfObj['By']
Bz = dfObj['Bz']
mx = float(input("mx= "))
my = float(input("my= "))
mz = float(input("mz= "))
After many attempts and searching / reading I end up trying two ways according to solve the non-linear equation system. In both I used fsolve from scipy.optimize.
In the first one I defined the equations and then with an initial guess I used fsolve, like the codex below, and had an error:
'''ValueError: setting an array element with a sequence.'''
'''error: Result from function call is not a proper array of floats.'''
# fsolve functions
def magnetic_field(w):
x = w[0]
y = w[1]
z = w[2]
F = [3]
F.append(0)
F[0] = Bx - (10**-7)*(mx*3*x**2/(x**2+y**2+z**2)**2.5 +
my*3*x*y/(x**2+y**2+z**2)**2.5 + mz*3*x*z/(x**2+y**2+z**2)**2.5 -
mx/(x**2+y**2+z**2)**1.5)
F.append(1)
F[1] = By - (10**-7)*(mx*3*x*y/(x**2+y**2+z**2)**2.5 +
my*3*y**2/(x**2+y**2+z**2)**2.5 + mz*3*y*z/(x**2+y**2+z**2)**2.5 -
my/(x**2+y**2+z**2)**1.5)
F.append(2)
F[2] = Bz - (10**-7)*(mx*3*x*z/(x**2+y**2+z**2)**2.5 +
my*3*y*z/(x**2+y**2+z**2)**2.5 + mz*3*z**2/(x**2+y**2+z**2)**2.5 -
mz/(x**2+y**2+z**2)**1.5)
return F
wGuess = [1,1,1,1]
w = fsolve(magnetic_field,wGuess)
print(w)
print(magnetic_field)
The second attempt was with lambda functions, in which the error is:
'''TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument ''.Shape should be (3,) but it is (3, 180).'''
def magnetic_field(f1,f2,f3,x0,y0,z0):
func = lambda x: [f1(x[0], x[1], x[2]), f2(x[0], x[1], x[2]), f3(x[0],
x[1], x[2])]
return fsolve(func,[x0,y0,z0])
f1 = lambda x, y, z : Bx - (10**-7)*(mx*3*x**2/(x**2+y**2+z**2)**2.5 +
my*3*x*y/(x**2+y**2+z**2)**2.5 + mz*3*x*z/(x**2+y**2+z**2)**2.5 -
mx/(x**2+y**2+z**2)**1.5)
f2 = lambda x, y, z : By - (10**-7)*(mx*3*x*y/(x**2+y**2+z**2)**2.5 +
my*3*y**2/(x**2+y**2+z**2)**2.5 + mz*3*y*z/(x**2+y**2+z**2)**2.5 -
my/(x**2+y**2+z**2)**1.5)
f3 = lambda x, y, z : Bz - (10**-7)*(mx*3*x*z/(x**2+y**2+z**2)**2.5 +
my*3*y*z/(x**2+y**2+z**2)**2.5 + mz*3*z**2/(x**2+y**2+z**2)**2.5 -
mz/(x**2+y**2+z**2)**1.5)
res = magnetic_field(f1,f2,f3,1,1,1)
print(res)
I am new in python and I don’t understand the errors. Where am i mistaken?
Thanks in advance.
python-3.x
python-3.x
asked Nov 22 at 18:26
Leda
11
11
Could you provide fake values for the missing parameters? LikeBx
,f1
, ....
– Eskapp
Nov 22 at 18:53
add a comment |
Could you provide fake values for the missing parameters? LikeBx
,f1
, ....
– Eskapp
Nov 22 at 18:53
Could you provide fake values for the missing parameters? Like
Bx
, f1
, ....– Eskapp
Nov 22 at 18:53
Could you provide fake values for the missing parameters? Like
Bx
, f1
, ....– Eskapp
Nov 22 at 18:53
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436452%2fpython-solve-three-non-linear-equations-with-fsolve-errors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436452%2fpython-solve-three-non-linear-equations-with-fsolve-errors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you provide fake values for the missing parameters? Like
Bx
,f1
, ....– Eskapp
Nov 22 at 18:53