How to increase the number of decimals of the values stored in a matrix
조회 수: 5 (최근 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?
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!