python iteration for a derivative matrix












0















This is my code. I want to iterate the function f(x) and its derivative matrix simultaneously both in matrix form. How can i do that?



import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x



def dm1(x):
return sp.diff(m1(x),x)
def dm2(x):
return sp.diff(m2(x),x)
def dm3(x):
return sp.diff(m3(x),x)
def dm4(x):
return sp.diff(m4(x),x)

def f(x):
return([[m1(x),m2(x)],[m3(x),m4(x)]]) # function matrix

def k(x):
return ([[dm1(x),dm2(x)],[dm3(x),dm4(x)]]) # derivative matrix









share|improve this question

























  • what is it exactly you need? do you mean by iterate the Newton iterative solver ? If so what have you done so far to attempt this ?

    – Hadi Farah
    Nov 26 '18 at 9:55











  • yes. exactly. i want to use a nonlinear inversion equation to solve my problem. i can iterate the function matrix easily by a for loop but cant iterates the derivative. at the end what i need is to use the gauss newton method for solving my equation

    – Sreedev Sreedev
    Nov 26 '18 at 10:14













  • with 2 variables x1 and x2 ? Because usually the function is a vector and the derivative is a matrix. Which means for 4 functions. You usually have a 4x1 vector and a 4x4 derivative matrix for 4 distinct variables

    – Hadi Farah
    Nov 26 '18 at 10:15













  • first, i have to solve with one variable just x. then maybe in next stage I may need 2 variable

    – Sreedev Sreedev
    Nov 26 '18 at 10:19











  • why do you need a matrix of functions then ? You need to have a 1 to 1 function to variable. Or you want to solve this 4 times for 4 different results?

    – Hadi Farah
    Nov 26 '18 at 10:23
















0















This is my code. I want to iterate the function f(x) and its derivative matrix simultaneously both in matrix form. How can i do that?



import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x



def dm1(x):
return sp.diff(m1(x),x)
def dm2(x):
return sp.diff(m2(x),x)
def dm3(x):
return sp.diff(m3(x),x)
def dm4(x):
return sp.diff(m4(x),x)

def f(x):
return([[m1(x),m2(x)],[m3(x),m4(x)]]) # function matrix

def k(x):
return ([[dm1(x),dm2(x)],[dm3(x),dm4(x)]]) # derivative matrix









share|improve this question

























  • what is it exactly you need? do you mean by iterate the Newton iterative solver ? If so what have you done so far to attempt this ?

    – Hadi Farah
    Nov 26 '18 at 9:55











  • yes. exactly. i want to use a nonlinear inversion equation to solve my problem. i can iterate the function matrix easily by a for loop but cant iterates the derivative. at the end what i need is to use the gauss newton method for solving my equation

    – Sreedev Sreedev
    Nov 26 '18 at 10:14













  • with 2 variables x1 and x2 ? Because usually the function is a vector and the derivative is a matrix. Which means for 4 functions. You usually have a 4x1 vector and a 4x4 derivative matrix for 4 distinct variables

    – Hadi Farah
    Nov 26 '18 at 10:15













  • first, i have to solve with one variable just x. then maybe in next stage I may need 2 variable

    – Sreedev Sreedev
    Nov 26 '18 at 10:19











  • why do you need a matrix of functions then ? You need to have a 1 to 1 function to variable. Or you want to solve this 4 times for 4 different results?

    – Hadi Farah
    Nov 26 '18 at 10:23














0












0








0








This is my code. I want to iterate the function f(x) and its derivative matrix simultaneously both in matrix form. How can i do that?



import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x



def dm1(x):
return sp.diff(m1(x),x)
def dm2(x):
return sp.diff(m2(x),x)
def dm3(x):
return sp.diff(m3(x),x)
def dm4(x):
return sp.diff(m4(x),x)

def f(x):
return([[m1(x),m2(x)],[m3(x),m4(x)]]) # function matrix

def k(x):
return ([[dm1(x),dm2(x)],[dm3(x),dm4(x)]]) # derivative matrix









share|improve this question
















This is my code. I want to iterate the function f(x) and its derivative matrix simultaneously both in matrix form. How can i do that?



import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x



def dm1(x):
return sp.diff(m1(x),x)
def dm2(x):
return sp.diff(m2(x),x)
def dm3(x):
return sp.diff(m3(x),x)
def dm4(x):
return sp.diff(m4(x),x)

def f(x):
return([[m1(x),m2(x)],[m3(x),m4(x)]]) # function matrix

def k(x):
return ([[dm1(x),dm2(x)],[dm3(x),dm4(x)]]) # derivative matrix






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 9:52









