Problem with solving set of symbolic equations
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Raunak Gupta
2019-9-30
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 个评论
Walter Roberson
2019-9-30
Use vpasolve(), or use matlabFunction to translate the symbolic vector to an anonymous function for use with fsolve()
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!