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 Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by