Vishnudev

1,156518




1,156518










asked Nov 26 '18 at 9:40









Sreedev SreedevSreedev Sreedev

32




32













  • what is it exactly you need? do you mean by iterate the Newton iterative solver ? If so what have you done so far to attempt this ?

    – Hadi Farah
    Nov 26 '18 at 9:55











  • yes. exactly. i want to use a nonlinear inversion equation to solve my problem. i can iterate the function matrix easily by a for loop but cant iterates the derivative. at the end what i need is to use the gauss newton method for solving my equation

    – Sreedev Sreedev
    Nov 26 '18 at 10:14













  • with 2 variables x1 and x2 ? Because usually the function is a vector and the derivative is a matrix. Which means for 4 functions. You usually have a 4x1 vector and a 4x4 derivative matrix for 4 distinct variables

    – Hadi Farah
    Nov 26 '18 at 10:15













  • first, i have to solve with one variable just x. then maybe in next stage I may need 2 variable

    – Sreedev Sreedev
    Nov 26 '18 at 10:19











  • why do you need a matrix of functions then ? You need to have a 1 to 1 function to variable. Or you want to solve this 4 times for 4 different results?

    – Hadi Farah
    Nov 26 '18 at 10:23



















  • what is it exactly you need? do you mean by iterate the Newton iterative solver ? If so what have you done so far to attempt this ?

    – Hadi Farah
    Nov 26 '18 at 9:55











  • yes. exactly. i want to use a nonlinear inversion equation to solve my problem. i can iterate the function matrix easily by a for loop but cant iterates the derivative. at the end what i need is to use the gauss newton method for solving my equation

    – Sreedev Sreedev
    Nov 26 '18 at 10:14













  • with 2 variables x1 and x2 ? Because usually the function is a vector and the derivative is a matrix. Which means for 4 functions. You usually have a 4x1 vector and a 4x4 derivative matrix for 4 distinct variables

    – Hadi Farah
    Nov 26 '18 at 10:15













  • first, i have to solve with one variable just x. then maybe in next stage I may need 2 variable

    – Sreedev Sreedev
    Nov 26 '18 at 10:19











  • why do you need a matrix of functions then ? You need to have a 1 to 1 function to variable. Or you want to solve this 4 times for 4 different results?

    – Hadi Farah
    Nov 26 '18 at 10:23

















what is it exactly you need? do you mean by iterate the Newton iterative solver ? If so what have you done so far to attempt this ?

– Hadi Farah
Nov 26 '18 at 9:55





what is it exactly you need? do you mean by iterate the Newton iterative solver ? If so what have you done so far to attempt this ?

– Hadi Farah
Nov 26 '18 at 9:55













yes. exactly. i want to use a nonlinear inversion equation to solve my problem. i can iterate the function matrix easily by a for loop but cant iterates the derivative. at the end what i need is to use the gauss newton method for solving my equation

– Sreedev Sreedev
Nov 26 '18 at 10:14







yes. exactly. i want to use a nonlinear inversion equation to solve my problem. i can iterate the function matrix easily by a for loop but cant iterates the derivative. at the end what i need is to use the gauss newton method for solving my equation

– Sreedev Sreedev
Nov 26 '18 at 10:14















with 2 variables x1 and x2 ? Because usually the function is a vector and the derivative is a matrix. Which means for 4 functions. You usually have a 4x1 vector and a 4x4 derivative matrix for 4 distinct variables

– Hadi Farah
Nov 26 '18 at 10:15







with 2 variables x1 and x2 ? Because usually the function is a vector and the derivative is a matrix. Which means for 4 functions. You usually have a 4x1 vector and a 4x4 derivative matrix for 4 distinct variables

– Hadi Farah
Nov 26 '18 at 10:15















first, i have to solve with one variable just x. then maybe in next stage I may need 2 variable

– Sreedev Sreedev
Nov 26 '18 at 10:19





first, i have to solve with one variable just x. then maybe in next stage I may need 2 variable

– Sreedev Sreedev
Nov 26 '18 at 10:19













why do you need a matrix of functions then ? You need to have a 1 to 1 function to variable. Or you want to solve this 4 times for 4 different results?

– Hadi Farah
Nov 26 '18 at 10:23





why do you need a matrix of functions then ? You need to have a 1 to 1 function to variable. Or you want to solve this 4 times for 4 different results?

– Hadi Farah
Nov 26 '18 at 10:23












1 Answer
1






active

oldest

votes


















0














import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_bar = 5.
x_results =

