Sie sind auf Seite 1von 5

LoopsAddendum

Inclassdiscussionofprogressiveimprovementofasimplerecursivealgorithmfor
theestimationofsquarerootswithemphasison:
Passingargumentsintofunctions
Useofthenarginfunction
Beneficialuseofthewhileloop
Improvingtheefficiencyofalgorithmsbycontrollingthetightnessofconvergence
factorsandbytheiterationcount

Thefollowingfunctionfileswereusedininthelastlecturetoillustratethe
progressiveimprovementofabasicalgorithmforestimatingsquareroots.These
MATLABfunctionfileswillalsobeuploadedonD2Lforeaseofaccessand
practicewiththem.
Foreaseofnavigatingthefunctionfile,Ihaveretainedthescriptsfromthefirst
basiccodetothethirdmodificationbutcommentedthemout.

Noticetheuseoffprintftooutputtitles(paceholders)andalsonumericalvalues
oftargetvariables.

1|P a g e

Hereisthestartingfunction,withonlyoneinputathenumberwhosesquare
rootwewishtoapproximate.

function NewtonSqrt(a)
%Sample programming for determining square root in a
repetition structure
%use long format to see convergence trends more easily
%
%Synopsis: Call the function NewtonSqrt and pass a
numerical value 'a'
%whose square root we need to approximate
format long
x=a/2;
disp(['The approach to sqrt(a) for a=',num2str(a)]);
for i=1:6
x=(x+a/x)/2;
disp(x)
end
disp('Matlab''s value: ')
disp(sqrt(a))

2|P a g e

Thisisthefirstmodificationinthecasestudyseries.
Replacetheforloopwiththewhileloop.Thishasmoreflexibilitywiththe
determinationofterminationconditionsfortheestimationofthesquareroot.

function NewtonSqrtCS(a)
%Sample programming for determining square root in a
repetition structure
%use long format to see convergence trends more easily
%
%Synopsis: Call the function NewtonSqrt and pass a
numerical value 'a'
%whose square root we need to approximate
format long
x=a/2;
disp(['The approach to sqrt(a) for a=',num2str(a)]);
i=0;
while i<7
x=(x+a/x)/2;
disp(x)
i=i+1
end
disp('Matlab''s value: ')
disp(sqrt(a))

3|P a g e

Thenextmodificationaddsthenumberofargumentstobepassedintoinclude
a,theconvergencetoleranceleveldeltaandthemaximumallowablenumber
ofiterationsmaxit.
Noticethedefinitionofdefaultvaluesofarguments2and3

function NewtonSqrtCS2(a,delta,maxit)
%The simple square root function with added
functionality
%
%Synopsis: Call the function NewtonSqrt and pass a
numerical value 'a'
%whose square root we need to approximate. Pass also
delta and maxit for
%convergence control
if nargin<2,delta=5E-6;end
if nargin<3, maxit=5; end
format long
x=a/2;
%Use fprintf command as a place holder
fprintf('\n the estimate for the square root of a is
\n')
%disp(['The approach to sqrt(a) for a=',num2str(a)]);
i=0;
while i<7
x=(x+a/x)/2;
disp(x)
i=i+1;
end
%disp('Matlab''s value: ')
fprintf('Matlab''s value is: ')
%disp(sqrt(a))
fprintf('%12.6f \n', x)

4|P a g e

Thisnextmodificationmakessomechangesinthevariablewhosesquarerootwe
seekfromatox,Thisalsochangestheconvergencecheckcriteriontoonethat
usesdeltaandtherelativeconvergenceparameterwhichtracksthemagnitude
oftheabsolutevalueofthecurrentsquarerootestimatertotheprevious
squarerootestimaterold
function NewtonSqrtCS3(x,delta,maxit)
%The simple square root function with added
functionality
%
%Synopsis: Call the function NewtonSqrt and pass a
numerical value 'a'
%whose square root we need to approximate. Pass also
delta and maxit for
%convergence control
if nargin<2,delta=5E-6;end
if nargin<3, maxit=5; end
r=x/2;
rold=x;
%Use fprintf command as a place holder
fprintf('\n the estimate for the square root of x is
\n')
%disp(['The approach to sqrt(a) for a=',num2str(a)]);
%i=0;
while abs((r-rold)/rold)>delta
%while i<6
rold=r; %Save old value of r for next convergence
r=0.5*(rold+x/rold);
disp(r)
%i=i+1;
end
fprintf('%14.6f \n', r)
%disp('Matlab''s value: ')
fprintf('Matlab''s value is: ')
disp(sqrt(x))

5|P a g e

Das könnte Ihnen auch gefallen