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

1 visualizzazione (ultimi 30 giorni)
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)

Risposta accettata

Geoff Hayes
Geoff Hayes il 25 Set 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

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by