Why matlab output gives dIfferent result for a f(x) executed, into a program compared with the result of the same f(x) executed through the console?

2 views (last 30 days)
Hi, im actually working porting to java some portion of matlab code. Im not matlab programer, but it was very natural for me to understand matlab code and how it worls. For analysis and test reasons, i executed the original matlab code on matlab 2018 to get the outputs in each step of the program.
Actualiiy im stuck in this line:
t = 2 * pi * rem(time,1) + ll;
where: ll = -1.1087
and: time= 2457600
and: rem(time,1) gives a 0 result cause rem(2457600,1)
.....very simple......
1) But when the entire application is executed on matlab, it displays as a result, the value -0.7465
2)When i executed the same formula through the console,the result is -1.1087 (I think the correct value, for obvious reasons, Some people is using this application with this formula on an academic project, Im sure they must take notice of this error if it happened because they check the application output through many ways, Because of that i think i have something im not taking notice of cause of my short experience using matlab)
Someone who can help me?. Thanks in advance
Why matlab output gives dIfferent result for a f(x) into a program when is executed, compared with the same f(x) executed through the console?
  3 Comments
Walter Roberson
Walter Roberson on 9 Dec 2018
Try
which -all time
in both situations. I suspect that time is a local variable inside the function that is shadowing some function.
Stephen23
Stephen23 on 9 Dec 2018
Edited: Stephen23 on 9 Dec 2018
"Why matlab output gives dIfferent result for a f(x) into a program when is executed, compared with the same f(x) executed through the console? "
All sorts of reasons: a different value, different scope, changed search path, variable shadowing a function, etc, etc.
Use the debugging tools to find out what is happening.

Sign in to comment.

Accepted Answer

Andrew pit
Andrew pit on 12 Dec 2018
Yes, finally the correct SOLUTION for the issue is:
CONCLUSSION:
disp() command truncated the output, showing 2.4576e+06 while the complete value was 2457600.0576. Those decimals .0576 are very important in the f(x) to be calculated.
SOLUTION:
Input through the console the comand:
format long
Then any output as 2.4576e+06 will be displayed as:
##210# time :
time =
2.457636057638889e+06
Here we can watch all decimal places

More Answers (1)

Andrew pit
Andrew pit on 11 Dec 2018
Hi, Walter, Hi Steven, thank you very much for your contributions, i started using yours advices, and finally i founded this confusing situation for me generating the problem or well a kind of confusion:
When i executed the matlab program ,
t = 2 * pi * rem(time,1) + ll;
where: ll = -1.1087
and: time= 2457600
this means rem(time,1) will be zero.........
The error was here!!!! i was watching time variable value through a display(time); , and it shows me in the console the value 2.4576e+06, that is correct, but incomplete, cause i discover such value was truncated by MATLAB ( i think display() truncates output for some reason i dont undestand)
Finally......
I founded for the complete value of 'time' ,
with decimals inclided is 2457600.0576
this means rem(time,1) will be 0.0576 and not zero ( this changes everything in the final resut )
Using this value for 'time' (without truncation( gives me the correct result executing the f(x) through the console, or well when the f(x) is executed by the MATLAB application.
Thank you very much
  1 Comment
Stephen23
Stephen23 on 11 Dec 2018
Edited: Stephen23 on 12 Dec 2018
So you used different input values, and got different output values.
Remember that how a value is displayed is not the same thing as what value is stored in MATLAB memory. The only reliable way to use the same value in two locations is to pass it as an argument.
"i think display() truncates output for some reason i dont undestand"
Learn about binary floating point numbers. Most binary floating point numbers are truncated in their decimal representation.

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!