How can I make a loop that gives values in each iteration?

조회 수: 1 (최근 30일)
Kownain Shahzad
Kownain Shahzad 2021년 2월 24일
답변: Alan Stevens 2021년 2월 24일
In my code, I can only get final values for Fe (forward error) and Be (backward error). How can I make a loop that gives me values for both errors in each iteration (line 20 to line 23 needs correction), there are total 7 iterations.
clear;
clc;
format long e;
%constants
m = 2000;
k = 500E3;
c = 38E3;
x_y = 0.2;
w = 0.1:0.01:100;
f = @(w)sqrt((m.*c.*w.^3)./(k.*(k-m.*w.^2)+(c.*w).^2))-(x_y);
options = optimset('tolX',2.2e-16);
[r] = fzero(f,[2 8],options); % exact value
options = optimset('display','iter','tolX',1e-10);
[xc] = fzero(f,[2 8],options);
for i = 1:length(w)
Fe(i) = abs(f(xc(i)));
Be(i) = abs(r-xc(i));
end
fprintf('\nxc = %f\t\n',xc);
disp('Forward error = ')
disp(Fe);
disp('Backward error = ')
disp(Be);

답변 (1개)

Alan Stevens
Alan Stevens 2021년 2월 24일
Why do you need a loop? There is just one zero between 2 and 8. Your values of w are not used in any meaningful way (they would be if you were to plot f(w) vs w). You can just replace your loop with
Fe = abs(f(xc));
Be = abs(r-xc);

카테고리

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

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by