Friends, I am posting all relevant functions one by one after adding comments.

One by one I will post all functions but there is no guarantee that everything would work properly though I will explain all logic so friends could understand and change anything they wish. I will try to carefully read my posts and code to make sure that there is no change in anything I have posted.

Here is the relevant function that inverts a univariate Z-series to find a value of Z corresponding to the input value of univariate stochastic variable. I was earlier using Newton inversion but I continued to face some problems with Newton inversion so I shifted the logic of function from Newton root-finding to bisection which was extremely reliable but slightly slow. You can increase the speed of this function by playing with it.

Here is the logic of bisection algorithm.

%This program takes a value of Y as input and returns the corresponding

value of Z given the parameters of the Z-series.
where
[$] Y(Z)= \, c_0 \, + c_1 \ Z \,+ c_2 \, Z^2 \,+ c_3 \, Z^3 \,+ c_4 \, Z^4 \,+ c_5 \, Z^5 \,+ c_6 \, Z^6 \,+ c_7 \, Z^7 \,[$]
It first sees if the value of Y is greater than median c0 or smaller than

median c0. If the value of Y is greater than median c0, Z has to be greater than zero and if the value of Y is smaller than median c0, Z has to be negative.
Once we know whether Z is greater than Z or smaller than Zero, we divide

the program into two repeated if loops, one for Z greater than zero and
one for Z smaller than zero.

If Z is positive, we have to assign it a valid band of upper and

lower values between which it can be found.
This is done sequentially by calculating Y(.5) at Z=.5, if Y is smaller

than Y(.5), we know that true value of Y will lie between 0 and .5 so we assign a lower band value Za=0 and upper band value Zb=.5
If however Y is greater than Y(.5), we know that true Z should be larger

than .5 and we again evaluate Y(1) at Z=1, If Y is smaller than Y(1), we know that true value of Z should be between .5 and 1, and we assign a lower band value Za=.5 and upper band value of Zb=1. Through repeated
elseif we continue until we find an appropriate band value of Z.

Once we have determined the upper band value of Zb and lower band value of

Za, We find Zmid=(Za+Zb)/2. We then evaluate Y(Zmid) and if Y < Y(Zmid) we know that true value of Z should be between Za and Zmid so we move the upper band to Zb=Zmid while keeping Za at older value. However if Y> Y(Zmid), we know that true value of Z should be between Zmid and Zb so we assign the lower band value equal to Zmid as Za=Zmid while keeping Zb at its old value. In a few iterations, this bisection procedure gets extremely close to true value of Z for which Y(Z)=Y
```
function [Z] = CalculateZgivenXAndZSeriesC7(X,c0,c)
Z(1:length(X))=0;
for nn=1:length(X)
Z(nn)=CalculateZgivenXAndZSeriesBisection2C7(Y(nn),c0,c);
end
end
```

.
.
.
.
```
function [X] = EvaluateZSeriesC7(c0,c,Z)
X=c0+c(1)*Z+c(2)*Z.^2+c(3)*Z.^3+c(4)*Z.^4+c(5)*Z.^5+c(6)*Z.^6+c(7)*Z.^7;
end
```

.

.

