Stop the iteration loop at desire Ea

2 ビュー (過去 30 日間)
Siti Salmiah
Siti Salmiah 2020 年 11 月 9 日
回答済み: Mathieu NOE 2020 年 11 月 9 日
I need help with my fixed point MATLAB coding. My iteration seems does not stop at my desired Ea. I want the iteration to stop at Ea<5 but the iteration will always stop at the max number of iteration, N. I have try to use the "break else return" but than it not show any of the iteration. Below are my MATLAB code:
function FixedPoint
clear
clc
cinitial = input('\nGive the initial value of c : '); % Inputs initial value of x
iterations = 0;
N = 100; % max no. iterations.
fprintf('\n\nIteration c Ea(%)')
%-------------------- Runs Fixed Point algorithm --------------------------
while iterations < N
c = f(cinitial);
Ea = abs((c-cinitial)/c)*100; % Calculates error.
iterations = iterations + 1; % Calculates iteration no.
cinitial = c; % Replaces old value with new one.
if abs((c-cinitial)/c)*100 <= 5
fprintf('\n%5.0f%16.8f %f', iterations,c,Ea )
end
if iterations > 1000
Ea <= 5;
disp('No Convergence (Ignore answer below)') % Halts algorithm after 10000
end % iterations if convergence is
end % not achieved.
fprintf('\nSolution is %f \n', double(c))
end
%-------------------- Ends algorithm --------------------------------------
function F = f(c)
F = ((9.81 * 82).* (1-exp(-0.04878.*c)))/36; % defines function
end
%-------------------- End of program --------------------------------------

回答 (2 件)

Alan Stevens
Alan Stevens 2020 年 11 月 9 日
編集済み: Alan Stevens 2020 年 11 月 9 日
Like so
%cinitial = input('\nGive the initial value of c : '); % Inputs initial value of x
cinitial = 1;
iterations = 0;
N = 100; % max no. iterations.
% This will need to be much larger if you tighten
% the convergence tolerance
Ea = 1;
fprintf('\n\nIteration c Ea(%)')
%-------------------- Runs Fixed Point algorithm --------------------------
while iterations < N && Ea > 10^-2 % You need an error test
c = f(cinitial);
Ea = abs((c-cinitial)/c)*100; % Calculates error.
iterations = iterations + 1; % Calculates iteration no.
if Ea <= 5
fprintf('\n%5.0f%16.8f %f', iterations,c,Ea )
end
cinitial = c; % Replaces old value with new one.
if iterations > N
Ea <= 5;
disp('No Convergence (Ignore answer below)') % Halts algorithm after N
end % iterations if convergence is
end % not achieved.
fprintf('\nSolution is %f \n', double(c))
%-------------------- Ends algorithm --------------------------------------
function F = f(c)
F = ((9.81 * 82).* (1-exp(-0.04878.*c)))/36; % defines function
end
%-------------------- End of program --------------------------------------

Mathieu NOE
Mathieu NOE 2020 年 11 月 9 日
hello
I believe it is working better now
I tested it even with much tighter error constraint (down to 0.1)
I'll let you check
all the best
clear
clc
c_initial = input('\nGive the initial value of c : '); % Inputs initial value of x
iterations = 0;
N = 1000; % max no. iterations.
fprintf('\n\nIteration c Ea(%)')
%-------------------- Runs Fixed Point algorithm --------------------------
c_old = c_initial;
while iterations < N
c = f(c_old);
Ea = abs((c-c_old)/c)*100; % Calculates error.
iterations = iterations + 1; % Calculates iteration no.
c_old = c; % Replaces old value with new one.
if Ea <= 0.1
fprintf('\n%5.0f%16.8f %f', iterations,c,Ea )
return
end
if iterations > 1000
Ea <= 5;
disp('No Convergence (Ignore answer below)') % Halts algorithm after 10000
end % iterations if convergence is
end % not achieved.
fprintf('\nSolution is %f \n', double(c))
% end
%-------------------- Ends algorithm --------------------------------------
function F = f(c)
F = ((9.81 * 82).* (1-exp(-0.04878.*c)))/36; % defines function
end

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by