From: Alex Pereira on
Hi All,
I've created a function with cfit, with the code shown bellow. Now I need to find the x value for a given y value.

I know how to evaluate y values with this command y=feval(func, x). is it possible to create inverse functions ? As much as I remember, this function has no inverse form.

I can't even imagine how to do it. Does anyone knows ?

Thank you,
Alex

a=[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 ];
b=[1:size(a,2)];
b = b(:);
a = a(:);
fo=fitoptions('method','SmoothingSpline','SmoothingParam',0.010000000000000000555);
ft = fittype('smoothingspline');
func = fit(b,a,ft,fo);
From: John D'Errico on
"Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message <i1o94h$51j$1(a)fred.mathworks.com>...
> Hi All,
> I've created a function with cfit, with the code shown bellow. Now I need to find the x value for a given y value.
>
> I know how to evaluate y values with this command y=feval(func, x). is it possible to create inverse functions ? As much as I remember, this function has no inverse form.
>
> I can't even imagine how to do it. Does anyone knows ?
>
> Thank you,
> Alex
>
> a=[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 ];
> b=[1:size(a,2)];
> b = b(:);
> a = a(:);
> fo=fitoptions('method','SmoothingSpline','SmoothingParam',0.010000000000000000555);
> ft = fittype('smoothingspline');
> func = fit(b,a,ft,fo);

hint:

help fzero

I know, your next question will be that fzero looks for
a point where the function is ZERO. But you want to
find where it equals a given value. But you CAN
subtract the desired value from your function. Then
fzero would indeed be looking for a root.

Of course, there are multiple solutions. fzero will find
only one of them.

John
From: Alex Pereira on
"John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <i1oado$pv2$1(a)fred.mathworks.com>...
> "Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message <i1o94h$51j$1(a)fred.mathworks.com>...
> > Hi All,
> > I've created a function with cfit, with the code shown bellow. Now I need to find the x value for a given y value.
> >
> > I know how to evaluate y values with this command y=feval(func, x). is it possible to create inverse functions ? As much as I remember, this function has no inverse form.
> >
> > I can't even imagine how to do it. Does anyone knows ?
> >
> > Thank you,
> > Alex
> >
> > a=[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 ];
> > b=[1:size(a,2)];
> > b = b(:);
> > a = a(:);
> > fo=fitoptions('method','SmoothingSpline','SmoothingParam',0.010000000000000000555);
> > ft = fittype('smoothingspline');
> > func = fit(b,a,ft,fo);
>
> hint:
>
> help fzero
>
> I know, your next question will be that fzero looks for
> a point where the function is ZERO. But you want to
> find where it equals a given value. But you CAN
> subtract the desired value from your function. Then
> fzero would indeed be looking for a root.
>
> Of course, there are multiple solutions. fzero will find
> only one of them.
>
> John
Dear John,
It will work.

Thank you very much.
Alex
From: Alex Pereira on
"Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message <i1ocne$lhb$1(a)fred.mathworks.com>...
> "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <i1oado$pv2$1(a)fred.mathworks.com>...
> > "Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message <i1o94h$51j$1(a)fred.mathworks.com>...
> > > Hi All,
> > > I've created a function with cfit, with the code shown bellow. Now I need to find the x value for a given y value.
> > >
> > > I know how to evaluate y values with this command y=feval(func, x). is it possible to create inverse functions ? As much as I remember, this function has no inverse form.
> > >
> > > I can't even imagine how to do it. Does anyone knows ?
> > >
> > > Thank you,
> > > Alex
> > >
> > > a=[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 ];
> > > b=[1:size(a,2)];
> > > b = b(:);
> > > a = a(:);
> > > fo=fitoptions('method','SmoothingSpline','SmoothingParam',0.010000000000000000555);
> > > ft = fittype('smoothingspline');
> > > func = fit(b,a,ft,fo);
> >
> > hint:
> >
> > help fzero
> >
> > I know, your next question will be that fzero looks for
> > a point where the function is ZERO. But you want to
> > find where it equals a given value. But you CAN
> > subtract the desired value from your function. Then
> > fzero would indeed be looking for a root.
> >
> > Of course, there are multiple solutions. fzero will find
> > only one of them.
> >
> > John
> Dear John,
> It will work.
>
> Thank you very much.
> Alex

Dear John,

Do you know how can I subtract a scalar from the function returned by the fit function ?

Thank you,
Alex
From: Steven Lord on

"Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message
news:i1ptet$6gk$1(a)fred.mathworks.com...
> "Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message
> <i1ocne$lhb$1(a)fred.mathworks.com>...
>> "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message
>> <i1oado$pv2$1(a)fred.mathworks.com>...

*snip*

> Dear John,
>
> Do you know how can I subtract a scalar from the function returned by the
> fit function ?

You can't subtract a scalar from a fit object -- subtraction isn't defined
for fit objects. What you can do is _evaluate_ the fit object and subtract
a scalar from the result.

Using the example from the reference page for FIT to try to find when the US
population was 100 million:

http://www.mathworks.com/access/helpdesk/help/toolbox/curvefit/fit.html


% Load data
load census

% Perform the fit
s = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,max(cdate)],...
'Startpoint',[1 1]);
f = fittype('a*(x-b)^n','problem','n','options',s);
[c2,gof2] = fit(cdate,pop,f,'problem',2);

% Determine when the population was 100
desiredPopulation = 100;
objective = @(date) c2(date) - desiredPopulation;
desiredDate = fzero(objective, 1900)

% Evaluate the fit at the calculated time
populationOnThatDate = c2(desiredDate)

% Plot it
plot(cdate, pop, 'k-', desiredDate, c2(desiredDate), 'r+')


--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com