http://www.mathworks.com/matlabcentral/newsreader/view_thread/241696
MATLAB Central Newsreader  extract the exact value from an approximated variable
Feed for thread: extract the exact value from an approximated variable
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 29 Dec 2008 20:16:02 +0000
extract the exact value from an approximated variable
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241696#619191
Orchid Bee
hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.<br>
i have a variable, fitmodel fm1, with the coefficients of the polynomial that fit the data. if i use the symbolic values of these coefficients in a consequent equation i get different result than if i use the numeric values of the coefficients that are showed for these variables.<br>
supposing the case:<br>
Linear model Poly9:<br>
fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 + <br>
p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10<br>
Coefficients (with 95% confidence bounds):<br>
p1 = 8.32e007 (8.257e007, 8.383e007)<br>
p2 = 5.217e005 (5.25e005, 5.185e005)<br>
p3 = 0.001308 (0.001301, 0.001315)<br>
p4 = 0.01684 (0.01693, 0.01676)<br>
p5 = 0.1189 (0.1183, 0.1195)<br>
p6 = 0.4585 (0.461, 0.456)<br>
p7 = 0.9485 (0.9422, 0.9547)<br>
p8 = 1.185 (1.193, 1.176)<br>
p9 = 1.063 (1.058, 1.068)<br>
p10 = 0.5639 (0.565, 0.5629)<br>
<br>
i get different result if i use fm1.p1 than if i use 8.32/10^7.<br>
nonetheless i need to use the numerical data, not the variable itself; so i'd like to ask you about how can i extract the exact value of the variables? <br>
thank you so very much!<br>
<br>
Orchy

Mon, 29 Dec 2008 20:40:03 +0000
Re: extract the exact value from an approximated variable
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241696#619194
Roger Stafford
"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...<br>
> hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.<br>
> i have a variable, fitmodel fm1, with the coefficients of the polynomial that fit the data. if i use the symbolic values of these coefficients in a consequent equation i get different result than if i use the numeric values of the coefficients that are showed for these variables.<br>
> supposing the case:<br>
> Linear model Poly9:<br>
> fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 + <br>
> p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10<br>
> Coefficients (with 95% confidence bounds):<br>
> p1 = 8.32e007 (8.257e007, 8.383e007)<br>
> p2 = 5.217e005 (5.25e005, 5.185e005)<br>
> p3 = 0.001308 (0.001301, 0.001315)<br>
> p4 = 0.01684 (0.01693, 0.01676)<br>
> p5 = 0.1189 (0.1183, 0.1195)<br>
> p6 = 0.4585 (0.461, 0.456)<br>
> p7 = 0.9485 (0.9422, 0.9547)<br>
> p8 = 1.185 (1.193, 1.176)<br>
> p9 = 1.063 (1.058, 1.068)<br>
> p10 = 0.5639 (0.565, 0.5629)<br>
> <br>
> i get different result if i use fm1.p1 than if i use 8.32/10^7.<br>
> nonetheless i need to use the numerical data, not the variable itself; so i'd like to ask you about how can i extract the exact value of the variables? <br>
> thank you so very much!<br>
> <br>
> Orchy<br>
<br>
You haven't stated what kind of fit to the data you are striving for. If you have exactly ten data points, in general an exact unique fit is possible, but if there are more, then the coefficients you obtain will depend on what sort of fit you want. This is true symbolically as well as numerically. For example, a least squares fit can be obtained using the matlab backslash operator '\' and the result of that is exact for a symbolic solution and numerically the accuracy can be to approximately one part in 1e16.<br>
<br>
Roger Stafford

