MATLAB: Using a function to answer new questions

calling functionsif statement

I have created
function y=Gss(n);
a=3; b=0.2; N(1)=0.2;
for v=2:n+1
N(v)=exp(-a*N(v-1)^2)+b;
end
y=N(v);
How would I use this code to calculate something like
if abs(N(v-1)-N(v))<5
answer=v
else v=v+1 'till you get the desired v'
end
Do I create another script and call Gss(n) or how would I write it in the first function file. I think I would have to use a loop for the second code

Best Answer

function [N,answer] = Gss(n);
N=zeros(1,n+1);
answer=0;
a=3; b=0.2; N(1)=0.2;
for v=2:n+1
N(v)=exp(-a*N(v-1)^2)+b;
if abs(N(v-1)-N(v))<5
answer=v;
end
end
end
Or you could just wait to get array N back and then:
function N = Gss(n);
N=zeros(1,n+1);
a=3; b=0.2; N(1)=0.2;
for v=2:n+1
N(v)=exp(-a*N(v-1)^2)+b;
end
end
%once array N comes back
answer=find(abs(diff(N))<5)+1;%this provides multiple answers if multiple differences are <5