scientific notation convertion of coefficients of a polynomial

syms x
f = - 0.042440155 + 0.29476113*x + 0.47380563*x^2 - 0.17632127*x^3 + 0.038426007*x^4 - 0.005090693*x^5 + ...
0.00073686606*x^6 - 0.00024549191*x^7 + 0.000072758471*x^8 - 0.000017260649*x^9 + 0.0000048409949*x^10 - ...
0.0000014708831*x^11 + 0.00000037777292*x^12 - 0.000000086757727*x^13 + 0.000000020031646*x^14 - ...
4.1039434e-9*x^15 + 5.354131e-10*x^16 + 3.6711725e-12*x^17 - 1.9835736e-11*x^18 + 4.9956502e-12*x^19 - ...
7.3416852e-13*x^20 + 7.2822196e-14*x^21 - 4.9740077e-15*x^22 + 2.2607386e-16*x^23 - ...
6.1866125e-18*x^24 + 7.7315661e-20*x^25;
%% I want Matlab to convert all the coefficients of f(x) like: 7.7315661 e-20 (one digit before decimal and the exponential form with base 10 or e) and give me a modified f(x)

3 Commenti

Is the purpose to get the data in that particular format just for display? Or is it something else?
Yes, it is required for a particular sense. Actually I got the code from this format 3 years back but forgotten. That comand solved my purpose which vpa couldn't. That's why it is needed.
syms x
f = - 0.042440155 + 0.29476113*x + 0.47380563*x^2 - 0.17632127*x^3 + 0.038426007*x^4 - 0.005090693*x^5 + ...
0.00073686606*x^6 - 0.00024549191*x^7 + 0.000072758471*x^8 - 0.000017260649*x^9 + 0.0000048409949*x^10 - ...
0.0000014708831*x^11 + 0.00000037777292*x^12 - 0.000000086757727*x^13 + 0.000000020031646*x^14 - ...
4.1039434e-9*x^15 + 5.354131e-10*x^16 + 3.6711725e-12*x^17 - 1.9835736e-11*x^18 + 4.9956502e-12*x^19 - ...
7.3416852e-13*x^20 + 7.2822196e-14*x^21 - 4.9740077e-15*x^22 + 2.2607386e-16*x^23 - ...
6.1866125e-18*x^24 + 7.7315661e-20*x^25;
vpa(f, 8)
ans = 

Accedi per commentare.

 Risposta accettata

FM = regexprep(char(vpa(f)),'([0-9]+\.[0-9]+)','${num2str(str2num($1),''%e'')}')
This is working.

5 Commenti

This command converts the polynomial as one single text string, I think it is not a polynomial expression anymore.
syms x
f = - 0.042440155 + 0.29476113*x + 0.47380563*x^2 - 0.17632127*x^3 + 0.038426007*x^4 - 0.005090693*x^5 + ...
0.00073686606*x^6 - 0.00024549191*x^7 + 0.000072758471*x^8 - 0.000017260649*x^9 + 0.0000048409949*x^10 - ...
0.0000014708831*x^11 + 0.00000037777292*x^12 - 0.000000086757727*x^13 + 0.000000020031646*x^14 - ...
4.1039434e-9*x^15 + 5.354131e-10*x^16 + 3.6711725e-12*x^17 - 1.9835736e-11*x^18 + 4.9956502e-12*x^19 - ...
7.3416852e-13*x^20 + 7.2822196e-14*x^21 - 4.9740077e-15*x^22 + 2.2607386e-16*x^23 - ...
6.1866125e-18*x^24 + 7.7315661e-20*x^25
f = 
FM = regexprep(char(vpa(f)),'([0-9]+\.[0-9]+)','${num2str(str2num($1),''%e'')}')
FM = '2.947611e-01*x + 4.738056e-01*x^2 - 1.763213e-01*x^3 + 3.842601e-02*x^4 - 5.090693e-03*x^5 + 7.368661e-04*x^6 - 2.454919e-04*x^7 + 7.275847e-05*x^8 - 1.726065e-05*x^9 + 4.840995e-06*x^10 - 1.470883e-06*x^11 + 3.777729e-07*x^12 - 8.675773e-08*x^13 + 2.003165e-08*x^14 - 4.103943e-09*x^15 + 5.354131e-10*x^16 + 3.671173e-12*x^17 - 1.983574e-11*x^18 + 4.995650e-12*x^19 - 7.341685e-13*x^20 + 7.282220e-14*x^21 - 4.974008e-15*x^22 + 2.260739e-16*x^23 - 6.186612e-18*x^24 + 7.731566e-20*x^25 - 4.244016e-02'
@ VBBV
May be (how to check?), but it works for me.
Are you okay with the output being a character array, and not a symbolic expression?
@ Dyuman
Yes, I will use that expression as solution of ODE, nothing more with that.

Accedi per commentare.

Più risposte (2)

syms x
f = - 0.042440155 + 0.29476113*x + 0.47380563*x^2 - 0.17632127*x^3 + 0.038426007*x^4 - 0.005090693*x^5 + ...
0.00073686606*x^6 - 0.00024549191*x^7 + 0.000072758471*x^8 - 0.000017260649*x^9 + 0.0000048409949*x^10 - ...
0.0000014708831*x^11 + 0.00000037777292*x^12 - 0.000000086757727*x^13 + 0.000000020031646*x^14 - ...
4.1039434e-9*x^15 + 5.354131e-10*x^16 + 3.6711725e-12*x^17 - 1.9835736e-11*x^18 + 4.9956502e-12*x^19 - ...
7.3416852e-13*x^20 + 7.2822196e-14*x^21 - 4.9740077e-15*x^22 + 2.2607386e-16*x^23 - ...
6.1866125e-18*x^24 + 7.7315661e-20*x^25
f = 
f = vpa(f,2)
f = 
something like this

1 Commento

@VBBV, nice idea, however, it does not change the notation for all values, please see the coefficients of x^4, x^3, x^2, x^1 and x^0.

Accedi per commentare.

syms x
f = - 0.042440155 + 0.29476113*x + 0.47380563*x^2 - 0.17632127*x^3 + 0.038426007*x^4 - 0.005090693*x^5 + ...
0.00073686606*x^6 - 0.00024549191*x^7 + 0.000072758471*x^8 - 0.000017260649*x^9 + 0.0000048409949*x^10 - ...
0.0000014708831*x^11 + 0.00000037777292*x^12 - 0.000000086757727*x^13 + 0.000000020031646*x^14 - ...
4.1039434e-9*x^15 + 5.354131e-10*x^16 + 3.6711725e-12*x^17 - 1.9835736e-11*x^18 + 4.9956502e-12*x^19 - ...
7.3416852e-13*x^20 + 7.2822196e-14*x^21 - 4.9740077e-15*x^22 + 2.2607386e-16*x^23 - ...
6.1866125e-18*x^24 + 7.7315661e-20*x^25
f = 
c = coeffs(f, 'all') % or
c = 
[c, t] = coeffs(f)
c = 
t = 
Now you can do whatever formatting you want with c.
The 'all' option and/or the second input are useful if one of the powers of x is not present in f, as in this example:
f2 = x^3+2*x+3
f2 = 
c1 = coeffs(f2) % Missing the x^2 term and in a different order
c1 = 
c2 = coeffs(f2, 'all') % Including the x^2 term
c2 = 
[c3, t3] = coeffs(f2) % x^2 (and its coefficient) are not present in both c3 and t3
c3 = 
t3 = 

Categorie

Scopri di più su Christmas / Winter in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by