```
function [Z]=CalculateZgivenXAndZSeriesBisection2C7(Y,c0,c)
%This program takes a value of Y as input and returns the corresponding
%value of Z given the parameters of the Z-series.
%It first see if the value of Y is greater than median or smaller than
%median. If the value of Y is greater than median Z has to be greater than
%zero and if the value of Y is smaller than median Z has to be negative.
% Once we know whether Z is greater than Z or smaller than Zero, we divide
% the program into two repeated if loops, one for Z greater than zero and
%one for Z smaller than zero.
%If Z is positive, we have to assign it a valid band of upper and
%lower values between which it can be found.
%This is done sequentially by calculating Y(.5) at Z=.5, if Y is smaller
%than Y(.5), we know that true value of Y will lie between 0 and .5
%so we assign a lower band value Za=0 and upper band value Zb=.5
%If however Y is greater than Y(.5), we know that true Z should be larger
%than .5 and we again evaluate Y(1) at Z=1, If Y is smaller than Y(1),
%we know that true value of Z should be between .5 and 1, and we assign a
%lower band value Za=.5 and upper band value of Zb=1. Through repeated
%elseif we continue until we find an appropriate band value of Z.
%Once we have determined the upper band value of Zb and lower band value of
%Za, We find Zmid=(Za+Zb)/2. We then evaluate Y(Zmid) and if Y < Y(Zmid) we
%move the upper band to Zb=Zmid while keeping Za at older value. However if
% Y> Y(Zmid), we assign the lower band value equal to Zmid as Za=Zmid. In a
% few iterations, this bisection procedure gets extremely close to true
% value of Z for which Y(Z)=Y
if(Y>c0)
%Z=.5;
if(Y< EvaluateZSeriesC7(c0,c,.5))
Za=0;
Zb=.5;
elseif(Y< EvaluateZSeriesC7(c0,c,1))
Za=.5;
Zb=1;
elseif(Y< EvaluateZSeriesC7(c0,c,1.5))
Za=1;
Zb=1.5;
elseif(Y< EvaluateZSeriesC7(c0,c,2))
Za=1.5;
Zb=2;
elseif(Y< EvaluateZSeriesC7(c0,c,2.5))
Za=2;
Zb=2.5;
elseif(Y< EvaluateZSeriesC7(c0,c,3))
Za=2.5;
Zb=3;
elseif(Y< EvaluateZSeriesC7(c0,c,3.5))
Za=3;
Zb=3.5;
elseif(Y< EvaluateZSeriesC7(c0,c,4))
Za=3.5;
Zb=4;
elseif(Y< EvaluateZSeriesC7(c0,c,4.5))
Za=4;
Zb=4.5;
elseif(Y< EvaluateZSeriesC7(c0,c,5))
Za=4.5;
Zb=5;
elseif(Y< EvaluateZSeriesC7(c0,c,5.5))
Za=5;
Zb=5.5;
elseif(Y< EvaluateZSeriesC7(c0,c,6))
Za=5.5;
Zb=6;
else
Za=6;
Zb=12;
end
for mm=1:35
Zmid=.5*Za+.5*Zb;
if(Y< EvaluateZSeriesC7(c0,c,Zmid))
Zb=Zmid;
elseif(Y> EvaluateZSeriesC7(c0,c,Zmid))
Za=Zmid;
end
Z=.5*Za+.5*Zb; %This can be taken out of the loop and does not
%necessarily have to be calculated again and again. Has to be
%calculated at least once after the loop to determine the true value of
%Z which is the final result of the function.
end
end
if(Y<c0)
if(Y> EvaluateZSeriesC7(c0,c,-.5))
Za=-.5;
Zb=0;
elseif(Y> EvaluateZSeriesC7(c0,c,-1))
Za=-1;
Zb=-.5;
elseif(Y> EvaluateZSeriesC7(c0,c,-1.5))
Za=-1.5;
Zb=-1;
elseif(Y> EvaluateZSeriesC7(c0,c,-2))
Za=-2;
Zb=-1.5;
elseif(Y> EvaluateZSeriesC7(c0,c,-2.5))
Za=-2.5;
Zb=-2.0;
elseif(Y> EvaluateZSeriesC7(c0,c,-3))
Za=-3;
Zb=-2.5;
elseif(Y> EvaluateZSeriesC7(c0,c,-3.5))
Za=-3.5;
Zb=-3.0;
elseif(Y> EvaluateZSeriesC7(c0,c,-4))
Za=-4;
Zb=-3.5;
elseif(Y> EvaluateZSeriesC7(c0,c,-4.5))
Za=-4.5;
Zb=-4.0;
elseif(Y> EvaluateZSeriesC7(c0,c,-5))
Za=-4.5;
Zb=-5.0;
elseif(Y> EvaluateZSeriesC7(c0,c,-5.5))
Za=-5.0;
Zb=-5.5;
elseif(Y> EvaluateZSeriesC7(c0,c,-6.0))
Za=-5.5;
Zb=-6.0;
else
Za=-12.0;
Zb=-6.0;
end
for mm=1:35
Zmid=.5*Za+.5*Zb;
if(Y< EvaluateZSeriesC7(c0,c,Zmid))
Zb=Zmid;
elseif(Y> EvaluateZSeriesC7(c0,c,Zmid))
Za=Zmid;
end
Z=.5*Za+.5*Zb; %This can be taken out of the loop and does not
%necessarily have to be calculated again and again. Has to be
%calculated at least once after the loop to determine the true value of
%Z which is the final result of the function.
end
end
end
```

You think life is a secret, Life is only love of flying, It has seen many ups and downs, But it likes travel more than the destination. Allama Iqbal