fprintf with complex numbers and ordinary arrays
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
%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 Kommentare
Jan
am 19 Nov. 2022
Bearbeitet: Jan
am 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?
Akzeptierte Antwort
Jan
am 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 Kommentare
Walter Roberson
am 20 Nov. 2022
Bearbeitet: Walter Roberson
am 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
am 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)
compose('%d %d\n', x, y)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!