필터 지우기
필터 지우기

fsolve in for loop: failure in initial objective function evaluation

조회 수: 1 (최근 30일)
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.

채택된 답변

Anastasia Gladkina
Anastasia Gladkina 2017년 3월 6일
The problem was that I uncommented one of the lines that I needed to set up the initial condition. Whoops!

추가 답변 (1개)

Alan Weiss
Alan Weiss 2017년 3월 3일
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
Anastasia Gladkina
Anastasia Gladkina 2017년 3월 6일
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.

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

카테고리

Help CenterFile Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by