for i in range(0,4):
h=1
x_temp = x_bar
print(x_temp)
while (abs(h) > tol):
h = -1.*f(x_temp)[i]/k(x_temp)[i]
print(h)
x_temp = x_temp + h
x_results.append(x_temp)
print(x_results)
print(f(x_results[0]))


EDIT: the above solves every function individually and not all together for 1 variable like it would in linear seismic inversion, the following should solve it the correct way:



import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_start = 5.
F = np.array(f(x_start))
dF = np.array(k(x_start))
dx = 1.
x_temp = x_start


while abs(dx) > tol:
make_square = np.dot(np.transpose(dF),dF)
print(make_square)
print(type(dF))
if make_square is not np.ndarray:
inverse = 1/make_square
#inverse = np.linalg.inv(make_square)
else:
inverse = np.linalg.inv(make_square)
#inverse = 1/make_square
inv_trans = np.dot(inverse,np.transpose(dF))
dx = -1.*np.dot(inv_trans, F)
print(dx)
x_temp = x_temp + dx
F = np.array(f(x_temp))
dF = np.array(k(x_temp))
print(x_temp)





share|improve this answer


























  • they all solve for x=0 because 0 is one solution to all your functions m1 to m4 change the functions at will and check the solutions in x_results to see how it changes. You will notice that the starting point x_start influences the results. For example the equations have all negative solutions so if you start with x_start = {positive float} all the answers will come up as 0 but if you start at -15. you will get all negative results instead.

    – Hadi Farah
    Nov 26 '18 at 12:30













  • Hey I had done a mistake, I reviewed the algorithms from: Linear seismic inversion. If I can do it in 20 mins from a wiki page, you can. Good Luck.

    – Hadi Farah
    Nov 26 '18 at 15:19













  • Theoretically, linear seismic inversion should work on n variable with m functions. In my case I had m=4 and n=1 (4 functions and 1 variable): This leads to the following matrix manipulation: [1x1] = ( [1x4]·[4x1] )⁻¹ · [1x4] · [4x1]. For higher numbers of variables and/or functions follow the quations in the link.

    – Hadi Farah
    Nov 26 '18 at 15:27













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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478298%2fpython-iteration-for-a-derivative-matrix%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_bar = 5.
x_results =

for i in range(0,4):
h=1
x_temp = x_bar
print(x_temp)
while (abs(h) > tol):
h = -1.*f(x_temp)[i]/k(x_temp)[i]
print(h)
x_temp = x_temp + h
x_results.append(x_temp)
print(x_results)
print(f(x_results[0]))


EDIT: the above solves every function individually and not all together for 1 variable like it would in linear seismic inversion, the following should solve it the correct way:



import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_start = 5.
F = np.array(f(x_start))
dF = np.array(k(x_start))
dx = 1.
x_temp = x_start


while abs(dx) > tol:
make_square = np.dot(np.transpose(dF),dF)
print(make_square)
print(type(dF))
if make_square is not np.ndarray:
inverse = 1/make_square
#inverse = np.linalg.inv(make_square)
else:
inverse = np.linalg.inv(make_square)
#inverse = 1/make_square
inv_trans = np.dot(inverse,np.transpose(dF))
dx = -1.*np.dot(inv_trans, F)
print(dx)
x_temp = x_temp + dx
F = np.array(f(x_temp))
dF = np.array(k(x_temp))
print(x_temp)





share|improve this answer


























  • they all solve for x=0 because 0 is one solution to all your functions m1 to m4 change the functions at will and check the solutions in x_results to see how it changes. You will notice that the starting point x_start influences the results. For example the equations have all negative solutions so if you start with x_start = {positive float} all the answers will come up as 0 but if you start at -15. you will get all negative results instead.

    – Hadi Farah
    Nov 26 '18 at 12:30













  • Hey I had done a mistake, I reviewed the algorithms from: Linear seismic inversion. If I can do it in 20 mins from a wiki page, you can. Good Luck.

    – Hadi Farah
    Nov 26 '18 at 15:19













  • Theoretically, linear seismic inversion should work on n variable with m functions. In my case I had m=4 and n=1 (4 functions and 1 variable): This leads to the following matrix manipulation: [1x1] = ( [1x4]·[4x1] )⁻¹ · [1x4] · [4x1]. For higher numbers of variables and/or functions follow the quations in the link.

    – Hadi Farah
    Nov 26 '18 at 15:27


















0














import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_bar = 5.
x_results =

for i in range(0,4):
h=1
x_temp = x_bar
print(x_temp)
while (abs(h) > tol):
h = -1.*f(x_temp)[i]/k(x_temp)[i]
print(h)
x_temp = x_temp + h
x_results.append(x_temp)
print(x_results)
print(f(x_results[0]))


