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)
Mostra commenti meno recenti
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));
0 Commenti
Risposte (2)
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
That empty row vector causes an error when evaluating f(x).
Instead, we could do
x = -1:-1:-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)
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..
0 Commenti
Star Strider
il 11 Set 2022
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)
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.
.
0 Commenti
Vedere anche
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!