Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fsolve - premature and stalled

Subject: fsolve - premature and stalled

From: KUNHEE

Date: 4 Apr, 2013 11:39:07

Message: 1 of 7

Hi, I am trying to use fsolve for a problem I need to solve.

The code works perfectly for one case (cartesian equations) but when modified to adopt axisymmetric, it does not work.

The error message shows :
"fsolve stopped because the relative size of the current step is less than the
default value of the step size tolerance squared and the vector of function values
is near zero as measured by the default value of the function tolerance."

I have checked the coding several times but I am unable to fix this problem.
I believe it is a problem with optimisation and to do with function solution tolerances. I tried loosening up on MaxFunEvals, MaxIter, which eliminated problem with premature stops, but I still have stalling problem. I also tried TolFun and TolX, unfortunately, no improvement.

If anyone could suggest a solution or point me in the direction, or just anything.....I am struggling big time.

The code of the function is as follows:
% Accepts nx1 vector h, a real x (equidistant mesh size)
% and integer n (# of mesh points)
% non-dimensional constant d
% Returns nx1 vector corresponding to A(h)
function [f] = elasticdef(h)
global x X d dt hfirststep ratio_terms gravityterm
n=length(h);
%matrices approximating the derivatives
s0 = ones(n,1); s1 = ones(n-1,1);
s2 = ones(n-2,1); s3 = ones(n-3,1);
D41 = sparse(diag(s3,3) + diag(-5*s2,2) + diag(10*s1,1)...
+ diag(-10*s0) + diag(5*s1,-1) + diag(-s2,-2));
D42 = sparse(diag(s3,3) + diag(-6*s2,2) + diag(15*s1,1)...
+ diag(-20*s0) + diag(15*s1,-1) + diag(-6*s2,-2)...
+ diag(s3,-3));
D43 = sparse(diag(s2,2) + diag(-5*s1,1) + diag(10*s0)...
+ diag(-10*s1,-1) + diag(5*s2,-2) + diag(-s3,-3));

D21 = sparse(diag(s1,1) + diag(-s0));
D22 = sparse(diag(s1,1) + diag(-2*s0) + diag(s1,-1) );
D23 = sparse(diag(s0) + diag(-s1,-1));

D1 = sparse(diag(s1,1) + diag(-s1,-1));

Du = sparse(diag(s1,1)); Dd = sparse(diag(s1,-1));

h3 = h.^3;

A4=ratio_terms*((Du*h3).*(D41*h) + (h3).*(D42*h)...
- (Dd*h3).*(D43*h));
A2 = ((Du*h3).*(D21*h) + (h3).*(D22*h)...
- (Dd*h3).*(D23*h));

for i=1:n
    A4(i,:)=A4(i,:)./abs(X(i).^3);
    A2(i,:)=A2(i,:).*abs(X(i));
end

for i=1:n
    A4(i,:) = (A4(i,:)./(2*x^6))./abs(X(i));
    A2(i,:) = (A2(i,:)./(2*x^2))./abs(X(i));
end

%%A1 = -(D1*h3)/(2*x);
A1 = 0;
%for solving the PDE with 0 inclination
A = A4 + (gravityterm*A2) + A1; %% note the change of sign
A(1) = 0; A(2) = 0;
A(end) = 0; A(end-1) = 0;
A(3) = A(3) + ((h3(3)+h3(2) )*h(2))/(2*x^6);
A(end-2) = A(end-2) + ((h3(end-1)+h3(end-2))*h(end-1))/(2*x^6);

for i=1:n
    f(i)=h(i)-hfirststep(i)-A(i)*dt;
end

================================================
and the code which calls upon the function is as follows:

close all
clear all
N=30;
X=linspace(-4,4,N);
h=exp(-(X.^2));
h=h';

d=1;

x=X(2)-X(1);
t = 0; dt = 0.3;
tmax = 2000;
plot(X,h); hold on

global x X d dt hfirststep ratio_terms gravityterm
ratio_terms=1;
hfirststep=h;
gravityterm=1;

for i = 1:4 %two loops are used so the solution can
for j = 1:3 %be plotted only at selected intervals
    j , i
t = t+dt;
h=fsolve('elasticdef',hfirststep,optimset('MaxFunEvals',3000000,'MaxIter',400000));

hfirststep=h;
end
plot(X,h); hold on

I=find(h>0.01*max(h));
length_gc(i)=abs(X(max(I))-X(min(I)));
end
figure(2)
loglog(length_gc,'k+');
hold on
===============================================

Anyhelp will be greatly appreciated
Thanks a lot for even reading this long post
Thanks a lot

Regards,
Thanks a lot

Subject: fsolve - premature and stalled

From: Alan_Weiss

Date: 4 Apr, 2013 12:17:23

Message: 2 of 7

On 4/4/2013 7:39 AM, KUNHEE wrote:
> Hi, I am trying to use fsolve for a problem I need to solve.
>
> The code works perfectly for one case (cartesian equations) but when
> modified to adopt axisymmetric, it does not work.
>
> The error message shows :
> "fsolve stopped because the relative size of the current step is less
> than the
> default value of the step size tolerance squared and the vector of
> function values
> is near zero as measured by the default value of the function tolerance."
>
> I have checked the coding several times but I am unable to fix this
> problem.
> I believe it is a problem with optimisation and to do with function
> solution tolerances. I tried loosening up on MaxFunEvals, MaxIter,
> which eliminated problem with premature stops, but I still have
> stalling problem. I also tried TolFun and TolX, unfortunately, no
> improvement.
>
> If anyone could suggest a solution or point me in the direction, or
> just anything.....I am struggling big time.
>
> **SNIP**
> Anyhelp will be greatly appreciated
> Thanks a lot for even reading this long post
> Thanks a lot
>
> Regards,
> Thanks a lot

The exit message does not indicate a problem, and, indeed, fsolve told
you it found a solution: "... the vector of function values is near zero
as measured by the default value of the function tolerance."

Did you check to see if the solution was satisfactory? I bet it is.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: fsolve - premature and stalled

From: KUNHEE

Date: 4 Apr, 2013 12:32:07

Message: 3 of 7

Alan_Weiss <aweiss@mathworks.com> wrote in message <kjjr0j$i08$3@newscl01ah.mathworks.com>...
> On 4/4/2013 7:39 AM, KUNHEE wrote:
> > Hi, I am trying to use fsolve for a problem I need to solve.
> >
> > The code works perfectly for one case (cartesian equations) but when
> > modified to adopt axisymmetric, it does not work.
> >
> > The error message shows :
> > "fsolve stopped because the relative size of the current step is less
> > than the
> > default value of the step size tolerance squared and the vector of
> > function values
> > is near zero as measured by the default value of the function tolerance."
> >
> > I have checked the coding several times but I am unable to fix this
> > problem.
> > I believe it is a problem with optimisation and to do with function
> > solution tolerances. I tried loosening up on MaxFunEvals, MaxIter,
> > which eliminated problem with premature stops, but I still have
> > stalling problem. I also tried TolFun and TolX, unfortunately, no
> > improvement.
> >
> > If anyone could suggest a solution or point me in the direction, or
> > just anything.....I am struggling big time.
> >
> > **SNIP**
> > Anyhelp will be greatly appreciated
> > Thanks a lot for even reading this long post
> > Thanks a lot
> >
> > Regards,
> > Thanks a lot
>
> The exit message does not indicate a problem, and, indeed, fsolve told
> you it found a solution: "... the vector of function values is near zero
> as measured by the default value of the function tolerance."
>
> Did you check to see if the solution was satisfactory? I bet it is.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation


Hi,

Thanks for your reply, I appreciate your help

However, the result should show that range of X where value of h is not zero should increase, in other words, the first graph (modelling motion of a incompressible finite volume fluid) should flow away, in both X directions. Instead, it the height lowers, but the fluid never moves out.

the solution becomes very weird when I try to increase either the number of time steps or the time intervals. I do not think the problem is mathematical but with the my lack of programming skills.

I understand it must be difficult to help me since I don't understand what the problem is in the first place. The solution is definately not satisfactory.

Do you have any idea why?

Many Thanks,
Kunhee Lee

Subject: fsolve - premature and stalled

From: KUNHEE

Date: 4 Apr, 2013 12:55:08

Message: 4 of 7

My initial thoughts are that the mesh size is too large for well-defined simulation, therefore I increased number of meshes, then I do not get a solution and get this message:

No solution found.

fsolve stopped because the relative size of the current step is less than the
selected value of the step size tolerance squared, but the vector of function values
is not near zero as measured by the selected value of the function tolerance.

Do you have any idea how to solve this problem?

Many Thanks,
Kunhee Lee

Subject: fsolve - premature and stalled

From: KUNHEE

Date: 4 Apr, 2013 12:59:08

Message: 5 of 7

I apologize. I am so sorry. I should have told you the initial problem.
 
When I first tried, it did not give me a solution and showed:

"No solution found.

fsolve stopped because the relative size of the current step is less than the
selected value of the step size tolerance squared, but the vector of function values
is not near zero as measured by the selected value of the function tolerance."

This problem was overcome by reducing the number of meshes, which might have caused the inaccurate results.

Do you have any idea how to solve this problem? or why it does not want to give me a solution?

Thank you so much for your help,
Kunhee Lee

Subject: fsolve - premature and stalled

From: Alan_Weiss

Date: 5 Apr, 2013 11:56:02

Message: 6 of 7

On 4/4/2013 8:59 AM, KUNHEE wrote:
> I apologize. I am so sorry. I should have told you the initial problem.
>
> When I first tried, it did not give me a solution and showed:
>
> "No solution found.
>
> fsolve stopped because the relative size of the current step is less
> than the
> selected value of the step size tolerance squared, but the vector of
> function values
> is not near zero as measured by the selected value of the function
> tolerance."
>
> This problem was overcome by reducing the number of meshes, which
> might have caused the inaccurate results.
> Do you have any idea how to solve this problem? or why it does not
> want to give me a solution?
>
> Thank you so much for your help,
> Kunhee Lee

I feel that I came in somewhere in the middle of a conversation. You
talk about incompressible fluids, graphs, meshes, and time steps, none
of which have anything to do with fsolve as far as I know.

So, if you don't mind, I'll restrict my comments to fsolve issues.

There can be many reasons why fsolve does not reach a solution. Some
suggestions are listed here:
http://www.mathworks.com/help/optim/ug/when-the-solver-fails.html#br7o7ou

If your objective function vector is the result of a simulation or
solution to an ODE, then fsolve can have trouble because of difficulties
in estimating derivatives. See
http://www.mathworks.com/help/optim/ug/optimizing-a-simulation-or-ordinary-differential-equation.html

It is possible that you are integrating a function whose value is given
by an fsolve solution. In that case, it is usually a good idea to use
the starting point for fsolve at the next time step in the integration
as the previous fsolve solution. Usually, solutions don't change much as
you vary time. But, of course, you can hit a point where this procedure
does not work, and then you have to look all over for a good starting point.

Good luck,

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: fsolve - premature and stalled

From: KUNHEE

Date: 10 Apr, 2013 11:28:08

Message: 7 of 7

Mr. Alen Weiss

Thank you very much for your advice.

I think I have an idea why it isn't doing what I expected with your kind help.

Hopefully I can get over this problem soon.

Thanks,
Kunhee lee

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us