PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Fitting formula to graph

Started by charliecoutas, Dec 29, 2021, 11:40 AM

Previous topic - Next topic


I need  little help please. Attached is a graph of my constant power pulse generator.

The curve looks like a square law but I need to work out a formula for it.
I need to work out (approx) what value I will need on the horizontal axis to obtain the matching value on the vertical axis.
Or how much drive is needed (horiz axis) to generate the milliwatts (vert axis).




Charlie, this looks like Excel data - you have that option in excel built-in.

John Drew

Hi Charlie, I use an Excel spreadsheet.
I put the X and the Y values in two columns. Then plot the graph, then access the polynomial calculation.
Once a polynomial is created you have your formula. You might need to go to at least x cubed for a good match.
It works beautifully.
If you need more help I'll do it via email.

PS I'm on my phone so can't look up the method but from memory right click with cursor on the line of the graph. Choose from options.


If you don't want to use the excel function just use this online one:

I guessed your values off the chart and a second order polynomial comes out : y = 3.343699 + 0.01234755*x + 0.00004668034*x^2

X          Y
0          0
500     30
1000   55
1500  130
2000  210
2200  260


Guys: Thank you so much. I didn't know there were automatic ways of doing it. You've saved me a lot of fiddling around.



As your curve is simple, you could consider just using a lookup table to simplify the math overhead


I considered that Jon, but there will be several different curves to store. The overhead of doing the calculations is no problem as it's only done before a run, to determine the starting value. In any case, Les's floating point code running on a dspPIC33CK at 200MHz runs like a bat out of hell anyway!


John Drew

The 64bit floats work really well in the 16bit PICs. 32 bit floats will compromise things but I'm sure you thought of that.


I hadn't thought of that John, but thanks for pointing it out, changed all my Floats to Doubles. I have got to grips with generating the formula for the curve that my hardware is producing. I am using your method, two columns in Excel then click the + sign top right of chart and select polynomial trendline. Choose order of polynomial that gives required accuracy. Good stuff.

Off to Tenerife for a weeks sun tomorrow morning, let's hope we only come back with a little suntan and nothing else!

Happy New Year to all.