Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: extract the exact value from an approximated variable
Date: Mon, 29 Dec 2008 22:52:02 +0000 (UTC)
Organization: The MathWorks Inc
Lines: 42
Message-ID: <gjbkai$mr9$1@fred.mathworks.com>
References: <gjbb62$7dg$1@fred.mathworks.com> <gjbcj3$2j5$1@fred.mathworks.com> <gjbdu6$ocp$1@fred.mathworks.com> <gjbgeq$sej$1@fred.mathworks.com> <gjbhvh$9h$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1230591122 23401 172.30.248.37 (29 Dec 2008 22:52:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 29 Dec 2008 22:52:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869871
Xref: news.mathworks.com comp.soft-sys.matlab:509151


"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