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