EDIT: the above solves every function individually and not all together for 1 variable like it would in linear seismic inversion, the following should solve it the correct way:



import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_start = 5.
F = np.array(f(x_start))
dF = np.array(k(x_start))
dx = 1.
x_temp = x_start


while abs(dx) > tol:
make_square = np.dot(np.transpose(dF),dF)
print(make_square)
print(type(dF))
if make_square is not np.ndarray:
inverse = 1/make_square
#inverse = np.linalg.inv(make_square)
else:
inverse = np.linalg.inv(make_square)
#inverse = 1/make_square
inv_trans = np.dot(inverse,np.transpose(dF))
dx = -1.*np.dot(inv_trans, F)
print(dx)
x_temp = x_temp + dx
F = np.array(f(x_temp))
dF = np.array(k(x_temp))
print(x_temp)





share|improve this answer


























  • they all solve for x=0 because 0 is one solution to all your functions m1 to m4 change the functions at will and check the solutions in x_results to see how it changes. You will notice that the starting point x_start influences the results. For example the equations have all negative solutions so if you start with x_start = {positive float} all the answers will come up as 0 but if you start at -15. you will get all negative results instead.

    – Hadi Farah
    Nov 26 '18 at 12:30













  • Hey I had done a mistake, I reviewed the algorithms from: Linear seismic inversion. If I can do it in 20 mins from a wiki page, you can. Good Luck.

    – Hadi Farah
    Nov 26 '18 at 15:19













  • Theoretically, linear seismic inversion should work on n variable with m functions. In my case I had m=4 and n=1 (4 functions and 1 variable): This leads to the following matrix manipulation: [1x1] = ( [1x4]·[4x1] )⁻¹ · [1x4] · [4x1]. For higher numbers of variables and/or functions follow the quations in the link.

    – Hadi Farah
    Nov 26 '18 at 15:27
















0












0








0







import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_bar = 5.
x_results =

for i in range(0,4):
h=1
x_temp = x_bar
print(x_temp)
while (abs(h) > tol):
h = -1.*f(x_temp)[i]/k(x_temp)[i]
print(h)
x_temp = x_temp + h
x_results.append(x_temp)
print(x_results)
print(f(x_results[0]))


EDIT: the above solves every function individually and not all together for 1 variable like it would in linear seismic inversion, the following should solve it the correct way:



import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_start = 5.
F = np.array(f(x_start))
dF = np.array(k(x_start))
dx = 1.
x_temp = x_start


while abs(dx) > tol:
make_square = np.dot(np.transpose(dF),dF)
print(make_square)
print(type(dF))
if make_square is not np.ndarray:
inverse = 1/make_square
#inverse = np.linalg.inv(make_square)
else:
inverse = np.linalg.inv(make_square)
#inverse = 1/make_square
inv_trans = np.dot(inverse,np.transpose(dF))
dx = -1.*np.dot(inv_trans, F)
print(dx)
x_temp = x_temp + dx
F = np.array(f(x_temp))
dF = np.array(k(x_temp))
print(x_temp)





share|improve this answer















import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_bar = 5.
x_results =

for i in range(0,4):
h=1
x_temp = x_bar
print(x_temp)
while (abs(h) > tol):
h = -1.*f(x_temp)[i]/k(x_temp)[i]
print(h)
x_temp = x_temp + h
x_results.append(x_temp)
print(x_results)
print(f(x_results[0]))


EDIT: the above solves every function individually and not all together for 1 variable like it would in linear seismic inversion, the following should solve it the correct way:



import numpy as np
import sympy as sp

x=sp.Symbol('x')
k=
tol = 1e-3

def m1(x):
return 4*x**2 + 8*x
def m2(x):
return 8*x**2 + 6*x
def m3(x):
return x**2 + 10*x
def m4(x):
return 10*x**2 + x


def dm1(val):
return m1(x).diff(x).subs(x,val)
def dm2(val):
return m2(x).diff(x).subs(x,val)
def dm3(val):
return m3(x).diff(x).subs(x,val)
def dm4(val):
return m4(x).diff(x).subs(x,val)


def f(x):
return [m1(x), m2(x), m3(x), m4(x)] # function matrix

def k(val):
return [dm1(val), dm2(val), dm3(val), dm4(val)] # derivative list

x_start = 5.
F = np.array(f(x_start))
dF = np.array(k(x_start))
dx = 1.
x_temp = x_start


