Clear Filters
Clear Filters

what chace i need to make better answer? check fsolve

1 view (last 30 days)
I have this algorithm:
function F=giaiphuongtrinh(x)
Pv=5;
Pred=6;
Pl=10;
Ebmax=20;
Ebmin=2;
Eb=2;
Pdiesel=2;
Edum=0;
if (Pv+Pred)>=Pl
x(2)=Pv+Pred-Pl
Ech=x(2)
if Ech<=(Ebmax-Eb)
Eb=Eb+Ech
x(3)=0
else
Eb=Ebmax
x(3)=Ech-(Ebmax-Eb)
end
else
x(1)=Pl-(Pv+Pred)
Edch=x(1)
if Edch<=(Eb-Ebmin)
Eb=Eb-Edch
else
Eb=Ebmin
end
end
F=(Pl-(Pv+Pred+Pdiesel+x(1)-x(2)-x(3)))
I ran:
x0=[0;0;0];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@giaiphuongtrinh,x0,options)
and the result is:
4 20 3.81809e-28 1.95e-14 1e-06 1.97781e-09
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
x =
-2.0000
0
0
fval =
-1.9540e-14
I think it must be another result. Do you see something wrong? Please help me. Thank you so much.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Oct 2016
Your code appears to assume that if you change the variable Eb in one call, that it will have the new value in the next call. That is not the case. All of the local variables disappear as soon as the call returns, and are re-initialized according to your assignment statements.
Your assignment statements for Pv, Pred, Pl are such that your test
if (Pv+Pred)>=Pl
is always true. Then, when it is true, you ignore the value of x(2) passed in and do
x(2)=Pv+Pred-Pl
which assigns in a constant value to x(2). That constant x(2) is then assigned to Ech and the test
if Ech<=(Ebmax-Eb)
is effectively testing constants and is always true as well. You then do
x(3)=0
so you are ignoring the input x(3) and assigning a constant to x(3)
Your last line
F=(Pl-(Pv+Pred+Pdiesel+x(1)-x(2)-x(3)))
is then working with pure constants, and an x(2) that is calculated from constants, and x(3) that has been assigned a constant 0. Your code is therefore really only a single variable system, just x(1) having any influence on the output value. It is a plain linear system and has a single solution in x(1)
You might have expected the output x(2) and x(3) to have different values, perhaps the values that you assigned to them during execution of the function. Remember, though, that the function is being passed a copy of a variable, and changes to x(2) and x(3) are operating on that copy, with no change being made to the actual variable. fsolve() is telling you what x value it passed in to your function to get the result of 0, and it happened to pass in [-2 0 0] and then your function ignored the two 0's but fsolve doesn't know that.

More Answers (1)

Steven Lord
Steven Lord on 13 Oct 2016
Why do you believe something is wrong?
The message that was displayed is NOT, repeat NOT an error. If it had been an error the message would have appeared in red text and probably would have contained the word "error". That is a informational status message telling you why fsolve stopped iterating.

Tags

Community Treasure Hunt

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

Start Hunting!