Mon, 29 Dec 2008 20:57:02 +0000
Re: extract the exact value from an approximated variable
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241696#619196
someone
"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...<br>
> hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.<br>
> i have a variable, fitmodel fm1, with the coefficients of the polynomial that fit the data. if i use the symbolic values of these coefficients in a consequent equation i get different result than if i use the numeric values of the coefficients that are showed for these variables.<br>
> supposing the case:<br>
> Linear model Poly9:<br>
> fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 + <br>
> p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10<br>
> Coefficients (with 95% confidence bounds):<br>
> p1 = 8.32e007 (8.257e007, 8.383e007)<br>
> p2 = 5.217e005 (5.25e005, 5.185e005)<br>
> p3 = 0.001308 (0.001301, 0.001315)<br>
> p4 = 0.01684 (0.01693, 0.01676)<br>
> p5 = 0.1189 (0.1183, 0.1195)<br>
> p6 = 0.4585 (0.461, 0.456)<br>
> p7 = 0.9485 (0.9422, 0.9547)<br>
> p8 = 1.185 (1.193, 1.176)<br>
> p9 = 1.063 (1.058, 1.068)<br>
> p10 = 0.5639 (0.565, 0.5629)<br>
> <br>
> i get different result if i use fm1.p1 than if i use 8.32/10^7.<br>
> nonetheless i need to use the numerical data, not the variable itself; so i'd like to ask you about how can i extract the exact value of the variables? <br>
> thank you so very much!<br>
> <br>
> Orchy<br>
<br>
% If I understand, try<br>
<br>
doc format<br>
<br>
fm1.p1<br>
format long<br>
fm1.p1<br>
<br>
% You will see many more digits displayed.<br>
<br>
% By default (with format short), MATLAB displays only about 4 significant digits.<br>
% Using format long dispalys about 3 times as many significant digits.<br>
<br>
% format affects ONLY how the numbers are DISPLAYED.<br>
% Not how MATLAB computes or stores the numbers.<br>
<br>
% Or use the File > Preferences > Command Window option

Mon, 29 Dec 2008 21:03:02 +0000
Re: extract the exact value from an approximated variable
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241696#619197
Orchid Bee
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjbcj3$2j5$1@fred.mathworks.com>...<br>
> "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...<br>
> > hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.<br>
> > i have a variable, fitmodel fm1, with the coefficients of the polynomial that fit the data. if i use the symbolic values of these coefficients in a consequent equation i get different result than if i use the numeric values of the coefficients that are showed for these variables.<br>
> > supposing the case:<br>
> > Linear model Poly9:<br>
> > fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 + <br>
> > p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10<br>
> > Coefficients (with 95% confidence bounds):<br>
> > p1 = 8.32e007 (8.257e007, 8.383e007)<br>
> > p2 = 5.217e005 (5.25e005, 5.185e005)<br>
> > p3 = 0.001308 (0.001301, 0.001315)<br>
> > p4 = 0.01684 (0.01693, 0.01676)<br>
> > p5 = 0.1189 (0.1183, 0.1195)<br>
> > p6 = 0.4585 (0.461, 0.456)<br>
> > p7 = 0.9485 (0.9422, 0.9547)<br>
> > p8 = 1.185 (1.193, 1.176)<br>
> > p9 = 1.063 (1.058, 1.068)<br>
> > p10 = 0.5639 (0.565, 0.5629)<br>
> > <br>
> > i get different result if i use fm1.p1 than if i use 8.32/10^7.<br>
> > nonetheless i need to use the numerical data, not the variable itself; so i'd like to ask you about how can i extract the exact value of the variables? <br>
> > thank you so very much!<br>
> > <br>
> > Orchy<br>
> <br>
> You haven't stated what kind of fit to the data you are striving for. If you have exactly ten data points, in general an exact unique fit is possible, but if there are more, then the coefficients you obtain will depend on what sort of fit you want. This is true symbolically as well as numerically. For example, a least squares fit can be obtained using the matlab backslash operator '\' and the result of that is exact for a symbolic solution and numerically the accuracy can be to approximately one part in 1e16.<br>
> <br>
> Roger Stafford<br>
<br>
 Hi, Roger.<br>
