Why Do MATLAB and My Calculator Show Different Results for g(x) and h(x)?

  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Matlab Output
AI Thread Summary
The discussion revolves around discrepancies in the results of g(x) and h(x) calculated using MATLAB and a calculator. The user notes that for x=1e-10, both functions have the same numerator but yield different results, with g(x) being positive and h(x) equating to zero. Additionally, for x=2-33, g(x) returns a negative value on the calculator while MATLAB shows it as zero. The conversation highlights the significance of precision and the order of operations, suggesting that roundoff errors may contribute to these differences in outputs. Understanding these factors is crucial for accurate calculations in numerical analysis.
ver_mathstats
Messages
258
Reaction score
21
Homework Statement
We are required to explain the values of g(x) and h(x).

The script for Matlab is:
g = @(x) (exp(x)-1-x)./x.^2;
h = @(x) (exp(x)-x-1)./x.^2;
x = 1e-10;
fprintf(’x=%.16e\ng(x)=%.16e\nh(x)=%.16e\n’, x, g(x), h(x))
x = 2^(-33);
fprintf(’x=%.16e\ng(x)=%.16e\nh(x)=%.16e\n’, x, g(x), h(x))

The output is:
x=1.0000000000000000e-10
g(x)=8.2740370962658164e+02
h(x)=0.0000000000000000e+00
x=1.1641532182693481e-10
g(x)=0.0000000000000000e+00
h(x)=0.0000000000000000e+00
Relevant Equations
-
I am having a bit of trouble understanding what the values of g(x) and h(x) mean. I went through it by hand and here is information that I gathered about the values and questions I am trying to answer to gain a better understanding. Firstly, for the value of x=1e-10 both the numerator of g(x) and the numerator of h(x) equate to the same number on my calculator and of course they have the same denominator, but then I don't understand why g(x) equals a positive number whereas h(x) equals 0, does this all just come from the error? I get the answer I am expecting for g(x) but not h(x).

Secondly, when I calculate g(x) for x=2-33 I get a negative number and when I calculate h(x) when x=2-33 I get 0. But why would the output for g(x) be 0 on MATLAB but when I calculate it I obtain a negative number? Is this simply only due to the precision of using 16 decimals whereas on my calculator it's much less?

Any help would be appreciated, thanks.
 
Physics news on Phys.org
ver_mathstats said:
I went through it by hand ... equate to the same number on my calculator
Working it out on your calculator is not going through it by hand.

ver_mathstats said:
does this all just come from the error? ... Is this simply only due to the precision of using 16 decimals whereas on my calculator it's much less?
This question is all about the impact of the order of operations on roundoff error.
 
  • Like
Likes ver_mathstats and DrClaude
Back
Top