finding mathematical function of set of points
Show older comments
There are some points on X-Y coordinates. Is it possible in MATLAB to find mathematical function between X and Y?
for example for X=1,3,5,7,8,9,23,25,30 respectively Y is equals to Y=1,3,5,7,9,12,13,17,20
now how MATLAB could find relation between X and Y in mathematical function form? ( finding Y=f(X) )
mathematical function must be algebra in this form: y=a+bx^2+cx^3+dx^4+...nx^K (Not sinusoidal and etc)
that a,b,...,n and K are unknown. Is it possible in MATLAB to find this parameters?
Accepted Answer
More Answers (3)
plot your data by below format:
X=[1,3,5,7,8,9,23,25,30]
Y=[1,3,5,7,9,12,13,17,20]
plot(X,Y)
then from ---tools find the ---Basic Fitting by activation the --show equation and select one or more fitting functions you will see the F(x) just on the your plot figure.

1 Comment
friendly nadeem
on 5 Feb 2018
Thank You
Image Analyst
on 20 Nov 2011
3 votes
How about using Lagrange Interpolation: http://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html
3 Comments
mohammad
on 20 Nov 2011
mohammad
on 20 Nov 2011
Image Analyst
on 20 Nov 2011
Well polyfit should do it - give you the Lagrange Interpolation coefficients - AS LONG AS the order of the polynomial is exactly equal to the number of coordinate points that you have. Otherwise if you put in an order less than the number of points, it's a regression and won't go through your points exactly. Make sense? See my code as an answer. I put it there so I could properly format it as code.
Image Analyst
on 20 Nov 2011
Try this:
fontSize = 16;
% Generate the sample data.
X=[1,3,5,7,8,9,23,25,30]
Y=[1,3,5,7,9,12,13,17,20]
% Find the coefficients.
coeffs = polyfit(X, Y, length(Y)+1)
plot(X, Y, 'ro', 'MarkerSize', 10);
% Make a finer sampling so we can see what it
% does in between the training points.
interpolatedX = linspace(min(X), max(X), 500);
interpolatedY = polyval(coeffs, interpolatedX);
% Plot the interpolated points.
hold on;
plot(interpolatedX, interpolatedY, 'b-', 'LineWidth', 3);
grid on;
title('Interpolating Polynomial', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Be aware of the drawback of using such a high order polynomial. Yes it will go through all your points but with such a high order, the oscillations between your training points grow wildly and the estimated values there are less reliable. You can see this on the plot given by the above code. Look what happens between 10 and 20 and between 25 and 30 - it goes crazy.
3 Comments
mohammad
on 20 Nov 2011
rahman sajadi
on 18 Aug 2015
this code gives a garph of points only, please help me to find out mathmatical function between X and Y
Walter Roberson
on 18 Aug 2015
The variable coeffs gives the coefficients of the polynomial, highest degree first. For example if the coeffs was 7 4 13 -6 then it would represent 7*x^3 + 4*x^2 + 13*x^1 - 6
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!