Problem with solving set of symbolic equations
10 views (last 30 days)
Show older comments
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!
0 Comments
Answers (2)
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
0 Comments
Walter Roberson
on 30 Sep 2019
Use vpasolve(), or use matlabFunction to translate the symbolic vector to an anonymous function for use with fsolve()
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!