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:
Inverse evaluation - Give x value and get back the y value

Subject: Inverse evaluation - Give x value and get back the y value

From: Alex Pereira

Date: 16 Jul, 2010 00:29:05

Message: 1 of 5

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

Subject: Inverse evaluation - Give x value and get back the y value

From: John D'Errico

Date: 16 Jul, 2010 00:51:04

Message: 2 of 5

"Alex Pereira" <alexlopespereira@gmail.com> wrote in message <i1o94h$51j$1@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

Subject: Inverse evaluation - Give x value and get back the y value

From: Alex Pereira

Date: 16 Jul, 2010 01:30:22

Message: 3 of 5

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <i1oado$pv2$1@fred.mathworks.com>...
> "Alex Pereira" <alexlopespereira@gmail.com> wrote in message <i1o94h$51j$1@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

Subject: Inverse evaluation - Give x value and get back the y value

From: Alex Pereira

Date: 16 Jul, 2010 15:22:05

Message: 4 of 5

"Alex Pereira" <alexlopespereira@gmail.com> wrote in message <i1ocne$lhb$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <i1oado$pv2$1@fred.mathworks.com>...
> > "Alex Pereira" <alexlopespereira@gmail.com> wrote in message <i1o94h$51j$1@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

Subject: Inverse evaluation - Give x value and get back the y value

From: Steven Lord

Date: 16 Jul, 2010 17:39:51

Message: 5 of 5


"Alex Pereira" <alexlopespereira@gmail.com> wrote in message
news:i1ptet$6gk$1@fred.mathworks.com...
> "Alex Pereira" <alexlopespereira@gmail.com> wrote in message
> <i1ocne$lhb$1@fred.mathworks.com>...
>> "John D'Errico" <woodchips@rochester.rr.com> wrote in message
>> <i1oado$pv2$1@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@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

Tags for this Thread

No tags are associated with 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