Stop the iteration loop at desire Ea
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 --------------------------------------
0 Kommentare
Antworten (2)
Alan Stevens
am 9 Nov. 2020
Bearbeitet: Alan Stevens
am 9 Nov. 2020
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 --------------------------------------
0 Kommentare
Mathieu NOE
am 9 Nov. 2020
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!