-1

Here's the code :

  clear;
channel = ddeinit('view','tagname');
temperature = ddereq(channel,'temperature');
poistka = 0;
time = 0;
kvas = 0;
ohrev= 1;

steam=300;
pressure=100;
steam2= 50;
tempom = 1;
pom = 0;
while time<3600
ventil = ddereq(channel,'ventil');    
pause(0.1);
time= time+1;
pom = pom+1;

if (kvas<=100)
kvas = kvas+1;
end;

if (kvas>=100 && temperature<95 && ohrev==1)

     temperature = temperature+1;
    tempom=0;

end;

if (temperature==95)
    ohrev=0;

end;

if (ohrev==0)  
temperature = temperature -0.1;
tempom = 1;

end;

if (temperature==70)
ohrev=1;

end;

    end;

I'm comunnicating with matlab and doing visualisation in intouch but I can't figure out why the variable ohrev won't became 1 when temperature reaches 70 value. It goes up to the 95, then goes down to 0 but it should stop at 70 and again go to 95 and so on but it doesn't work. Any advices? Thank you very much

s0re
  • 35
  • 9
  • your code is incomplete - variable temperature isn't started. – 16per9 May 09 '16 at 19:05
  • 1
    My guess: [don't perform exact matches with floating point numbers](http://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab), especially when they're computed not assigned. – TroyHaskin May 09 '16 at 19:07
  • I run your code and when kvas becomes 100, variable temperature raises up to 70 and reaches it's value. – 16per9 May 09 '16 at 19:09
  • I edited it, please check it now – s0re May 09 '16 at 19:15

1 Answers1

1

The problem is where you check for the specific temperature of 70 degrees:

if (temperature==70)
   ohrev=1;
end;

The reason this fails has to do with fundamental issues regarding the representation of floating point numbers. For example:

>> fprintf('%0.17e', 0.1)
1.00000000000000010e-01

Notice that in MATLAB (and most general-purpose languages) the floating point literal 0.1 is not represented exactly as a MATLAB floating point number. There is a little extra in the 16th decimal place. For that reason, once you start subtracting 0.1 from your integer temperature value:

if (ohrev==0)  
    temperature = temperature -0.1;
    tempom = 1;
end;

you will no longer have a number that is exactly an integer value. Hence, the test temperature == 70 can never be true.

The general solution is to ALWAYS check floating point numbers using a tolerance. So instead of checking equality, do the following:

tolerance = 1e-6;  %% 0.000001; use whatever makes sense for your program
if abs(temperature - 70) < tolerance
    ohrev = 1;
end

This is a general issue when working with floating point numbers, so I strongly recommmend reading more on the topic if you are going to write scientific programs in MATLAB (or Python, or Java, etc.)

More: resources:

http://www.mathworks.com/matlabcentral/answers/57444-faq-why-is-0-3-0-2-0-1-not-equal-to-zero

Why can't decimal numbers be represented exactly in binary?

Community
  • 1
  • 1
gariepy
  • 3,576
  • 6
  • 21
  • 34
  • Isn't your second name Genius? Thank you very much! Well you know, it's my task, I didn't choose matlab... – s0re May 10 '16 at 10:15
  • :) well, like I mentioned, you'll have this problem in almost any language using the native tools, but it's easy to work around. There are packages that allow for precision arithmetic computations, but they are usually much, much slower than using the builtin floating point capabilities. – gariepy May 10 '16 at 14:47