How to use "format long" in fmincon?
8 views (last 30 days)
Show older comments
Elizabeth Valente
on 1 Sep 2020
Commented: Steven Lord
on 1 Sep 2020
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))])
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!