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

6 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2014-2-13
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.
  3 个评论
Shameer Parmar
Shameer Parmar 2014-12-8
Hello Oladunjoye,
Can you please tell me how you get to achieve the value upto 20 decimal places? because the same concept I am using in my scripting but I am getting the values upto 16 decimal places only and after 16, it is rounding that values to zeroooooo
Thanks for your help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by