I have a graph with the approximate shape of 1/x type. I wish to fit it with the equation 1/(x-A) +B . How can I fit a particular equation onto it rather than using the basic fitting tools?

Answer by John D'Errico
on 14 Mar 2019

Edited by John D'Errico
on 15 Mar 2019

Accepted Answer

Easiest is if you just use the curvefitting toolbox, OR the stats toolbox, OR the optimization toolbox.

Still easy enough to use fminsearch.

% practice data.

x = 0:100;

y = 1./(x + 17) + pi + randn(size(x))/500;

plot(x,y,'.')

Now fit it.

% objective function for fminsearch

obj = @(AB) norm(y - (1./(x + AB(1)) + AB(2)));

AB0 = [5,5];

[AB,fval] = fminsearch(obj,AB0)

AB =

16.907 3.1416

fval =

0.017046

Which seems reasonable.

hold on

plot(x,1./(x + AB(1)) + AB(2),'-r')

Will this fit solve your problem? I predict it will not do so,and your next question will be to ask how to make your equation fit your data better. At least, that is what usually happens on this sort of question. Why?

Because you have chosen a VERY specific model form,and we have not seen the data you have to know if that model form is a valid one for your data. You say only that your data has an approximate 1/x shape. Yeah, right. Not enough information. Why not?

That requires I talk some about the model you chose, and what it represents. The model

y = 1/(x+A) + B

can be re-written simply as

(x+A)*(y-B) = 1

What can we gain from that form, since it converts a simple functional form into an implicit form? The equation

x*y = 1

is the classic equation of a hyperbolic arc, one that has explicitly horizontal and vertical asymptotes. Thus, the latter form has y-->0 as x-->inf, and y-->inf as x--> 0 from above.

The form you have chosen is the same equation, except the asymptotes are translated. So we get a horizontal asymptote at y=B, where X must go to inf. That is, when y==b, the implicit equation reduces to

(x+A)*0 = 1

There is no finite x that solves it. Likewise, at x==-A, there is again a problem, since the problem reduces to

0*(Y-B) = 1

Again, no finite value of y produces a valid result here. That is, we have a singularity.

So the form you have chosen has a VERY specific behavior. But, even more than that, you still have a problem. You say only that your data looks sort of like y = 1/x. But what you don't understand I think is that there are MANY sets of data that would look like that, yet your model form will fail to fit it.

For example, suppose I gave you some simple data, with no noise at all in it.

x = 1:.2:25;

y = 2./x;

plot(x,y,'o')

I could have chosen as much data as you wish. But this will be easy to plot and look at. But, can you fit it with that model form? I mean, it looks like 1/x.

What will happen is the model you have chosen is inadequate. It will show lack of fit. There is nothing you can do to make the two forms fit up.

y = 1/(x+A) + B = 2/x

No choice of A and B will give the correct fit, since by changing A or B, we change only the location of the asymptotes!

obj = @(AB) norm(y - (1./(x + AB(1)) + AB(2)));

AB0 = [5,5];

[AB,fval] = fminsearch(obj,AB0)

AB =

-0.53389 0.10157

fval =

0.84217

hold on

plot(x,1./(x + AB(1)) + AB(2),'-r')

So there is significant lack of it. Your model is inadequate to fit that data, because changing the asymptote locations is not what was needed. Had I posed a model that looked like

y = C/(x + A) + B

NOW I could have fit a model that fit my data exactly. The result from the fit would have been

A = 0

B = 0

C = 2

(Do I really need to do that fit to prove my point? No, I hope not.)

My point is, even though your data looks vaguely like 1/x, that does not mean you have chosen even remotely the correct model. Without seeing your data, OR some valid reason to believe your claim that it looks like 1/x, all I can suggest is as I did before, that even though I've shown you how to fit your data to this model, in the end you will probably be disappointed. After all, 1/x LOOKS an awful lot like 2/x. Sigh.

로그인 to comment.

Opportunities for recent engineering grads.

Apply Today
## 댓글 수: 1

## Jim Riggs (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/449932-fitting-an-equation-on-to-graph#comment_681295

로그인 to comment.