fprintf with complex numbers and ordinary arrays
이전 댓글 표시
%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
"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
2022년 11월 19일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Univariate Discrete Distributions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!