How to solve N nonlinear equations using fsolve?

4 visualizaciones (últimos 30 días)
Berk Özdemir
Berk Özdemir el 3 de Mayo de 2020
Comentada: Walter Roberson el 4 de Mayo de 2020
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 comentario
Walter Roberson
Walter Roberson el 3 de Mayo de 2020
do you have the symbolic toolbox? if so then create a vector of equations and matlabFunction that

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Mayo de 2020
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 comentarios
Berk Özdemir
Berk Özdemir el 4 de Mayo de 2020
Editada: Berk Özdemir el 4 de Mayo de 2020
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.
Walter Roberson
Walter Roberson el 4 de Mayo de 2020
You could also just use
f=matlabFunction(F,'vars',{A});

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by