How to find local minimum of fit polynomial

13 views (last 30 days)
I have some stock data (time and price).
I log the data, and then use fit function to fit a polynomial onto it
then i make a function f
This is shown in the three lines of code below
date_log10 = log10(datenum(T2.Time)); price_log10 = log10(T2.Close);
[f_mdl,gof,output] = fit(date_log10,price_log10,'poly1','Normalize','on','Robust','Bisquare');
f(j) = 10.^f_mdl(log10(datenum(T2.Time(end))));
My question is, how to i now find the local minimum of this function f?
I was thinking i would use fminsearch, but just unsure how i would make use of that.
  2 Comments
Rizwan Khan
Rizwan Khan on 1 May 2022
Dear Sir,
Thanks for the quick response, while you're there, can i quickly ask, where are you downloading the data from, are you able to share the code?
Secondly, in relation to my query, although the model is linear 1, it actually then chagnes the log back and eventually is equivialent to poly3.
So even when i plot it, i get a polynomial, and my question is how do i find the local minimum and maximum.
So as an example, if you look at your data, and change your linear orange line to a polynomial of degree 3, then you will see that it turns at the top, now how can you find the local minimum of that, i believe this required fminsearch or fmin function.
The eventual goal is to draw a polynomial, and then find the most recent turning point, so that may not be the very extreme (global) min or max value of the chart, as the charts goes up and then down and so on, the these local peaks and trophs keep coming, and i want to find the most recent local min or local max
I think the answer is in using fmin or fminsearch function, but i'm unsure how to exactly do that.
Thanks again so much for your response

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 30 Apr 2022
I use polyfit() instead of fit(). Have you gotten a fitted y value vector yet? If so, just use min
miny = min(yFitted(index1 : index2))
  4 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 30 Apr 2022
poly1 is of the form a*x+b for some model parameters a and b. The minimum of a straight line is:
  • at the lower bound of x if a is positive
  • at the upper bound of x if a is negative
  2 Comments
Walter Roberson
Walter Roberson on 1 May 2022
If you take 10 to the power of a linear fit, then the result will still have its maximum at either the upper bound or the lower bound. It will not turn into a cubic!!
If you had the coefficients p for a cubic then use roots(polyder(p)) to find the locations of the critical points

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by