How should I plot the function (sqrt(1 + x) - 1) / x using the values x = 0.1, 0.01, 0.001, ..., 10^-20?

4 visualizzazioni (ultimi 30 giorni)
Below is my attempt to write a program for part of an assignment that does what's mentioned in the title. I cannot seem to get it to work and it just returns a blank graph. I'm still very new to Matlab so I'm not sure what I'm specifcally doing wrong. Any help is greatly appreciated!
x = -1:-20
f = @(x) (sqrt(1 + (10^x)) - 1) / (10^x);
plot(f(x));

Risposte (2)

Paul
Paul il 11 Set 2022
Modificato: Paul il 11 Set 2022
Hi Jackson,
If you want x to from -1 to -20 you have to put the stride of -1 in the colon operator, otherwise it uses the default stride of +1, and since we can't step from -1 to -20 with stride of +1, x is an empty row vector.
x = -1:-20
x = 1×0 empty double row vector
That empty row vector causes an error when evaluating f(x).
Instead, we could do
x = -1:-1:-20
x = 1×20
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20
But these aren't really the values we want to use to evaluate the function.
I'd suggest defining the function as it's defined in the question.
It's important to use ./ (note the . (dot)) for element-wise division
f = @(x) (sqrt(1+x)-1)./x;
Now we want to evaluate at the indicated values. Again, we use element-wise power operator .^
format short e
vals = 10.^(-1:-1:-20)
vals = 1×20
1.0000e-01 1.0000e-02 1.0000e-03 1.0000e-04 1.0000e-05 1.0000e-06 1.0000e-07 1.0000e-08 1.0000e-09 1.0000e-10 1.0000e-11 1.0000e-12 1.0000e-13 1.0000e-14 1.0000e-15 1.0000e-16 1.0000e-17 1.0000e-18 1.0000e-19 1.0000e-20
The rest is up to you.
Suggest reading this doc page and the links at the bottom to learn more about Malab operators and operations..

Star Strider
Star Strider il 11 Set 2022
Using the logspace function to create the independent variable ‘x’ vector makes this easier.
Plot it as a function of the independent variable.
It returns a blank plot because you need to use element-wise array division (./) instead of matrix division (/) in order tor the numerator vector to be divided element-wise by the denominator vector. (This is probably the most frequent problem I see here on Answers, so you are not alone in not considering it. See Array vs. Matrix Operations for details.)
If you want it to go from to , it will be necessary to reverse the x-axis direction. MATLAB plots from the lowest (here ) to the highest () by default.
Try this —
format shortE
x = logspace(-20, -1, 20)
x = 1×20
1.0000e-20 1.0000e-19 1.0000e-18 1.0000e-17 1.0000e-16 1.0000e-15 1.0000e-14 1.0000e-13 1.0000e-12 1.0000e-11 1.0000e-10 1.0000e-09 1.0000e-08 1.0000e-07 1.0000e-06 1.0000e-05 1.0000e-04 1.0000e-03 1.0000e-02 1.0000e-01
f = @(x) (sqrt(1 + (x)) - 1) ./ (x);
figure
plot(x,f(x),'.-')
grid
Ax = gca;
Ax.XScale = 'log';
figure
plot(x,f(x),'.-')
grid
Ax = gca;
Ax.XScale = 'log';
Ax.XDir = 'reverse';
I log-scaled the x-axis and added the grid lines for clarity. Delete those lines if you do not want the plot to show them.
.

Categorie

Scopri di più su Time Series Objects in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by