finding mathematical function of set of points
115 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Walter Roberson
2011-11-20
There are literally an infinite number of mathematical functions that fit any finite set of (X,Y) pairs exactly. Unless you have some prior information as to what the form of the mathematical function "should be", there is no way of distinguishing which of those infinite number of functions is the "right" one.
6 个评论
Sofía Carrillo
2015-3-10
@WalterRoberson is there a demonstration of why there are infinite functions that contain the given points? Or how do you know so?
Image Analyst
2015-3-10
Let's take a simple case: two points. Can you find a line that goes through them? Sure. How about a parabola? Sure, lot of them - an infinite number of them. What about a cubic? Of course? Can you find a 4th order polynomial? A 5th, a 6th, a 7th order polynomial? Of course you can. What about a circle that touches the two points? Yes. How about an ellipse or hyperbola? Yes and Yes. Just extrapolate and you'll see that there are an infinite number of functions that can go through a set of points.
更多回答(3 个)
Image Analyst
2011-11-20
How about using Lagrange Interpolation: http://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html
3 个评论
Image Analyst
2011-11-20
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
2011-11-20
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 个评论
rahman sajadi
2015-8-18
this code gives a garph of points only, please help me to find out mathmatical function between X and Y
Walter Roberson
2015-8-18
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!