필터 지우기
필터 지우기

fprintf with complex numbers and ordinary arrays

조회 수: 14 (최근 30일)
Ali Kiral
Ali Kiral 2022년 11월 19일
댓글: Jan 2022년 11월 20일
%Enter coefficients in a descending order
N=input('Enter the degree of the main polynomial ');
for k=1:N+1
Pol1(k)=input('Enter a coefficient ');
end
u=input('Enter the first estimate ');
v=input('Enter the second estimate ');
w=input('Enter the third estimate ');
k=input('Enter accuracy ');
n=1;
delta0=(polyval(Pol1, v)-polyval(Pol1, u))/(v-u);
delta1=(polyval(Pol1, w)-polyval(Pol1, v))/(w-v);
h0=v-u;
h1=w-v;
a=(delta1-delta0)/(h1+h0);
b=a*h1+delta1;
c=polyval(Pol1, w);
if abs(b+sqrt(b^2-4*a*c))>=abs(b-sqrt(b^2-4*a*c))
g1=b+sqrt(b^2-4*a*c);
end
if abs(b+sqrt(b^2-4*a*c))<abs(b-sqrt(b^2-4*a*c))
g1=b-sqrt(b^2-4*a*c);
end
x(n)=w-2*c/g1;
u=v;
v=w;
w=x(n);
if abs(polyval(Pol1,x(n)))<10^-k
end
while abs(polyval(Pol1,x(n)))>=10^-k
n=n+1;
delta0=(polyval(Pol1, v)-polyval(Pol1, u))/(v-u);
delta1=(polyval(Pol1, w)-polyval(Pol1, v))/(w-v);
h0=v-u;
h1=w-v;
a=(delta1-delta0)/(h1+h0);
b=a*h1+delta1;
c=polyval(Pol1, w);
if abs(b+sqrt(b^2-4*a*c))>=abs(b-sqrt(b^2-4*a*c))
g1=b+sqrt(b^2-4*a*c);
end
if abs(b+sqrt(b^2-4*a*c))<abs(b-sqrt(b^2-4*a*c))
g1=b-sqrt(b^2-4*a*c);
end
x(n)=w-2*c/g1;
u=v;
v=w;
w=x(n);
end
iteration_number=(1:n);
for m=1:n
p_xi(m)=polyval(Pol1,x(m));
end
fprintf(' iteration number root estimates \n')
fprintf('%10.0f\n ',iteration_number)
fprintf( '%41.16f%+.16fi\n', real(x), imag(x))
Above code finds one of the roots (it is complex) of the polynomial x^4-6x^2-3x+. When i run the code i get
I am asking for two things: Push that iteration number '1' a little bit to the right (how do I do that by changing the inside of fprintf) so as to align it vertically with the remaining numbers and carry root estimates upwards (by changing the inside of fprintf commands again) to make them horizontally level them with iteration numbers.
  댓글 수: 2
Jan
Jan 2022년 11월 19일
편집: Jan 2022년 11월 19일
"a little bit to the right*" - what does this mean? You can simply edit the question to insert a change.
Please edit your question, select the code an press the icon to format it. This increases the readability of the code. A standard indentation of the code would be useful also.
This is not clear also: "p(xi) and |p(xi)| are going to be taken care of later."
And: " I need a little help here, because when i run the code i get " - you get what?
" Push that iteration number '1' a little bit to the left" - what do you call "that iteration number?
Ali Kiral
Ali Kiral 2022년 11월 19일
Hey Jan, sorry for an unorganized question. I have edited it, it is better now.

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

채택된 답변

Jan
Jan 2022년 11월 19일
This does not work without a loop.
fprintf(' iteration number root estimates\n');
for k = 1:n
fprintf('%10.0f%41.16f %+.16fi\n', k, real(x(k)), imag(x(k)));
end
To shift the numbers to the right or left, modify the values, which define the width of the output. See:
doc fprintf
It is: %<width>.<precision>f
  댓글 수: 3
Walter Roberson
Walter Roberson 2022년 11월 20일
편집: Walter Roberson 2022년 11월 20일
You can do it without a loop:
fprintf('%s\n', compose("%10.0f%41.16f %+.16fi", (1:n).', real(x(:)), imag(x(:))))
Jan
Jan 2022년 11월 20일
@Walter Roberson: Thanks, this is useful. compose processes the inputs row-wise over the arguments, while sprintf uses the inputs elementwise:
x = (1:2).';
y = (3:4).';
sprintf('%d %d\n', x, y)
ans =
'1 2 3 4 '
compose('%d %d\n', x, y)
ans = 2×1 cell array
{'1 3↵'} {'2 4↵'}

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by