Looks like a machine error

9 次查看(过去 30 天)
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.

采纳的回答

Steven Lord
Steven Lord 2020-1-10
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 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by