yarnamc
yarnamc

Reputation: 143

Solving non-linear system of equations in MATLAB

I encountered a non-linear system of equations that has to be solved. The system of equations can be written as: Ax + exp(x) = b with b a known Nx1 matrix, A a known NxN matrix, and x the unknown Nx1 vector for which has to be solved. The exp is defined element-wise on the x vector. I tried to search the MATLAB-manual but I'm having a hard time finding how to solve this kind of equations with MATLAB, so I hope someone can help me out.

Upvotes: 2

Views: 1174

Answers (3)

Royi
Royi

Reputation: 4953

I just saw this is a cross post.

This is my solution for the other posting:

The function can be written in the form:

$$ f \left( x \right) = A x + \exp \left( x \right) - b $$

Which is equivalent to the above once a root of $ f \left( x \right) $ is found.
One could use Newton's Method for root finding.

The Jacobian (Like the Transpose of Gradient) of $ f \left( x \right) $ is given by:

$$ J \left( f \left( x \right) \right) = A + diag \left( \exp \left( x \right) \right) $$

Hence the newton iteration is given by:

$$ {x}^{k + 1} = {x}^{k} - { J \left( f \left( {x}^{k} \right) \right) }^{-1} f \left( {x}^{k} \right) $$

You can see the code in my Mathematics Q1462386 GitHub Repository which includes both analytic and numerical derivation of the Jacobian.

This is the result of one run:

enter image description here

Pay attention that while it finds a root for this problem there are more than 1 root hence the solution is one of many and depends on the initial point.

Upvotes: 0

Jeff Irwin
Jeff Irwin

Reputation: 1051

You can use Newton-Raphson. Re-arrange your system into a zero residual:

R = A * x + exp(x) - b

Then take the derivative of R with respect to x:

dRdx = A + diag(exp(x))

Then iterate. An example is shown below:

n = 3;

a = rand(n, n);
b = rand(n, 1);

% solve a * x + exp(x) = b for x

x = zeros(n, 1);

for itr = 1: 10
    x = x - (a + diag(exp(x))) \ (a * x + exp(x) - b);
end

Of course, you could make this more intelligent by stopping iteration after the residual is small enough.

Upvotes: 4

freude
freude

Reputation: 3832

I would solve it iteratively starting with the solution of the linearized system [A+1]x(0)=b-1 as an initial guess, where 1 is an identity matrix. At the each step of the iterative procedure I would add the exponential of the previous solution at the right-hand side: Ax(j)=b-exp(x(j-1))

Upvotes: 1

Related Questions