Write a Matlab script that inputs a small number ε and large positive integer nMax, computes the smallest n∗such that
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jason
le 25 Sep 2022
Réponse apportée : Geoff Hayes
le 25 Sep 2022
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)
0 commentaires
Réponse acceptée
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
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Language Support dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!