i'm not sure i get your point. is the type of fit relevant for my issue?<br>
i use polynomial of 9 degree to fit my data whose size varies. <br>
i used the data to obtain the fit function using 9 degree polynomial. further, i want to make use of the polynomial i get. <br>
instead of using the expression <br>
pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...<br>
i want to use <br>
pf = 8.32/(10^7)*myx^95.217/(10^5)*myx^8+...<br>
<br>
my problem appears as the second pf is different than the first pf. and more certainly that's because fm1.p1 displays a rather approximated value of the first coefficient, so , when i copy it as it is displayed on the screen, i don't get the right coefficient, but that certain approximation. i don't know how to extract the exact numerical value from fm1.p1(, and other similar coefficients).<br>
Orchy

Mon, 29 Dec 2008 21:46:03 +0000
Re: extract the exact value from an approximated variable
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241696#619201
Jiro Doke
"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbdu6$ocp$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjbcj3$2j5$1@fred.mathworks.com>...<br>
> > "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...<br>
> > > hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.<br>
> > > i have a variable, fitmodel fm1, with the coefficients of the polynomial that fit the data. if i use the symbolic values of these coefficients in a consequent equation i get different result than if i use the numeric values of the coefficients that are showed for these variables.<br>
> > > supposing the case:<br>
> > > Linear model Poly9:<br>
> > > fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 + <br>
> > > p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10<br>
> > > Coefficients (with 95% confidence bounds):<br>
> > > p1 = 8.32e007 (8.257e007, 8.383e007)<br>
> > > p2 = 5.217e005 (5.25e005, 5.185e005)<br>
> > > p3 = 0.001308 (0.001301, 0.001315)<br>
> > > p4 = 0.01684 (0.01693, 0.01676)<br>
> > > p5 = 0.1189 (0.1183, 0.1195)<br>
> > > p6 = 0.4585 (0.461, 0.456)<br>
> > > p7 = 0.9485 (0.9422, 0.9547)<br>
> > > p8 = 1.185 (1.193, 1.176)<br>
> > > p9 = 1.063 (1.058, 1.068)<br>
> > > p10 = 0.5639 (0.565, 0.5629)<br>
> > > <br>
> > > i get different result if i use fm1.p1 than if i use 8.32/10^7.<br>
> > > nonetheless i need to use the numerical data, not the variable itself; so i'd like to ask you about how can i extract the exact value of the variables? <br>
> > > thank you so very much!<br>
> > > <br>
> > > Orchy<br>
> > <br>
> > You haven't stated what kind of fit to the data you are striving for. If you have exactly ten data points, in general an exact unique fit is possible, but if there are more, then the coefficients you obtain will depend on what sort of fit you want. This is true symbolically as well as numerically. For example, a least squares fit can be obtained using the matlab backslash operator '\' and the result of that is exact for a symbolic solution and numerically the accuracy can be to approximately one part in 1e16.<br>
> > <br>
> > Roger Stafford<br>
> <br>
>  Hi, Roger.<br>
> i'm not sure i get your point. is the type of fit relevant for my issue?<br>
> i use polynomial of 9 degree to fit my data whose size varies. <br>
> i used the data to obtain the fit function using 9 degree polynomial. further, i want to make use of the polynomial i get. <br>
> instead of using the expression <br>
> pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...<br>
> i want to use <br>
> pf = 8.32/(10^7)*myx^95.217/(10^5)*myx^8+...<br>
> <br>
> my problem appears as the second pf is different than the first pf. and more certainly that's because fm1.p1 displays a rather approximated value of the first coefficient, so , when i copy it as it is displayed on the screen, i don't get the right coefficient, but that certain approximation. i don't know how to extract the exact numerical value from fm1.p1(, and other similar coefficients).<br>
> Orchy<br>
<br>
Orchy,<br>
<br>
It looks like your are talking about the output that comes out for the "fit" function from the Curve Fitting Toolbox.<br>
<br>
fm1.p1, fm1.p2, etc are the accurate coefficients. What's displayed on the screen will not be the exact because of the precision for the display (it's merely for display). So ideally, you should use the actual values from the fit result (fm1.p1, etc).<br>
<br>
My question is WHY do you need to represent it with the actual numbers? I can think of many reasons why you should NOT do that, one of which is the accuracy problem you are encountering. Another reason is that you are hardcoding the results, so if you were to reuse your code on a different data set, you have to rewrite your code. Is there a reason you cannot use the variable form (the first expression you showed)?<br>
<br>
If you can answer me these questions, then we can try to address your issue.<br>
<br>
jiro

