Asked by Adi Yolovitch
on 26 Sep 2019

hello everyone

Im trying to solve set of N - symbolic equations. The equations are non-linear and the variables are defined as symbolic:

x= sym('x', [1 n]) %% x1,x2 ... xn

the problem is that for each N, the N equations looks different, so I used "for" loops. At the end, i got 1xN array with the equations, called "eqn".

I read about using "fsolve" but I found 2 major problems. first, using fsolve requires x(i) instead of the symbolic xi. second, The equations must be written in advance in the form of

F(1)=...

F(2)=..

Because of the use of loops, I cannot know in advance how my equations will look like.

for example- this is the set for N=2:

eqn =

[ 1/x2 - 1/x1 + 170 == 0, 170 - 1/x2 - 1/(x1 + x2) == 0]

and for N=3:

eqn =

[ 1/(x2 + x3) - 1/x1 + 1/x2 + 170 == 0, 1/x3 - 1/x2 - 1/(x1 + x2) + 170 == 0, 170 - 1/(x2 + x3) - 1/x3 - 1/(x1 + x2 + x3) == 0]

thanks a lot!

Answer by Raunak Gupta
on 30 Sep 2019

Hi,

I can understand from the question that you want to solve a set of N non-linear equation using fsolve. As the example given in the documentation of fsolve itself there is no need of declaring variable x if we assume that we have equal number of unknown variables and non-linear equations.

As it is mentioned that the value of N is known beforehand (as used in the for loop), the non-linear equations can also be declared in a for loop inside a solver function if there is some relationship between equations and variable which can be captured by the index. Otherwise declaring them one-by-one may only help. In most of the cases of solving system of non-linear equation such relationship which can be captured in a for loop is difficult.

You may look for a better initialization point if fsolve is not converging. Here is an example which may help you.

% If no relationship between variable and equations with respect to indices

fun = @funsolver;

x0 = [1,1,1];

x = fsolve(funsolver,x0);

function F = funsolver(x)

F(1) = 1/(x(2)+x(3)) - 1/x(1) + 1/x(2) + 170;

F(2) = 1/x(3) - 1/x(2) - 1/(x(1) + x(2)) + 170;

F(3) = 170 - 1/(x(2) + x(3)) - 1/x(3) - 1/(x(1) + x(2) + x(3));

end

Sign in to comment.

Answer by Walter Roberson
on 30 Sep 2019

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.