Why does it skips the if part?

Dear colleagues,
Need some help with the if statement. It appears that for some reason, Matlab ignores the if statment as if it was not there. It does not stop until k reachs 31 and not when G(1,i)=G(1,i-1), it does not display neither k variable, G(1,i) and G(1,i-1). Why and how can I fix it?
The code is:
a(1,1)=1;b(1,1)=2;e=1.5;
f=@(x) log(2*x+1)
k=1;i=1;
g = feval(f,e);
G=[];G(1,1)=g;
while k<=30
g = feval(f,g);
i=i+1;
G(1,i)=g;
k=k+1;
if G(1,i)==G(1,i-1)
disp(k)
G(1,i)
G(1,i-1)
end
end

Answers (1)

MATLAB doesn't ignore the if statement; the condition is never true.
You can print the values of G(1,i) and G(1,i-1) with more precision than default to see that they are never equal (at least, not while k<=30):
a(1,1)=1;
b(1,1)=2;
e=1.5;
f=@(x) log(2*x+1)
f = function_handle with value:
@(x)log(2*x+1)
k=1;
i=1;
g = feval(f,e);
G=[];
G(1,1)=g;
while k<=30
g = feval(f,g);
i=i+1;
G(1,i)=g;
k=k+1;
fprintf('k = %d\n',k);
fprintf('G(1,i) = %.99f\n',G(1,i));
fprintf('G(1,i-1) = %.99f\n',G(1,i-1));
if G(1,i)==G(1,i-1)
disp(k)
G(1,i)
G(1,i-1)
end
end
k = 2
G(1,i) = 1.327761429538331094946101984533015638589859008789062500000000000000000000000000000000000000000000000
G(1,i-1) = 1.386294361119890572453527965990360826253890991210937500000000000000000000000000000000000000000000000
k = 3
G(1,i) = 1.296239136069572861487131376634351909160614013671875000000000000000000000000000000000000000000000000
G(1,i-1) = 1.327761429538331094946101984533015638589859008789062500000000000000000000000000000000000000000000000
k = 4
G(1,i) = 1.278842290835604966758864975417964160442352294921875000000000000000000000000000000000000000000000000
G(1,i-1) = 1.296239136069572861487131376634351909160614013671875000000000000000000000000000000000000000000000000
k = 5
G(1,i) = 1.269109934856176735706867475528270006179809570312500000000000000000000000000000000000000000000000000
G(1,i-1) = 1.278842290835604966758864975417964160442352294921875000000000000000000000000000000000000000000000000
k = 6
G(1,i) = 1.263623739005177881722374877426773309707641601562500000000000000000000000000000000000000000000000000
G(1,i-1) = 1.269109934856176735706867475528270006179809570312500000000000000000000000000000000000000000000000000
k = 7
G(1,i) = 1.260517815509203387591696809977293014526367187500000000000000000000000000000000000000000000000000000
G(1,i-1) = 1.263623739005177881722374877426773309707641601562500000000000000000000000000000000000000000000000000
k = 8
G(1,i) = 1.258755159695245096074245338968466967344284057617187500000000000000000000000000000000000000000000000
G(1,i-1) = 1.260517815509203387591696809977293014526367187500000000000000000000000000000000000000000000000000000
k = 9
G(1,i) = 1.257753443730743381223646792932413518428802490234375000000000000000000000000000000000000000000000000
G(1,i-1) = 1.258755159695245096074245338968466967344284057617187500000000000000000000000000000000000000000000000
k = 10
G(1,i) = 1.257183721824975286551762110320851206779479980468750000000000000000000000000000000000000000000000000
G(1,i-1) = 1.257753443730743381223646792932413518428802490234375000000000000000000000000000000000000000000000000
k = 11
G(1,i) = 1.256859549935736453107892884872853755950927734375000000000000000000000000000000000000000000000000000
G(1,i-1) = 1.257183721824975286551762110320851206779479980468750000000000000000000000000000000000000000000000000
k = 12
G(1,i) = 1.256675049139879041248946123232599347829818725585937500000000000000000000000000000000000000000000000
G(1,i-1) = 1.256859549935736453107892884872853755950927734375000000000000000000000000000000000000000000000000000
k = 13
G(1,i) = 1.256570026240104276382680836832150816917419433593750000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256675049139879041248946123232599347829818725585937500000000000000000000000000000000000000000000000
k = 14
G(1,i) = 1.256510239406249729299247519520577043294906616210937500000000000000000000000000000000000000000000000
G(1,i-1) = 1.256570026240104276382680836832150816917419433593750000000000000000000000000000000000000000000000000
k = 15
G(1,i) = 1.256476202703789457615357605391182005405426025390625000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256510239406249729299247519520577043294906616210937500000000000000000000000000000000000000000000000
k = 16
G(1,i) = 1.256456825058573922859750382485799491405487060546875000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256476202703789457615357605391182005405426025390625000000000000000000000000000000000000000000000000
k = 17
G(1,i) = 1.256445792883999157396601731306873261928558349609375000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256456825058573922859750382485799491405487060546875000000000000000000000000000000000000000000000000
k = 18
G(1,i) = 1.256439511938709951976989032118581235408782958984375000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256445792883999157396601731306873261928558349609375000000000000000000000000000000000000000000000000
k = 19
G(1,i) = 1.256435935992019858886692418309394270181655883789062500000000000000000000000000000000000000000000000
G(1,i-1) = 1.256439511938709951976989032118581235408782958984375000000000000000000000000000000000000000000000000
k = 20
G(1,i) = 1.256433900083407051440076429571490734815597534179687500000000000000000000000000000000000000000000000
G(1,i-1) = 1.256435935992019858886692418309394270181655883789062500000000000000000000000000000000000000000000000
k = 21
G(1,i) = 1.256432740969231076633150223642587661743164062500000000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256433900083407051440076429571490734815597534179687500000000000000000000000000000000000000000000000
k = 22
G(1,i) = 1.256432081044278392667479238298255950212478637695312500000000000000000000000000000000000000000000000
G(1,i-1) = 1.256432740969231076633150223642587661743164062500000000000000000000000000000000000000000000000000000
k = 23
G(1,i) = 1.256431705325321779298519686562940478324890136718750000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256432081044278392667479238298255950212478637695312500000000000000000000000000000000000000000000000
k = 24
G(1,i) = 1.256431491414974344067445599648635834455490112304687500000000000000000000000000000000000000000000000
G(1,i-1) = 1.256431705325321779298519686562940478324890136718750000000000000000000000000000000000000000000000000
k = 25
G(1,i) = 1.256431369628081107236994284903630614280700683593750000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256431491414974344067445599648635834455490112304687500000000000000000000000000000000000000000000000
k = 26
G(1,i) = 1.256431300290393959784296384896151721477508544921875000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256431369628081107236994284903630614280700683593750000000000000000000000000000000000000000000000000
k = 27
G(1,i) = 1.256431260813936301445892240735702216625213623046875000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256431300290393959784296384896151721477508544921875000000000000000000000000000000000000000000000000
k = 28
G(1,i) = 1.256431238338557854206101183081045746803283691406250000000000000000000000000000000000000000000000000
G(1,i-1) = 1.256431260813936301445892240735702216625213623046875000000000000000000000000000000000000000000000000
k = 29
G(1,i) = 1.256431225542509855230832727102097123861312866210937500000000000000000000000000000000000000000000000
G(1,i-1) = 1.256431238338557854206101183081045746803283691406250000000000000000000000000000000000000000000000000
k = 30
G(1,i) = 1.256431218257255677528405612974893301725387573242187500000000000000000000000000000000000000000000000
G(1,i-1) = 1.256431225542509855230832727102097123861312866210937500000000000000000000000000000000000000000000000
k = 31
G(1,i) = 1.256431214109496208308769382711034268140792846679687500000000000000000000000000000000000000000000000
G(1,i-1) = 1.256431218257255677528405612974893301725387573242187500000000000000000000000000000000000000000000000

Categories

Find more on Psychology in Help Center and File Exchange

Asked:

on 10 Apr 2024

Edited:

on 10 Apr 2024

Community Treasure Hunt

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

Start Hunting!