how can I set the decimal digits of all coefficients at one time when outputting a polynomial f?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
marvellous
am 27 Okt. 2023
Kommentiert: marvellous
am 27 Okt. 2023
In Windows 10, MATLAB R2018a, I try to use fprintf to output a polynomial f, for example, 5.327416*x^2+3.660092*x+1.5799301. How can I set the decimal digits(e.g. 3) of all coefficients at one time instead of setting them one by one? That is, I want the result to be "5.327*x^2+3.660*x+1.580". Anyone can help me? Thanks!
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 27 Okt. 2023
P = [5.327416, -3.660092, 1.5799301]
%method 1
vpa(poly2sym(P, sym('x')),4)
%method 2
poly2sym(round(sym(P),3))
%method 3
fmt = [repmat('%.3f*x^%d + ', 1, length(P)-1), '%.3f\n'];
temp = reshape([P; length(P)-1:-1:0], 1, []);
fprintf(fmt, temp(1:end-1));
If you want the + -3.660 to instead show up as - 3.660 then it takes more work.
3 Kommentare
Dyuman Joshi
am 27 Okt. 2023
Bearbeitet: Dyuman Joshi
am 27 Okt. 2023
P = [5.327416, -3.660092, 1.5799301]
y = vpa(poly2sym(P, sym('x')),4);
disp(y)
z = char(y);
fprintf('%s', z)
Weitere Antworten (1)
Sam Chak
am 27 Okt. 2023
a = 5.327416;
b = 3.660092;
c = 1.5799301;
fprintf('f(x) = %.3f*x^2 + %.3f*x + %.3f', a, b, c)
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!