Am i stupid ? The easiest mistake of all. 2016b and basic maths problems

Hello,
I have such a simple problem i don't even know how to explain it. Trying to compare two vectors at equal column numbers, one is equal to 1 the other varies, however, definitely goes above one.
if true
A = (b_lim_l>chg_cum); % if b_lim_l is larger than chg_cum (returns1)
B = wind_p(pos) < demand(pos); % checks if demand is larger than supply(returns1)
C = A==1 & B==1; % returns 1 if both (A and B) ==1
for i = 1:range;
if C(i)==1; %when C==1
import(i)=(demand(i)-wind_p(i)); % do this
else
import(i)=0; % if C==0 do this
end
end
end
problem here is... A returns 1 at all times, even when chg_cum is larger than b_lim_l.... B doesn't work either
to prove this
b_lim_l(4000)
ans =
1
>> chg_cum(4000)
ans =
35.6958
>> A(4000)
ans =
logical
1

7 Comments

Does this happen when b_lim_l and chg_cum have only one element, the ones you mention above (from index 4000)?
By the way, what are the sizes of b_lim_l and chg_cum?
Also, what are the class of the variables?
I suspect that is some information missing from the question though. According to the demonstration A is a vector, yet B looks like a scalar (unless pos is a matrix the same size as b_lim_l), which will cause the logical test on the next line to fail due to inconsistent dimensions (in versions < R2016b).
Hi, Matlab ver is 2016b pos is a 4000*1 double b_lim_l is a 4000*1 double and chg_cum is a 4000*1 double
Debug things from the command line one line at a time. Start by typing b_lim_l(4000)>chg_cum(4000). I expect there is something amiss in your environment. You did not define true=false by accident preventing your code from running? Note that there appears to be a bug in the work space browser that it refuses to show true with a value of false or 0.
@ILONA DOMAGALA: you can either work through the code yourself using the debugging tools, or upload some sample data for us to try out.
@Stephen Cobeldick ; Stephen, uploading the code and required data files. Please have a look if you have time, I just don't know at this stage, I've spend so long at this I have no clue.
required data files can be found @ Here
Thanks alot,
In your Grid_Model.m, chg_cum gets initialised to a vector of 0 and never changes from then on. So it's unclear how you get chg_cum(4000) to be 35.6958.
If it's all zero, it makes sense that A is all one, since b_lim_l is 1.
edit:
A bigger problem is later on in the code:
if J(pos)==1; %== 1 redundant, J is already logical. semicolon not necessary
chg(pos)=(wind_p(pos))-demand(pos); %why the useless brackets around wind_p(pos)?
end
Since pos is a vector, the if will only be true if all of J(pos) is true. I'm fairly certain that's not the intent. I think the intent was:
chg(J(pos)) = wind_p(J(pos)) - demand(J(pos)); %only set values for which J(pos) is true. No if needed.
However since pos is all indices anyway:
chg(J) = wind_p(J) - demand(J);

Sign in to comment.

Answers (0)

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Asked:

on 22 Nov 2016

Edited:

on 29 Nov 2016

Community Treasure Hunt

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

Start Hunting!