while abs(dx) > tol:
make_square = np.dot(np.transpose(dF),dF)
print(make_square)
print(type(dF))
if make_square is not np.ndarray:
inverse = 1/make_square
#inverse = np.linalg.inv(make_square)
else:
inverse = np.linalg.inv(make_square)
#inverse = 1/make_square
inv_trans = np.dot(inverse,np.transpose(dF))
dx = -1.*np.dot(inv_trans, F)
print(dx)
x_temp = x_temp + dx
F = np.array(f(x_temp))
dF = np.array(k(x_temp))
print(x_temp)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 15:17

























answered Nov 26 '18 at 12:28









Hadi FarahHadi Farah

6531415




6531415













  • they all solve for x=0 because 0 is one solution to all your functions m1 to m4 change the functions at will and check the solutions in x_results to see how it changes. You will notice that the starting point x_start influences the results. For example the equations have all negative solutions so if you start with x_start = {positive float} all the answers will come up as 0 but if you start at -15. you will get all negative results instead.

    – Hadi Farah
    Nov 26 '18 at 12:30













  • Hey I had done a mistake, I reviewed the algorithms from: Linear seismic inversion. If I can do it in 20 mins from a wiki page, you can. Good Luck.

    – Hadi Farah
    Nov 26 '18 at 15:19













  • Theoretically, linear seismic inversion should work on n variable with m functions. In my case I had m=4 and n=1 (4 functions and 1 variable): This leads to the following matrix manipulation: [1x1] = ( [1x4]·[4x1] )⁻¹ · [1x4] · [4x1]. For higher numbers of variables and/or functions follow the quations in the link.

    – Hadi Farah
    Nov 26 '18 at 15:27





















  • they all solve for x=0 because 0 is one solution to all your functions m1 to m4 change the functions at will and check the solutions in x_results to see how it changes. You will notice that the starting point x_start influences the results. For example the equations have all negative solutions so if you start with x_start = {positive float} all the answers will come up as 0 but if you start at -15. you will get all negative results instead.

    – Hadi Farah
    Nov 26 '18 at 12:30













  • Hey I had done a mistake, I reviewed the algorithms from: Linear seismic inversion. If I can do it in 20 mins from a wiki page, you can. Good Luck.

    – Hadi Farah
    Nov 26 '18 at 15:19













  • Theoretically, linear seismic inversion should work on n variable with m functions. In my case I had m=4 and n=1 (4 functions and 1 variable): This leads to the following matrix manipulation: [1x1] = ( [1x4]·[4x1] )⁻¹ · [1x4] · [4x1]. For higher numbers of variables and/or functions follow the quations in the link.

    – Hadi Farah
    Nov 26 '18 at 15:27



















they all solve for x=0 because 0 is one solution to all your functions m1 to m4 change the functions at will and check the solutions in x_results to see how it changes. You will notice that the starting point x_start influences the results. For example the equations have all negative solutions so if you start with x_start = {positive float} all the answers will come up as 0 but if you start at -15. you will get all negative results instead.

– Hadi Farah
Nov 26 '18 at 12:30







they all solve for x=0 because 0 is one solution to all your functions m1 to m4 change the functions at will and check the solutions in x_results to see how it changes. You will notice that the starting point x_start influences the results. For example the equations have all negative solutions so if you start with x_start = {positive float} all the answers will come up as 0 but if you start at -15. you will get all negative results instead.

– Hadi Farah
Nov 26 '18 at 12:30















Hey I had done a mistake, I reviewed the algorithms from: Linear seismic inversion. If I can do it in 20 mins from a wiki page, you can. Good Luck.

– Hadi Farah
Nov 26 '18 at 15:19







Hey I had done a mistake, I reviewed the algorithms from: Linear seismic inversion. If I can do it in 20 mins from a wiki page, you can. Good Luck.

– Hadi Farah
Nov 26 '18 at 15:19















Theoretically, linear seismic inversion should work on n variable with m functions. In my case I had m=4 and n=1 (4 functions and 1 variable): This leads to the following matrix manipulation: [1x1] = ( [1x4]·[4x1] )⁻¹ · [1x4] · [4x1]. For higher numbers of variables and/or functions follow the quations in the link.

– Hadi Farah
Nov 26 '18 at 15:27







Theoretically, linear seismic inversion should work on n variable with m functions. In my case I had m=4 and n=1 (4 functions and 1 variable): This leads to the following matrix manipulation: [1x1] = ( [1x4]·[4x1] )⁻¹ · [1x4] · [4x1]. For higher numbers of variables and/or functions follow the quations in the link.

– Hadi Farah
Nov 26 '18 at 15:27






















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478298%2fpython-iteration-for-a-derivative-matrix%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga