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 problem

Subject: fsolve problem

From: Gregory

Date: 3 Feb, 2012 06:55:12

Message: 1 of 5

Hey all,
I'm fairly new here so bear with me. I'm also pretty bad at matlab, just starting.
I'm trying to find values for unknowns. There are 19 equations and 19 unknowns but fsolve seems to give me an error such as this:

fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
default value of the function tolerance.

Here is the function I'm using:

function psi = Ufun(s)

n = 19;
i = 1:n;
s = zeros(1,19);

xi = i/(n+1);
xi(20) = 0;

yi = (0.18/0.2) .* (0.2969.*sqrt(xi) - ...
    0.126.*xi - 0.3516.*(xi.^2) + 0.2843.*(xi.^3) ...
    - 0.1015.*(xi.^4));

for j = 1:n
    for i = 1:n
     if xi(i) == xi(j)
         psiU(i) = 0;
     else
        psiU(i) = s(i) .* atan((yi(i) - yi(j)) ./ (xi(i) - xi(j)));
     end

        psi(j) = sum(psiU) + yi(j);
    end
end

end

And here is the fsolve part:
sz = ones(1,19);
s = fsolve(@Ufun,sz)

So can anyone help me out in figuring out why fsolve wouldn't give me any values for s? There are multiple ways of going about writing the function but I figured this is the easiest way.

Subject: fsolve problem

From: Roger Stafford

Date: 3 Feb, 2012 08:14:24

Message: 2 of 5

"Gregory" wrote in message <jgg0cg$dr6$1@newscl01ah.mathworks.com>...
> .......
> fsolve stopped because the problem appears regular as measured by the gradient,
> but the vector of function values is not near zero as measured by the
> default value of the function tolerance.
> .......
> function psi = Ufun(s)
> s = zeros(1,19);
> ........
> for i = 1:n
> if xi(i) == xi(j)
> psiU(i) = 0;
> else
> psiU(i) = s(i) .* atan((yi(i) - yi(j)) ./ (xi(i) - xi(j)));
> end
> psi(j) = sum(psiU) + yi(j);
> end
> ........
- - - - - - - - -
  I see problems here in 'Ufun' as you have written it. First, you set 's' to all zeros no matter what argument 'Ufun' receives. That is certainly calculated to frustrate 'fsolve'. It keeps getting back the same answer no matter what it sends to 'Ufun'. That could well explain the error message.

  However if that is fixed, next I see where you have apparently placed the line

 psi(j) = sum(psiU) + yi(j);

in the wrong place. Where it is, it keeps overwriting psi(j) for each value of i in the for-loop. Why would you want to do that?

  Where you test for xi(i) == xi(j), why not just test for i==j? No two xi's are equal for different indices as you have defined them.

  Finally what you have here is a set of n linear equations in n unknowns in the 's' values. Why not use matlab's excellent matrix division to solve them instead of calling on 'fsolve' where life is much less secure. All you have to do is initially work out all the needed coefficients.

Roger Stafford

Subject: fsolve problem

From: Gregory

Date: 3 Feb, 2012 08:24:20

Message: 3 of 5

> I see problems here in 'Ufun' as you have written it. First, you set 's' to all zeros no matter what argument 'Ufun' receives. That is certainly calculated to frustrate 'fsolve'. It keeps getting back the same answer no matter what it sends to 'Ufun'. That could well explain the error message.
>
> However if that is fixed, next I see where you have apparently placed the line
>
> psi(j) = sum(psiU) + yi(j);
>
> in the wrong place. Where it is, it keeps overwriting psi(j) for each value of i in the for-loop. Why would you want to do that?
>
> Where you test for xi(i) == xi(j), why not just test for i==j? No two xi's are equal for different indices as you have defined them.
>
> Finally what you have here is a set of n linear equations in n unknowns in the 's' values. Why not use matlab's excellent matrix division to solve them instead of calling on 'fsolve' where life is much less secure. All you have to do is initially work out all the needed coefficients.
>
> Roger Stafford

