Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
extract the exact value from an approximated variable

Subject: extract the exact value from an approximated variable

From: Orchid Bee

Date: 29 Dec, 2008 20:16:02

Message: 1 of 8

hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.
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.
supposing the case:
Linear model Poly9:
       fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 +
                    p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10
     Coefficients (with 95% confidence bounds):
       p1 = 8.32e-007 (8.257e-007, 8.383e-007)
       p2 = -5.217e-005 (-5.25e-005, -5.185e-005)
       p3 = 0.001308 (0.001301, 0.001315)
       p4 = -0.01684 (-0.01693, -0.01676)
       p5 = 0.1189 (0.1183, 0.1195)
       p6 = -0.4585 (-0.461, -0.456)
       p7 = 0.9485 (0.9422, 0.9547)
       p8 = -1.185 (-1.193, -1.176)
       p9 = 1.063 (1.058, 1.068)
       p10 = -0.5639 (-0.565, -0.5629)

i get different result if i use fm1.p1 than if i use 8.32/10^7.
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?
thank you so very much!

Orchy

Subject: extract the exact value from an approximated variable

From: Roger Stafford

Date: 29 Dec, 2008 20:40:03

Message: 2 of 8

"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...
> hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.
> 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.
> supposing the case:
> Linear model Poly9:
> fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 +
> p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10
> Coefficients (with 95% confidence bounds):
> p1 = 8.32e-007 (8.257e-007, 8.383e-007)
> p2 = -5.217e-005 (-5.25e-005, -5.185e-005)
> p3 = 0.001308 (0.001301, 0.001315)
> p4 = -0.01684 (-0.01693, -0.01676)
> p5 = 0.1189 (0.1183, 0.1195)
> p6 = -0.4585 (-0.461, -0.456)
> p7 = 0.9485 (0.9422, 0.9547)
> p8 = -1.185 (-1.193, -1.176)
> p9 = 1.063 (1.058, 1.068)
> p10 = -0.5639 (-0.565, -0.5629)
>
> i get different result if i use fm1.p1 than if i use 8.32/10^7.
> 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?
> thank you so very much!
>
> Orchy

  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.

Roger Stafford

Subject: extract the exact value from an approximated variable

From: someone

Date: 29 Dec, 2008 20:57:02

Message: 3 of 8

"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...
> hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.
> 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.
> supposing the case:
> Linear model Poly9:
> fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 +
> p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10
> Coefficients (with 95% confidence bounds):
> p1 = 8.32e-007 (8.257e-007, 8.383e-007)
> p2 = -5.217e-005 (-5.25e-005, -5.185e-005)
> p3 = 0.001308 (0.001301, 0.001315)
> p4 = -0.01684 (-0.01693, -0.01676)
> p5 = 0.1189 (0.1183, 0.1195)
> p6 = -0.4585 (-0.461, -0.456)
> p7 = 0.9485 (0.9422, 0.9547)
> p8 = -1.185 (-1.193, -1.176)
> p9 = 1.063 (1.058, 1.068)
> p10 = -0.5639 (-0.565, -0.5629)
>
> i get different result if i use fm1.p1 than if i use 8.32/10^7.
> 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?
> thank you so very much!
>
> Orchy

% If I understand, try

doc format

fm1.p1
format long
fm1.p1

% You will see many more digits displayed.

% By default (with format short), MATLAB displays only about 4 significant digits.
% Using format long dispalys about 3 times as many significant digits.

% format affects ONLY how the numbers are DISPLAYED.
% Not how MATLAB computes or stores the numbers.

% Or use the File -> Preferences -> Command Window option

Subject: extract the exact value from an approximated variable

From: Orchid Bee

Date: 29 Dec, 2008 21:03:02

Message: 4 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjbcj3$2j5$1@fred.mathworks.com>...
> "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...
> > hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.
> > 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.
> > supposing the case:
> > Linear model Poly9:
> > fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 +
> > p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10
> > Coefficients (with 95% confidence bounds):
> > p1 = 8.32e-007 (8.257e-007, 8.383e-007)
> > p2 = -5.217e-005 (-5.25e-005, -5.185e-005)
> > p3 = 0.001308 (0.001301, 0.001315)
> > p4 = -0.01684 (-0.01693, -0.01676)
> > p5 = 0.1189 (0.1183, 0.1195)
> > p6 = -0.4585 (-0.461, -0.456)
> > p7 = 0.9485 (0.9422, 0.9547)
> > p8 = -1.185 (-1.193, -1.176)
> > p9 = 1.063 (1.058, 1.068)
> > p10 = -0.5639 (-0.565, -0.5629)
> >
> > i get different result if i use fm1.p1 than if i use 8.32/10^7.
> > 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?
> > thank you so very much!
> >
> > Orchy
>
> 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.
>
> Roger Stafford

- Hi, Roger.
i'm not sure i get your point. is the type of fit relevant for my issue?
i use polynomial of 9 degree to fit my data whose size varies.
i used the data to obtain the fit function using 9 degree polynomial. further, i want to make use of the polynomial i get.
instead of using the expression
pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...
i want to use
pf = 8.32/(10^7)*myx^9-5.217/(10^5)*myx^8+...

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).
Orchy

