How to solve N nonlinear equations using fsolve?

I'm trying to solve a system on N nonlinear equations. For , for example, I can simply input 5 equations by hand that has 5 variables in total. Here's an example just to show what I use;
%%%%% Example %%%%%
f=@(x) [x(1)^2-x(2);
x(2)^2-x(3);
x(3)^2-x(4);
x(4)^2-x(5);
x(5)^2-x(1)];
X=ones(1,N-3);
S=fsolve(f,X);
%%%%% Example %%%%%
What I really want to do is to solve N nonlinear equations that has N variables in total. In this case there are 2 issues I need to solve.
1-) How can I input N variables?
2-) How can I input N functions?
Is there any loop I can use?
Thanks in advance.

댓글 수: 1

do you have the symbolic toolbox? if so then create a vector of equations and matlabFunction that

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2020년 5월 3일

1 개 추천

If you do not have the symbolic toolbox, you will need to enter the equations as text. strjoin() the equations with ';' and put '@(x)[' ']' around that, and str2func() to create the function handle.

댓글 수: 4

Berk Özdemir
Berk Özdemir 2020년 5월 3일
편집: Berk Özdemir 2020년 5월 3일
Thanks for the reply, but I think I made a mistake by using the word "input". What I want to do is to create a system of nonlinear equations which should be written in script such as;
x(1)^2-x(2)
x(2)^2-x(3)
x(3)^2-x(4)
...
x(N-1)^2-x(N)
x(N)^2-x(1)
which depends to N. As you know, for large values of N, it is impossible to write them by hand, so I'm looking for a loop if it is possible do so.
EDIT : I managed to create the system of equations by using matlabFunction. This time, I got "not enough input" error even though my initial vector has the same size as the number of variables when I try to solve it by using fsolve.
Make sure you use the 'vars' option of matlabFunction()
As you know, for large values of N, it is impossible to write them by hand
You can generate the equations symbolically if you have suitable patterns, and then use matlabFunction. Or you can generate the equations as text, and join the text together using
str2func(['@(x)[', strjoin(CellOfText', ';'), ']'])
You could also,
funs = arrayfun(@(idx) @(x) x(idx).^2 - x(idx+1), 1:N-1, 'uniform', 0);
obj = @(x) arrayfun(@(idx) funs{idx}(x), 1:length(funs));
fsolve(obj, x0)
However, I would point out that if you are restricting yourself to real numbers, then the solution to the above pattern is that x(N) must be non-negative, and that each x(N-1) = sqrt(x(N)) in series, except that x(1) can be +/- sqrt(x(2)). You can generate the entire sequence as x(N).^((1/2).^(N-1:-1:0))
Berk Özdemir
Berk Özdemir 2020년 5월 4일
편집: Berk Özdemir 2020년 5월 4일
Dear Walter,
Thank you for your response, using vars option of matlabFunction worked, but that led me another problem.
I create a vector of symbolic variables such as;
A=sym('x', [1;N-3]);
where N is entered by the user. I checked for ,
f=matlabFunction(F,'vars',{[A(1) A(2) A(3) A(4) A(5) A(6) A(7)});
S=fsolve(f,X0);
and it worked. But what if A has 1000 variables?
EDIT : I simply changed
f=matlabFunction(F,'vars',{[A(1) A(2) A(3) A(4) A(5) A(6) A(7)});
to
f=matlabFunction(F,'vars',{[A]});
and it worked.
That helped a lot, thank you.
You could also just use
f=matlabFunction(F,'vars',{A});

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by