You were right on pis(j) not being in the right place.
I can't believe I didn't see that earlier. I feel so foolish.
Thank you Roger. I appreciate you taking the time to help.

Subject: fsolve problem

From: Steve Grikschat

Date: 3 Feb, 2012 15:26:20

Message: 4 of 5

"Gregory" wrote in message <jgg5jk$s02$1@newscl01ah.mathworks.com>...
> > I see problems here in 'Ufun' as you have written it. First, you set 's' to all zeros no matter what argument 'Ufun' receives. That is certainly calculated to frustrate 'fsolve'. It keeps getting back the same answer no matter what it sends to 'Ufun'. That could well explain the error message.
> >
> > However if that is fixed, next I see where you have apparently placed the line
> >
> > psi(j) = sum(psiU) + yi(j);
> >
> > in the wrong place. Where it is, it keeps overwriting psi(j) for each value of i in the for-loop. Why would you want to do that?
> >
> > Where you test for xi(i) == xi(j), why not just test for i==j? No two xi's are equal for different indices as you have defined them.
> >
> > Finally what you have here is a set of n linear equations in n unknowns in the 's' values. Why not use matlab's excellent matrix division to solve them instead of calling on 'fsolve' where life is much less secure. All you have to do is initially work out all the needed coefficients.
> >
> > Roger Stafford
>
> You were right on pis(j) not being in the right place.
> I can't believe I didn't see that earlier. I feel so foolish.
> Thank you Roger. I appreciate you taking the time to help.

Additionally, I want to attempt to answer your question about the message returned from fsolve.

What the message says is that the optimization has to stop because the gradient is near zero (meaning that it appears that it can't reduce the squared norm of the functions anymore). However, the residual (the squared norm of the functions) is not close to zero, suggesting that it has not found a solution to the problem F(x) = 0.

Subject: fsolve problem

From: Torsten

Date: 3 Feb, 2012 07:40:52

Message: 5 of 5

On 3 Feb., 07:55, "Gregory " <thr33z3...@gmail.com> wrote:
> Hey all,
> I'm fairly new here so bear with me. I'm also pretty bad at matlab, just starting.
> I'm trying to find values for unknowns. There are 19 equations and 19 unknowns but fsolve seems to give me an error such as this:
>
> fsolve stopped because the problem appears regular as measured by the gradient,
> but the vector of function values is not near zero as measured by the
> default value of the function tolerance.
>
> Here is the function I'm using:
>
> function psi = Ufun(s)
>
> n = 19;
> i = 1:n;
> s = zeros(1,19);
>
> xi = i/(n+1);
> xi(20) = 0;
>
> yi = (0.18/0.2) .* (0.2969.*sqrt(xi) - ...
>     0.126.*xi - 0.3516.*(xi.^2) + 0.2843.*(xi.^3) ...
>     - 0.1015.*(xi.^4));
>
> for j = 1:n
>     for i = 1:n
>      if xi(i) == xi(j)
>          psiU(i) = 0;
>      else
>         psiU(i) = s(i) .* atan((yi(i) - yi(j)) ./ (xi(i) - xi(j)));
>      end
>
>         psi(j) = sum(psiU) + yi(j);
>     end
> end
>
> end
>
> And here is the fsolve part:
> sz = ones(1,19);
> s = fsolve(@Ufun,sz)
>
> So can anyone help me out in figuring out why fsolve wouldn't give me any values for s? There are multiple ways of going about writing the function but I figured this is the easiest way.


fsolve tries to determine a vector s such that psi(s) = 0.
In the iteration process, fsolve wants you to evaluate your
function phi for a number of suggestions for the vector s in
order to get information on how to change s in order to
decrease |phi(s)|.
So why do you set the suggested values for s to zero in the line
> s = zeros(1,19);
?

Best wishes
Torsten.

Tags for 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