Write a Matlab script that inputs a small number ε and large positive integer nMax, computes the smallest n∗such that

2 vues (au cours des 30 derniers jours)
So this question uses a while loop, and I almost have the correct answer, I just don't know how to input the maximum nMax into the while loop. This is what I have so far. When ep=10^-3 and nMax=10^3, it should display that n*=22 and fn_star=0.0217357060868536
%smallest n number
ep=input('Enter smal number epsilon:');
f=n*(1-cos(1/n));
fn=(n+1)*(1-cos(1/n));
err=abs(f-fn);
while err>=ep
n=n+1;
nMax=input('Enter maximum:');
f=fn;
fn=(n+1)*(1-cos(1/n));
err=abs(f-fn);
end
ns=n;
fprintf('for epsilon=%.16e\n',ep)
fprintf('the smallest value n*=%.16d\n',ns)
fprintf('fn_star=%.16f\n',fn)

Réponse acceptée

Geoff Hayes
Geoff Hayes le 25 Sep 2022
@Jason - I think you just need to get the nMax outside of the loop as presumably it is used to set a maximum number of iterations for the loop. For example,
ep=input('Enter smal number epsilon:');
nMax=input('Enter maximum:');
f=n*(1-cos(1/n));
fn=(n+1)*(1-cos(1/n));
err=abs(f-fn);
while err>=ep && n <= nMax
n=n+1;
f=fn;
fn=(n+1)*(1-cos(1/n));
err=abs(f-fn);
end

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by