Mon, 29 Dec 2008 22:12:01 +0000
Re: extract the exact value from an approximated variable
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241696#619204
Orchid Bee
"Jiro Doke" <jiro.doke@mathworks.com> wrote in message <gjbgeq$sej$1@fred.mathworks.com>...<br>
> "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbdu6$ocp$1@fred.mathworks.com>...<br>
> > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjbcj3$2j5$1@fred.mathworks.com>...<br>
> > > "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...<br>
> > > > hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.<br>
> > > > i have a variable, fitmodel fm1, with the coefficients of the polynomial that fit the data. if i use the symbolic values of these coefficients in a consequent equation i get different result than if i use the numeric values of the coefficients that are showed for these variables.<br>
> > > > supposing the case:<br>
> > > > Linear model Poly9:<br>
> > > > fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 + <br>
> > > > p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10<br>
> > > > Coefficients (with 95% confidence bounds):<br>
> > > > p1 = 8.32e007 (8.257e007, 8.383e007)<br>
> > > > p2 = 5.217e005 (5.25e005, 5.185e005)<br>
> > > > p3 = 0.001308 (0.001301, 0.001315)<br>
> > > > p4 = 0.01684 (0.01693, 0.01676)<br>
> > > > p5 = 0.1189 (0.1183, 0.1195)<br>
> > > > p6 = 0.4585 (0.461, 0.456)<br>
> > > > p7 = 0.9485 (0.9422, 0.9547)<br>
> > > > p8 = 1.185 (1.193, 1.176)<br>
> > > > p9 = 1.063 (1.058, 1.068)<br>
> > > > p10 = 0.5639 (0.565, 0.5629)<br>
> > > > <br>
> > > > i get different result if i use fm1.p1 than if i use 8.32/10^7.<br>
> > > > nonetheless i need to use the numerical data, not the variable itself; so i'd like to ask you about how can i extract the exact value of the variables? <br>
> > > > thank you so very much!<br>
> > > > <br>
> > > > Orchy<br>
> > > <br>
> > > You haven't stated what kind of fit to the data you are striving for. If you have exactly ten data points, in general an exact unique fit is possible, but if there are more, then the coefficients you obtain will depend on what sort of fit you want. This is true symbolically as well as numerically. For example, a least squares fit can be obtained using the matlab backslash operator '\' and the result of that is exact for a symbolic solution and numerically the accuracy can be to approximately one part in 1e16.<br>
> > > <br>
> > > Roger Stafford<br>
> > <br>
> >  Hi, Roger.<br>
> > i'm not sure i get your point. is the type of fit relevant for my issue?<br>
> > i use polynomial of 9 degree to fit my data whose size varies. <br>
> > i used the data to obtain the fit function using 9 degree polynomial. further, i want to make use of the polynomial i get. <br>
> > instead of using the expression <br>
> > pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...<br>
> > i want to use <br>
> > pf = 8.32/(10^7)*myx^95.217/(10^5)*myx^8+...<br>
> > <br>
> > my problem appears as the second pf is different than the first pf. and more certainly that's because fm1.p1 displays a rather approximated value of the first coefficient, so , when i copy it as it is displayed on the screen, i don't get the right coefficient, but that certain approximation. i don't know how to extract the exact numerical value from fm1.p1(, and other similar coefficients).<br>
> > Orchy<br>
> <br>
> Orchy,<br>
> <br>
> It looks like your are talking about the output that comes out for the "fit" function from the Curve Fitting Toolbox.<br>
> <br>
> fm1.p1, fm1.p2, etc are the accurate coefficients. What's displayed on the screen will not be the exact because of the precision for the display (it's merely for display). So ideally, you should use the actual values from the fit result (fm1.p1, etc).<br>
> <br>
> My question is WHY do you need to represent it with the actual numbers? I can think of many reasons why you should NOT do that, one of which is the accuracy problem you are encountering. Another reason is that you are hardcoding the results, so if you were to reuse your code on a different data set, you have to rewrite your code. Is there a reason you cannot use the variable form (the first expression you showed)?<br>
> <br>
> If you can answer me these questions, then we can try to address your issue.<br>
> <br>
> jiro<br>
<br>
hi Jiro,<br>
yes, i am using the result from the Curve Fitting Toolbox . and yes, i am encountering the first issue, but not the second one. <br>
my goal here was to find a function with a particular property. to have that property , i knew i could come up with a sort of sinusoidal combination, however i needed to describe the function by polynomial coefficients, for some code reuse reasons. therefore, i generated data with some sinusoidal function, used curve fitting tool to have a polynomial description. now, rather than doing the same procedure everytime just to get those variables and used them , i wanted to simplify the problem by only having those numerical values in the expression of the function i am hunting. moreover, the expression has to be written eventually in C code, and there it's impossible to use any matlab function. <br>
so, this is the brief of the rationale.<br>
<br>
as advised above, i used 'format long' command to get more accurate values. the result now is ok, but it takes a great while to compute the values of the function, probably due to those long accurate numbers?<br>
but i suppose i can't avoid this.<br>
still i wonder why it takes longer time to compute <br>
pf = 8.32/(10^7)*myx^95.217/(10^5)*myx^8+...<br>
than it takes to compute <br>
pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...<br>
even if at the moment the numerical coefficients have the same accuracy as the symbolic ones.<br>
<br>
thanks a lot for assistance,<br>
Orchy

