Looks like a machine error
4 views (last 30 days)
Show older comments
Kwame Atta Gyamfi
on 10 Jan 2020
Answered: Steven Lord
on 10 Jan 2020
Dear all,
I need help with a tiny section of a code which involve a basic computation(seemingly):
The following operation,
>> ((-0.085 - -0.1)/(-0.085 - -0.098))*0.6 -(0.005/(-0.085 - -0.098))
gives
>> 0.3077
However, when I replace the figures with variables as below:
>> ((xp32 - xp12new)/(xp32next - xp12next))*U(tn,j+1)-(Dt/(xp32next - xp12new))
Here the variables have equal values of the figures in the preceeding operation.
I get a wierd answer:
>> 0.3590
I don't know what I'm missing here. Thanks in advance.
0 Comments
Accepted Answer
Steven Lord
on 10 Jan 2020
Your variables appear to contain the same values as the hard-coded constants, but looks can be deceiving. The small differences between the variable values and the constant values (which don't appear using the default display format but still exist in the stored values) result in the different answer.
You can check this by subtracting the constant values from the variables and noting that you receive a non-zero value.
xp32 - (-0.085) % Not exactly 0, but very small
You can also check this by changing the display format.
format longg
xp32 % not exactly -0.0085
0 Comments
More Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!