# Solving simultaneous equations to make function

I was calculating with Mathematica.

Equations are

5 x1 - 10 x2 + 4 x3 = 1
x1 - 8 x2 + 10 x3 = 25
10 x1 - 6 x2 + 2 x3 = 6


My process is

x011 := 1
x012 := 1
x013 := 1
Round[f1 = {x11 = 0.6 + 0.6 #2 - 0.2 #3,
x12 = -0.1 + 0.5 #4 + 0.4 #3,
x13 = 2.5 - 0.1 #4 + 0.8 #5} &[x011, x012, x013, x11, x12], 0.0001]


output

{1., 0.8, 3.04}


and

x021 := f1[]
x022 := f1[]
x023 := f1[]
Round[f2 = {x21 = 0.6 + 0.6 #2 - 0.2 #3,
x22 = -0.1 + 0.5 #4 + 0.4 #3,
x23 = 2.5 - 0.1 #4 + 0.8 #5} &[x021, x022, x023, x21, x22], 0.0001]

{0.472, 1.352, 3.5344}

x031 := f2[]
x032 := f2[]
x033 := f2[]
Round[f3 = {x31 = 0.6 + 0.6 #2 - 0.2 #3,
x32 = -0.1 + 0.5 #4 + 0.4 #3,
x33 = 2.5 - 0.1 #4 + 0.8 #5} &[x031, x032, x033, x31, x32], 0.0001]

{0.7043, 1.6659, 3.7623}

x041 := f3[]
x042 := f3[]
x043 := f3[]
Round[f4 = {x41 = 0.6 + 0.6 #2 - 0.2 #3,
x42 = -0.1 + 0.5 #4 + 0.4 #3,
x43 = 2.5 - 0.1 #4 + 0.8 #5} &[x041, x042, x043, x41, x42], 0.0001]

{0.8471, 1.8285, 3.8781}

x051 := f4[]
x052 := f4[]
x053 := f4[]
Round[f5 = {x51 = 0.6 + 0.6 #2 - 0.2 #3,
x52 = -0.1 + 0.5 #4 + 0.4 #3,
x53 = 2.5 - 0.1 #4 + 0.8 #5} &[x051, x052, x053, x51, x52], 0.0001]

{0.9215, 1.912, 3.9374}


I want to make this process simply, i.e., make one function to obtain each solution.

• I dont understand what you are trying to do. You should also check your definitions and basic syntax at the very beginning (= is not used for equations but ==). If you want to solve a system of linear equations in x1, x2 and x3, use Solve or LinearSolve (see documentation). – Mauricio Fernández Nov 15 '16 at 12:31

General note: for solving linear systems of equations, use Solve:

Solve[{5 x1 - 10 x2 + 4 x3 == 1, x1 - 8 x2 + 10 x3 == 25,
10 x1 - 6 x2 + 2 x3 == 6}, {x1, x2, x3}]


{{x1 -> 1, x2 -> 2, x3 -> 4}}

If this is meant to be an illustration/homework for, e.g., a numerical methods class, then:

From the system of equations, one-by-one, solve for variables wrt. the other variables, i.e.

Solve[10 x1 - 6 x2 + 2 x3 == 6., x1] // Expand


{{x1 -> 0.6 + 0.6 x2 - 0.2 x3}}

Solve[5 x1 - 10 x2 + 4 x3 == 1., x2] // Expand


{{x2 -> -0.1 + 0.5 x1 + 0.4 x3}}

Solve[x1 - 8 x2 + 10 x3 == 25., x3] // Expand


{{x3 -> 2.5 - 0.1 x1 + 0.8 x2}}

f = {0.6 + 0.6 #[] - 0.2 #[], -0.1 + 0.5 #[] + 0.4 #[], 2.5 - 0.1 #[] + 0.8 #[]} &


that one wants to nest onto the starting values {1, 1, 1} several times; Nest is designed for this:

Nest[f, {1, 1, 1}, 10]


{0.894418, 2.05859, 3.86727}

Nest[f, {1, 1, 1}, 50]


{0.999649, 2.00038, 3.99961}

Nest[f, {1, 1, 1}, 100]


{1., 2., 4.}

For finding the solution with a prescribed accuracy, FixedPoint could be used:

FixedPoint[f, {1, 1, 1}, SameTest -> (Max@Abs[#1 - #2] < 0.001 &)]


{1.0004, 1.99956, 4.00045}

FixedPoint[f, {1, 1, 1}, SameTest -> (Max@Abs[#1 - #2] < 0.0001 &)]


{0.999961, 2.00004, 3.99996}

FixedPoint[f, {1, 1, 1}, SameTest -> (Max@Abs[#1 - #2] < 0.00001 &)]


{1., 2., 4.}