how to use fprintf in string command

조회 수: 2 (최근 30일)
Andika
Andika 2012년 7월 16일
Hello everyone, I want to ask a question about using fprintf in string command. I have arranged a code to calculate minimum value of a function using hooke jeeves method (optimization problem). Here is the code :
clear all clear clc
% HOOKE-JEEVES (for 2 variables):
rasio = 0.6; x = [1 7]; delx = [0.1 0.1]; tol = [0.001 0.001]; n=length(x); Fopt=fungsi(x(1),x(2)); xopt=x;F=Fopt; disp([xopt Fopt])
%checking delta
chekdel=strcat('if delx(1)<=tol(1)&delx(2)<=tol(2);','disp([xopt Fopt]);'... ,'disp(delx);','else;','del=delx*rasio;','delx=del;','eval(eksplorasi);'... ,'end;');
%repeating success step
ulsuk=strcat('while Fopt<F;','disp([xopt Fopt]);','for i=1:n;',... 'x(i)=xopt(i)+delx(i)*tanda(i);','end;','F=fungsi(x(1),x(2));',... 'if F<=Fopt;','xopt=x;', 'Fopt=F;','else;','break;','end;','end;',... 'eval(eksplorasi)');
%exploration:
eksplorasi=strcat('for ep=1:n;','tanda(ep)=0;','x(ep)=xopt(ep)+delx(ep);',... 'Fd=fungsi(x(1),x(2));','if Fd<=Fopt;','xopt(ep)=x(ep);','Fopt=Fd;','tanda(ep)=1;',... 'else;','x(ep)=xopt(ep)-delx(ep);','Fd=fungsi(x(1),x(2));','if Fd<=Fopt;',... 'xopt(ep)=x(ep);','Fopt=Fd;','tanda(ep)=-1;','end;','end;','end;','disp([xopt Fopt]);',... 'if abs(tanda(1))>0|abs(tanda(2))>0;','eval(ulsuk);','else;','eval(chekdel);',... 'end;'); eval(eksplorasi);
function f=fungsi(x1,x2) f=(x1-3)^2+((x2-5)^2)/4+7.5; end
The code written above works well. What i want here is that I want to use fprintf instead of disp. But, the code won't work when I replace the disp command with fprintf command. Anyone can help me solve my problem ?
Thank you
Andika
  댓글 수: 1
Jan
Jan 2012년 7월 16일
편집: Jan 2012년 7월 16일
Please follow the "About Matlab Answers" link to learn more about the forum standards, e.g. the formatting of code. Thanks.
I strongly recommend not to create complicated strings and evaluate them using the evil EVAL. This is a bad bad bad programming style and ugly to debug. It is much nicer, smarter, more elegant, faster and cleaner to write commands as commands. Then you could even add comments, which explain your code.
Note: I haven't seen any case, which really required EVAL, and all workarounds have been big advantages in any case.
Another hint: "clear all" removes all loaded functions from the memory, and reloading them wastes a lot of time without any benefit. Even a "clear variables", which removes existing variables only, is a brute act and really serious bugs should be cauhgt by smarter techniques. It is like removing and reinserting the battery from your clock before you check the time.

댓글을 달려면 로그인하십시오.

답변 (2개)

Julian
Julian 2012년 7월 16일
I guess you appreciate fprintf(1,f,x) can be used as a replacement for disp(x) but the format string fmt e.g. '%s\n' or '%d\n' must be chosen according type of x.
  댓글 수: 3
Andika
Andika 2012년 7월 16일
It didn't work, Julian.
Jan
Jan 2012년 7월 16일
@Julian: You can edit your answer instead of fixing a typo in a comment.

댓글을 달려면 로그인하십시오.


Caleb
Caleb 2012년 7월 16일
fprintf('%g',[xopt Fopt])

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by