Determining The Matlab Output

In summary, the conversation discusses the confusion regarding the values of g(x) and h(x) and how they are affected by roundoff error. The speaker also questions the difference in output between MATLAB and their calculator. They seek clarification on the role of precision in these calculations.
  • #1
ver_mathstats
260
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
  • #2
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

1. How can I view the output of my Matlab code?

To view the output of your Matlab code, you can use the disp() function to display text or variables in the command window. Alternatively, you can use the fprintf() function to format and print your output in the command window.

2. How do I save the output of my Matlab code?

To save the output of your Matlab code, you can use the diary() function to create a diary file that records all the inputs and outputs in the command window. You can also use the save() function to save specific variables or arrays in a .mat file for future use.

3. How do I suppress the output of my Matlab code?

To suppress the output of your Matlab code, you can use the semicolon (;) at the end of the line to prevent the result from being displayed in the command window. You can also use the clc command to clear the command window before running your code to avoid cluttered output.

4. How can I format the output of my Matlab code?

You can use the fprintf() function to format your output in Matlab. This function allows you to specify the data type, precision, and alignment of your output. You can also use the sprintf() function to format and store your output as a string for further manipulation.

5. How do I debug the output of my Matlab code?

To debug the output of your Matlab code, you can use the debug mode in the Matlab editor. This allows you to step through your code line by line and check the values of variables at each step. You can also use the breakpoints feature to pause your code at a specific line and inspect the variables at that point.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
809
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
958
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
882
Back
Top