In the previous example, the values f(3) and [f (1), f(2), f(3)] were displayed
ID: 3111730 • Letter: I
Question
In the previous example, the values f(3) and [f (1), f(2), f(3)] were displayed in the command window using the numeric format we specified in preferences. Sometimes we want more control over the format, so we use the fprintf command (Section 1 of the Overview). As an example, f = @(x) x. 2 + 1: x = .6221: fprintf('f(%6.5f) = %12.4e ', x, f(x)) fprintf('f(%6.5f) = %12.4e ', 3.706, f(3.706)) Note that changing the output display does not change the value stored in MAT-LAB. Your assignment is to print the values of f(x) = sin(x^3) at the points x = 5.201,-8323.6, 0.0003 in floating point (f format) with 8 digits after the decimal and in scientific notation (e format) with 10 digits after the decimal. Check the help page (type helpwin fprintf) for details. Notice that the floating point format is not appropriate for the third x: get in the habit of printing in scientific notation to see the magnitude of a number.Explanation / Answer
%%%%%%% Matlab code %%%%%%%%%%%%%%%%
f=@(x) sin(x^3);
x=5.201;
fprintf('f(%1.8f) = %1.12e ', x,f(x));
x=-8323.6;
fprintf('f(%1.8f) = %1.12e ', x,f(x));
x=0.0003;
fprintf('f(%1.8f) = %1.12e ', x,f(x));
OUTPUT:
f(5.20100000) = 6.307612253751e-001
f(-8323.60000000) = -5.279469641728e-001
f(0.00030000) = 2.700000000000e-011
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.