Subject: extract the exact value from an approximated variable

From: Jiro Doke

Date: 29 Dec, 2008 21:46:03

Message: 5 of 8

"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbdu6$ocp$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjbcj3$2j5$1@fred.mathworks.com>...
> > "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...
> > > hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.
> > > 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.
> > > supposing the case:
> > > Linear model Poly9:
> > > fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 +
> > > p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10
> > > Coefficients (with 95% confidence bounds):
> > > p1 = 8.32e-007 (8.257e-007, 8.383e-007)
> > > p2 = -5.217e-005 (-5.25e-005, -5.185e-005)
> > > p3 = 0.001308 (0.001301, 0.001315)
> > > p4 = -0.01684 (-0.01693, -0.01676)
> > > p5 = 0.1189 (0.1183, 0.1195)
> > > p6 = -0.4585 (-0.461, -0.456)
> > > p7 = 0.9485 (0.9422, 0.9547)
> > > p8 = -1.185 (-1.193, -1.176)
> > > p9 = 1.063 (1.058, 1.068)
> > > p10 = -0.5639 (-0.565, -0.5629)
> > >
> > > i get different result if i use fm1.p1 than if i use 8.32/10^7.
> > > 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?
> > > thank you so very much!
> > >
> > > Orchy
> >
> > 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.
> >
> > Roger Stafford
>
> - Hi, Roger.
> i'm not sure i get your point. is the type of fit relevant for my issue?
> i use polynomial of 9 degree to fit my data whose size varies.
> i used the data to obtain the fit function using 9 degree polynomial. further, i want to make use of the polynomial i get.
> instead of using the expression
> pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...
> i want to use
> pf = 8.32/(10^7)*myx^9-5.217/(10^5)*myx^8+...
>
> 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).
> Orchy

Orchy,

It looks like your are talking about the output that comes out for the "fit" function from the Curve Fitting Toolbox.

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).

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 hard-coding the results, so if you were to reuse your code on a different data set, you have to re-write your code. Is there a reason you cannot use the variable form (the first expression you showed)?

If you can answer me these questions, then we can try to address your issue.

jiro

Subject: extract the exact value from an approximated variable

From: Orchid Bee

Date: 29 Dec, 2008 22:12:01

Message: 6 of 8

"Jiro Doke" <jiro.doke@mathworks.com> wrote in message <gjbgeq$sej$1@fred.mathworks.com>...
> "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbdu6$ocp$1@fred.mathworks.com>...
> > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjbcj3$2j5$1@fred.mathworks.com>...
> > > "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...
> > > > hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.
> > > > 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.
> > > > supposing the case:
> > > > Linear model Poly9:
> > > > fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 +
> > > > p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10
> > > > Coefficients (with 95% confidence bounds):
> > > > p1 = 8.32e-007 (8.257e-007, 8.383e-007)
> > > > p2 = -5.217e-005 (-5.25e-005, -5.185e-005)
> > > > p3 = 0.001308 (0.001301, 0.001315)
> > > > p4 = -0.01684 (-0.01693, -0.01676)
> > > > p5 = 0.1189 (0.1183, 0.1195)
> > > > p6 = -0.4585 (-0.461, -0.456)
> > > > p7 = 0.9485 (0.9422, 0.9547)
> > > > p8 = -1.185 (-1.193, -1.176)
> > > > p9 = 1.063 (1.058, 1.068)
> > > > p10 = -0.5639 (-0.565, -0.5629)
> > > >
> > > > i get different result if i use fm1.p1 than if i use 8.32/10^7.
> > > > 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?
> > > > thank you so very much!
> > > >
> > > > Orchy
> > >
> > > 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.
> > >
> > > Roger Stafford
> >
> > - Hi, Roger.
> > i'm not sure i get your point. is the type of fit relevant for my issue?
> > i use polynomial of 9 degree to fit my data whose size varies.
> > i used the data to obtain the fit function using 9 degree polynomial. further, i want to make use of the polynomial i get.
> > instead of using the expression
> > pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...
> > i want to use
> > pf = 8.32/(10^7)*myx^9-5.217/(10^5)*myx^8+...
> >
> > 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).
> > Orchy
>
> Orchy,
>
> It looks like your are talking about the output that comes out for the "fit" function from the Curve Fitting Toolbox.
>
> 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).
>
> 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 hard-coding the results, so if you were to reuse your code on a different data set, you have to re-write your code. Is there a reason you cannot use the variable form (the first expression you showed)?
>
> If you can answer me these questions, then we can try to address your issue.
>
> jiro

hi Jiro,
yes, i am using the result from the Curve Fitting Toolbox . and yes, i am encountering the first issue, but not the second one.
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.
so, this is the brief of the rationale.

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?
but i suppose i can't avoid this.
still i wonder why it takes longer time to compute
pf = 8.32/(10^7)*myx^9-5.217/(10^5)*myx^8+...
than it takes to compute
pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...
even if at the moment the numerical coefficients have the same accuracy as the symbolic ones.

thanks a lot for assistance,
Orchy

