fsolve in for loop: failure in initial objective function evaluation
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Anastasia Gladkina
el 3 de Mzo. de 2017
Respondida: Anastasia Gladkina
el 6 de Mzo. de 2017
I'm trying to solve a system of nonlinear equations using fsolve, and I have to do it for many timesteps, nsteps. The initial condition (defined outside the loop, I'm solving a differential equation underneath) is u, which is used in the definition of the function func. The first time we go around the loop, u is defined as the initial condition; but after that, I want the solution given by fsolve to be assigned to u. Therefore, I have to keep the function definition inside the loop so that u is able to change.
g=1;
r=1j*dt/2;
nsteps=100;
nn=100;
for i=1:nsteps
func=@(z) r*g*(norm(z)^2)*z+z-deriv*z+r*g*(norm(u)^2)*u-u-deriv*u;
input=ones(nn,1)';
u=fsolve(func,input);
end
I get the error in the fsolve(func,input) line that says "Failure to initial objective function evaluation. FSOLVE cannot continue." I read somewhere it could be because the function is defined in the loop and should be defined outside in a separate file, but I cannot do that fix because u has to be the previous solution that fsolve found. I am not saving these u's elsewhere.
deriv is an nn x nn matrix, z is the free vector that we're trying to solve for, dimension nn x 1, u is also nn x 1, r and g are constants.
edit: I also get an error "Error using *, Inner matrix dimensions must agree" for the function definition. Maybe Matlab's not recognizing that z is a vector instead of a variable, and then norm would be undefined?
edit 2: I posted all of my code.
0 comentarios
Respuesta aceptada
Más respuestas (1)
Alan Weiss
el 3 de Mzo. de 2017
Perhaps the problem is your statement
input=ones(nn,1)';
That makes input a 1-by-nn vector, when you want it to be nn-by-1. Change the line to
input=ones(nn,1);
and see if that fixes things.
Alan Weiss
MATLAB mathematical toolbox documentation
Ver también
Categorías
Más información sobre Systems of Nonlinear Equations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!