Mon, 29 Dec 2008 22:32:01 +0000
Re: extract the exact value from an approximated variable
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241696#619206
someone
"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbhvh$9h$1@fred.mathworks.com>...<br>
> "Jiro Doke" <jiro.doke@mathworks.com> wrote in message <gjbgeq$sej$1@fred.mathworks.com>...<br>
> > "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbdu6$ocp$1@fred.mathworks.com>...<br>
> > > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjbcj3$2j5$1@fred.mathworks.com>...<br>
> > > > "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...<br>
> > > > > hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.<br>
> > > > > i have a variable, fitmodel fm1, with the coefficients of the polynomial that fit the data. if i use the symbolic values of these coefficients in a consequent equation i get different result than if i use the numeric values of the coefficients that are showed for these variables.<br>
> > > > > supposing the case:<br>
> > > > > Linear model Poly9:<br>
> > > > > fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 + <br>
> > > > > p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10<br>
> > > > > Coefficients (with 95% confidence bounds):<br>
> > > > > p1 = 8.32e007 (8.257e007, 8.383e007)<br>
> > > > > p2 = 5.217e005 (5.25e005, 5.185e005)<br>
> > > > > p3 = 0.001308 (0.001301, 0.001315)<br>
> > > > > p4 = 0.01684 (0.01693, 0.01676)<br>
> > > > > p5 = 0.1189 (0.1183, 0.1195)<br>
> > > > > p6 = 0.4585 (0.461, 0.456)<br>
> > > > > p7 = 0.9485 (0.9422, 0.9547)<br>
> > > > > p8 = 1.185 (1.193, 1.176)<br>
> > > > > p9 = 1.063 (1.058, 1.068)<br>
> > > > > p10 = 0.5639 (0.565, 0.5629)<br>
> > > > > <br>
> > > > > i get different result if i use fm1.p1 than if i use 8.32/10^7.<br>
> > > > > nonetheless i need to use the numerical data, not the variable itself; so i'd like to ask you about how can i extract the exact value of the variables? <br>
> > > > > thank you so very much!<br>
> > > > > <br>
> > > > > Orchy<br>
> > > > <br>
> > > > You haven't stated what kind of fit to the data you are striving for. If you have exactly ten data points, in general an exact unique fit is possible, but if there are more, then the coefficients you obtain will depend on what sort of fit you want. This is true symbolically as well as numerically. For example, a least squares fit can be obtained using the matlab backslash operator '\' and the result of that is exact for a symbolic solution and numerically the accuracy can be to approximately one part in 1e16.<br>
> > > > <br>
> > > > Roger Stafford<br>
> > > <br>
> > >  Hi, Roger.<br>
> > > i'm not sure i get your point. is the type of fit relevant for my issue?<br>
> > > i use polynomial of 9 degree to fit my data whose size varies. <br>
> > > i used the data to obtain the fit function using 9 degree polynomial. further, i want to make use of the polynomial i get. <br>
> > > instead of using the expression <br>
> > > pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...<br>
> > > i want to use <br>
> > > pf = 8.32/(10^7)*myx^95.217/(10^5)*myx^8+...<br>
> > > <br>
> > > my problem appears as the second pf is different than the first pf. and more certainly that's because fm1.p1 displays a rather approximated value of the first coefficient, so , when i copy it as it is displayed on the screen, i don't get the right coefficient, but that certain approximation. i don't know how to extract the exact numerical value from fm1.p1(, and other similar coefficients).<br>
> > > Orchy<br>
> > <br>
> > Orchy,<br>
> > <br>
> > It looks like your are talking about the output that comes out for the "fit" function from the Curve Fitting Toolbox.<br>
> > <br>
> > fm1.p1, fm1.p2, etc are the accurate coefficients. What's displayed on the screen will not be the exact because of the precision for the display (it's merely for display). So ideally, you should use the actual values from the fit result (fm1.p1, etc).<br>
> > <br>
> > My question is WHY do you need to represent it with the actual numbers? I can think of many reasons why you should NOT do that, one of which is the accuracy problem you are encountering. Another reason is that you are hardcoding the results, so if you were to reuse your code on a different data set, you have to rewrite your code. Is there a reason you cannot use the variable form (the first expression you showed)?<br>
> > <br>
> > If you can answer me these questions, then we can try to address your issue.<br>
> > <br>
> > jiro<br>
> <br>
> hi Jiro,<br>
> yes, i am using the result from the Curve Fitting Toolbox . and yes, i am encountering the first issue, but not the second one. <br>
> my goal here was to find a function with a particular property. to have that property , i knew i could come up with a sort of sinusoidal combination, however i needed to describe the function by polynomial coefficients, for some code reuse reasons. therefore, i generated data with some sinusoidal function, used curve fitting tool to have a polynomial description. now, rather than doing the same procedure everytime just to get those variables and used them , i wanted to simplify the problem by only having those numerical values in the expression of the function i am hunting. moreover, the expression has to be written eventually in C code, and there it's impossible to use any matlab function. <br>
> so, this is the brief of the rationale.<br>
> <br>
> as advised above, i used 'format long' command to get more accurate values. the result now is ok, but it takes a great while to compute the values of the function, probably due to those long accurate numbers?<br>
> but i suppose i can't avoid this.<br>
> still i wonder why it takes longer time to compute <br>
> pf = 8.32/(10^7)*myx^95.217/(10^5)*myx^8+...<br>
> than it takes to compute <br>
> pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...<br>
> even if at the moment the numerical coefficients have the same accuracy as the symbolic ones.<br>
> <br>
> thanks a lot for assistance,<br>
> Orchy<br>
<br>
I doubt that the increase in time (how much?) is due to the "long accurate numbers". It probably has more to do with how the MATLAB parser works when it reads in an mfile.<br>
<br>
In the second case it simply retrieves the fm1.p1, etc. values from storage and uses it.<br>
<br>
In the first case, my guess is it does something like set a temp1 variable to 8.32. Then set a second temp2 variable to 10^7. Then divide the two and put that result in temp3. Then retrieve temp3 as fm1.p1 use it from there as in the second case. And you have 10 of these coefficients, right?<br>
<br>
But again, it depends on the specifics of the MATLAB parser, I could be wrong!

