fprintf with complex numbers and ordinary arrays
9 ビュー (過去 30 日間)
古いコメントを表示
%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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1200038/image.png)
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
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?
採用された回答
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
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
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)
compose('%d %d\n', x, y)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!