필터 지우기
필터 지우기

how to tabulate complex numbers using fprintf?

조회 수: 4 (최근 30일)
Otto
Otto 2012년 11월 11일
Hi All,
I have some results in complex form. I want to tabulate these results by using fprintf but I just get the real parts of numbers on my command window. Here is my code:
g=@(x)x^4-(0.4982*x^3)+(2.7464*x^2)-(1.1146*x)+(2.6699)
dgdx=@(x)(4*x^3)+(1.4946*x^2)+(5.4928*x)+(1.1146)
x0=1+i
x=x0
it_no=0
fprintf('\n\n%18s%18s%18s\n','it_no','x','g(x)');
while 1
it_no=it_no+1;
xprev=x;
x=xprev-(g(x)./dgdx(x));
if abs(g(x))<10^-4;
break
end
fprintf(' %17.0f\t %17.7f\t %17.7f\t\n',[it_no,x,g(x)]);
end
fprintf(' %17.0f\t %17.7f\t %17.7f\t\n',[it_no,x,g(x)])
How can I tabulate both real and imaginary parts of complex numbers using fprintf? Anyone to help me for this problem?
I'll appreciate for any help.
Thanks Already!

채택된 답변

Star Strider
Star Strider 2012년 11월 11일
편집: Star Strider 2012년 11월 11일
You have to separate the real and imaginary parts and print them individually. See if replacing your fprintf statements with this one produces the results you want:
fprintf(' %17.0f\t %17.7f %11.7f*i\t %17.7f %11.7f*i\t\n',[it_no,real(x),imag(x),real(g(x)),imag(g(x))])
  댓글 수: 2
Otto
Otto 2012년 11월 11일
Thank you very much for your interest. I have a last question: Is it possible to use "+" sign between real and imaginary parts or do i have to display results in the format that you suggested?
Star Strider
Star Strider 2012년 11월 11일
편집: Star Strider 2012년 11월 11일
My pleasure!
To get a ‘+’ sign, insert a ‘+’ right after the ‘%’ in the numeric format specifiers. This forces fprintf (and all other functions that use this sort of format) to print the sign. Use this for your fprintf statements to do that:
fprintf(' %17.0f\t %17.7f %+11.7f*i\t %17.7f %+11.7f*i\t\n',[it_no,real(x),imag(x),real(g(x)),imag(g(x))])

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by