Не могли бы вы объяснить, как работает этот код? Я могу понять большую часть этого, но команда cnt
сбила меня с толку. Что означает cnt
и как оно используется для решения уравнения?
t=0; %% Initial estimate of t
idx=1; %% Number of iterations
iter=zeros(1,100); %% Array to store sequence of t values
func=zeros(1,100); %% Array to store sequence of f(t) values
eps=1e-6; %% Required accuracy
n_max=100; %% Number of iterations
f=1; %% f contains f(t)
while abs(f)>eps && idx<n_max
f=-5-exp(-t)+cos(.3*pi*t)+(.1*t*t);
dfdt=exp(-t)-(0.3*pi*sin(0.3*pi*t))+(0.2*t);
iter(idx)=t;
func(idx)=f;
idx=idx+1;
t=t-f/dfdt;
end
cnt=idx-1;
t=linspace(0,10,100);
f=zeros(1,100);
for idx=1:100
f(idx)=-5-exp(-t(idx))+cos(.3*pi*t(idx))+(.1*t(idx)*t(idx));
end
figure, hold on;
grid on;
plot(t,f);
scatter(iter(1:cnt),func(1:cnt),'r');
title('Newton-Raphson Example','FontSize', 12)
legend('f(t)','Iterations', 'NorthWest');
xlabel('t', 'FontSize', 12);
ylabel('f(t)','FontSize', 12);