Subject: extract the exact value from an approximated variable

From: someone

Date: 29 Dec, 2008 22:32:01

Message: 7 of 8

"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbhvh$9h$1@fred.mathworks.com>...
> "Jiro Doke" <jiro.doke@mathworks.com> wrote in message <gjbgeq$sej$1@fred.mathworks.com>...
> > "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbdu6$ocp$1@fred.mathworks.com>...
> > > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjbcj3$2j5$1@fred.mathworks.com>...
> > > > "Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbb62$7dg$1@fred.mathworks.com>...
> > > > > hi, i am looking for a way to replicate the exact values that i get from a variable in matlab.
> > > > > 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.
> > > > > supposing the case:
> > > > > Linear model Poly9:
> > > > > fm1(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 +
> > > > > p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10
> > > > > Coefficients (with 95% confidence bounds):
> > > > > p1 = 8.32e-007 (8.257e-007, 8.383e-007)
> > > > > p2 = -5.217e-005 (-5.25e-005, -5.185e-005)
> > > > > p3 = 0.001308 (0.001301, 0.001315)
> > > > > p4 = -0.01684 (-0.01693, -0.01676)
> > > > > p5 = 0.1189 (0.1183, 0.1195)
> > > > > p6 = -0.4585 (-0.461, -0.456)
> > > > > p7 = 0.9485 (0.9422, 0.9547)
> > > > > p8 = -1.185 (-1.193, -1.176)
> > > > > p9 = 1.063 (1.058, 1.068)
> > > > > p10 = -0.5639 (-0.565, -0.5629)
> > > > >
> > > > > i get different result if i use fm1.p1 than if i use 8.32/10^7.
> > > > > 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?
> > > > > thank you so very much!
> > > > >
> > > > > Orchy
> > > >
> > > > 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.
> > > >
> > > > Roger Stafford
> > >
> > > - Hi, Roger.
> > > i'm not sure i get your point. is the type of fit relevant for my issue?
> > > i use polynomial of 9 degree to fit my data whose size varies.
> > > i used the data to obtain the fit function using 9 degree polynomial. further, i want to make use of the polynomial i get.
> > > instead of using the expression
> > > pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...
> > > i want to use
> > > pf = 8.32/(10^7)*myx^9-5.217/(10^5)*myx^8+...
> > >
> > > 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).
> > > Orchy
> >
> > Orchy,
> >
> > It looks like your are talking about the output that comes out for the "fit" function from the Curve Fitting Toolbox.
> >
> > 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).
> >
> > 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 hard-coding the results, so if you were to reuse your code on a different data set, you have to re-write your code. Is there a reason you cannot use the variable form (the first expression you showed)?
> >
> > If you can answer me these questions, then we can try to address your issue.
> >
> > jiro
>
> hi Jiro,
> yes, i am using the result from the Curve Fitting Toolbox . and yes, i am encountering the first issue, but not the second one.
> 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.
> so, this is the brief of the rationale.
>
> 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?
> but i suppose i can't avoid this.
> still i wonder why it takes longer time to compute
> pf = 8.32/(10^7)*myx^9-5.217/(10^5)*myx^8+...
> than it takes to compute
> pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...
> even if at the moment the numerical coefficients have the same accuracy as the symbolic ones.
>
> thanks a lot for assistance,
> Orchy

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 m-file.

In the second case it simply retrieves the fm1.p1, etc. values from storage and uses it.

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?

But again, it depends on the specifics of the MATLAB parser, I could be wrong!

Subject: extract the exact value from an approximated variable

From: Jiro Doke

Date: 29 Dec, 2008 22:52:02

Message: 8 of 8

"Orchid Bee" <orchidbee08@yahoo.com> wrote in message <gjbhvh$9h$1@fred.mathworks.com>...
> hi Jiro,
> yes, i am using the result from the Curve Fitting Toolbox . and yes, i am encountering the first issue, but not the second one.
> 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.
> so, this is the brief of the rationale.
>
> 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?
> but i suppose i can't avoid this.
> still i wonder why it takes longer time to compute
> pf = 8.32/(10^7)*myx^9-5.217/(10^5)*myx^8+...
> than it takes to compute
> pf = fm1.p1.*myx^9+fm1.p2.*myx^8+ ...
> even if at the moment the numerical coefficients have the same accuracy as the symbolic ones.
>
> thanks a lot for assistance,
> Orchy

Hi Orchy,

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:

pf = fm1(myx)

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 1e-7. Take a look at this:

>> tic;for ii = 1:10000000,a=3e-7;end;toc
Elapsed time is 0.036858 seconds.
>> tic;for ii = 1:10000000,b=3/(10^7);end;toc
Elapsed time is 2.888624 seconds.

The part about "write in C code" may make it a bit tricky. This is a bit off-topic 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 Real-Time 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:

>> x = 1:10;
>> y = rand(1,10);
>> p = polyfit(x,y,5) % 5-th order polynomial
p =
   -0.0017 0.0522 -0.5745 2.8134 -5.9002 4.5816
>> newY = polyval(p, [1.3, 5.7, 9.5])
newY =
    0.5465 0.6208 0.5614

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us