Skip to content

Instantly share code, notes, and snippets.

@elnikkis
Created July 21, 2017 04:48
Show Gist options
  • Save elnikkis/9a7387f03fec44de005fba885ca0d0f7 to your computer and use it in GitHub Desktop.
Save elnikkis/9a7387f03fec44de005fba885ca0d0f7 to your computer and use it in GitHub Desktop.
# coding:utf-8
def print_mat(mat):
for row in mat:
for c in row:
print(c, end=", ")
print()
def gauss_jordan(mat):
n = len(mat)
for y in range(n):
for x in range(n-1, 0, -1):
mat[y][x] = mat[y][x] / mat[y][y]
return mat
if __name__ == '__main__':
a = [
[1, 1, 1],
[1, 2, 3],
[1, 3, 2]]
ans = gauss_jordan(a)
print_mat(ans)
# -*- coding:utf-8 -*-
"""
Newton法でf(x)=0の方程式を解く
f(x)とその導関数であるdf(x)が分かっていなければならない。
"""
def f(x):
return x**3 + 4 * x**2 - 6
def df(x):
return 3 * x**2 + 8 * x
def newton(n, x=0.0):
for i in range(n):
print('f({0})={1}, df({0})={2}'.format(x, f(x), df(x)))
if f(x) == 0:
print('answer: x = {0}'.format(x))
return x
if df(x) == 0:
print('df({0}) = 0'.format(x))
continue
x = x - f(x) / df(x)
print(i, x)
return x
if __name__ == '__main__':
newton(10, x=4.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment