MATLAB: How to increase the number of decimals of the values stored in a matrix

decimal placesminimum of a matrix

Hi everyone. I wish to rephrase the question I asked last week.
I used the code below to obtain the minimum solutions of a function F and the corresponding values of F. The solution x is a five component vector i.e. x = [x1;x2;x3;x4;x5] and fvals the corresponding minimum of F at each solution. Next I stored x and fvals respectively as the first and second column of a matrix M and lastly I stored the lowest minimum at each j and the corresponding solution in a matrix P.
This is my code
for j = 1:1:numz
z=zvec(j);
% using all possible initial values by iterating over x0
for k=0:1:3
for l=0:1:3
for m=0:1:3
for n=0:1:3
for p=0:1:3
x0=[k*pi/3;l*pi/3;m*pi/3;n*pi/3;p*pi/3];
% calling the function
[x,fvals]=fmincon(@(x) F(x,z))...
,x0, [], [], [], [], -pi*ones(5,1), pi*ones(5,1));
% storing the results as a matrix
M(position, 1)= {x};
M(position, 2) = {fvals)};
position = position+1;
end
end
end
end
end
%picking the lowest minimum for each j
[~, ind] = min([M{:,2}]);
P(j,:) = M(ind,:);
end
I have two problem:
1) I want fvals to be stored in M upto twenty (20) decimal place instead of the default four(4) decimal places. I have tried
format long , longG, fprint('%0.20f', fvals) but none worked.
2) In the last part I tried to pick the lowest value of F for each value of j and its corresponding x and store them in a matrix P but the system is degenerate i.e. many x give the same solution or fvals. The code I wrote returns the first minimum for each j but I wish to pick another minimum for which x5 > 0 and x3 >0.
How do I solve this two problems?

Best Answer

MATLAB cannot calculate numerically to more than 53 bits of precision (relative error of about 1E-16)
You can use the Symbolic Toolbox if you have it, or you could attempt to use one of John D'Errico's variable precision packages from the File Exchange.