fprintf with complex numbers and ordinary arrays

18 vues (au cours des 30 derniers jours)
Ali Kiral
Ali Kiral le 19 Nov 2022
Commenté : Jan le 20 Nov 2022
%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 commentaires
Jan
Jan le 19 Nov 2022
Modifié(e) : Jan le 19 Nov 2022
"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 le 19 Nov 2022
Hey Jan, sorry for an unorganized question. I have edited it, it is better now.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 19 Nov 2022
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 commentaires
Walter Roberson
Walter Roberson le 20 Nov 2022
Modifié(e) : Walter Roberson le 20 Nov 2022
You can do it without a loop:
fprintf('%s\n', compose("%10.0f%41.16f %+.16fi", (1:n).', real(x(:)), imag(x(:))))
Jan
Jan le 20 Nov 2022
@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↵'}

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by