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

조회 수: 1 (최근 30일)
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)

채택된 답변

Geoff Hayes
Geoff Hayes 2022년 9월 25일
@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개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by