Azzera filtri
Azzera filtri

fsolve in for loop: failure in initial objective function evaluation

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Anastasia Gladkina
Anastasia Gladkina il 6 Mar 2017
The problem was that I uncommented one of the lines that I needed to set up the initial condition. Whoops!

Più risposte (1)

Alan Weiss
Alan Weiss il 3 Mar 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
  1 Commento
Anastasia Gladkina
Anastasia Gladkina il 6 Mar 2017
If I change input to not be transposed, I get almost the same error. It says "Error using -, Matrix dimensions must agree", while before, the error was "Error using *, Matrix dimensions must agree".
I will post all of my code because I realize it's difficult to troubleshoot.

Accedi per commentare.

Categorie

Scopri di più su Systems of Nonlinear Equations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by