How to use "format long" in fmincon?

8 views (last 30 days)
I need Matlab give the answers x1 and x2 decimal format with 15 digits after the decimal point.
% Função objetivo: Função de Rosenbrock
objetivo=@(x) 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
% Estimativa inicial
x0 = [0.5,0];
% Imprime o objetivo inicial
disp(['Objetivo inicial:' num2str(objetivo(x0))])
% Restrições lineares
% Desigualdade ("<=")
A = [1,2]; % Matriz contendo os coeficientes da restrição de desigualdade
b = [1]; % Vetor contendo o valor a ser satisfeito da desigualdade
% Igualdade ("=")
Aeq = [2,1]; % Matriz contendo os coeficientes da restrição de igualdade
beq = [1]; % Vetor contendo o valor a ser satisfeito da igualdade
% Otimização com fmincon
x = fmincon(objetivo,x0,A,b,Aeq,beq);
% Imprime o objetivo final
disp(['Objetivo final:' num2str(objetivo(x))])
% Imprime solução
disp('Solução')
disp(['x1 = ' num2str(x(1))])
disp(['x2 = ' num2str(x(2))])

Accepted Answer

Steven Lord
Steven Lord on 1 Sep 2020
Specify the second input (either precision or formatSpec) in your calls to num2str as shown on its documentation page.
doc num2str
  2 Comments
Elizabeth Valente
Elizabeth Valente on 1 Sep 2020
Edited: Elizabeth Valente on 1 Sep 2020
Unrecognized function or variable 'precision'.
Error in rosenbrock (line 27)
disp(['x1 = ' num2str(x(1), precision)])
Sorry, I dind't understand.
Where I specify "format long"?
Steven Lord
Steven Lord on 1 Sep 2020
You don't. format will not affect what num2str returns nor how the text that it returns is displayed.
Consider this example:
disp(num2str(pi))
disp(num2str(pi, 2))
disp(num2str(pi, 12))
Adapt these to your needs. See the examples on the documentation page for more information about the formatSpec input argument if you think you need the additional flexibility it can provide.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!