Mon, 29 Dec 2008 22:52:02 +0000
Re: extract the exact value from an approximated variable
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241696#619207
Jiro Doke
"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbhvh$9h$1@fred.mathworks.com>...<br>
> hi Jiro,<br>
> yes, i am using the result from the Curve Fitting Toolbox . and yes, i am encountering the first issue, but not the second one. <br>
> my goal here was to find a function with a particular property. to have that property , i knew i could come up with a sort of sinusoidal combination, however i needed to describe the function by polynomial coefficients, for some code reuse reasons. therefore, i generated data with some sinusoidal function, used curve fitting tool to have a polynomial description. now, rather than doing the same procedure everytime just to get those variables and used them , i wanted to simplify the problem by only having those numerical values in the expression of the function i am hunting. moreover, the expression has to be written eventually in C code, and there it's impossible to use any matlab function. <br>
> so, this is the brief of the rationale.<br>
> <br>
> as advised above, i used 'format long' command to get more accurate values. the result now is ok, but it takes a great while to compute the values of the function, probably due to those long accurate numbers?<br>
> but i suppose i can't avoid this.<br>
> still i wonder why it takes longer time to compute <br>
> pf = 8.32/(10^7)*myx^95.217/(10^5)*myx^8+...<br>
> than it takes to compute <br>
> pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...<br>
> even if at the moment the numerical coefficients have the same accuracy as the symbolic ones.<br>
> <br>
> thanks a lot for assistance,<br>
> Orchy<br>
<br>
Hi Orchy,<br>
<br>
You could always save the fit result (fm1) in a MAT file, which could be loaded into your session quickly whenever you need it. Also, you can evaluate the function at an arbitrary point just by typing:<br>
<br>
pf = fm1(myx)<br>
<br>
The longer computation time has nothing to do with the precision. The difference is that in your first case, you are performing 2 computations per coefficient: ^ and /. You should use scientific notation, e.g 1e7. Take a look at this:<br>
<br>
>> tic;for ii = 1:10000000,a=3e7;end;toc<br>
Elapsed time is 0.036858 seconds.<br>
>> tic;for ii = 1:10000000,b=3/(10^7);end;toc<br>
Elapsed time is 2.888624 seconds.<br>
<br>
The part about "write in C code" may make it a bit tricky. This is a bit offtopic from this thread, but depending on what you want, there are some options. If you just want to deploy MATLAB functionality to be used in C, you can use the MATLAB Compiler to create a shared library (you won't get C code). If you really want C code, you can look at Embedded MATLAB, which is a subset of the MATLAB language (not Toolbox) that can be converted to C (requires RealTime Workshop). In your case, if you're simply looking at a polynomial fit, you can just use POLYFIT (and POLYVAL) from MATLAB, instead of FIT from the Curve Fitting Toolbox:<br>
<br>
>> x = 1:10;<br>
>> y = rand(1,10);<br>
>> p = polyfit(x,y,5) % 5th order polynomial<br>
p =<br>
0.0017 0.0522 0.5745 2.8134 5.9002 4.5816<br>
>> newY = polyval(p, [1.3, 5.7, 9.5])<br>
newY =<br>
0.5465 0.6208 0.5614