How to plot the real function not just points in matlab?

3 views (last 30 days)
I am trying to plot the function that is defined by the points (xi,yi), but I am just getting a plot of the points that i gave, connected through lines.
here is my function:
function c = interpolation(x, y)
n = length(x);
V = ones(n);
for j = n:-1:2
V(:,j-1) = x.*V(:,j);
end
c = V \ y;
disp(V)
for i = 0:n-1
fprintf('c%d= %.3f\n', i, c(i+1));
end
plot (x,y)
hold on
plot (x,y,'o')
end
how to get the real curve? is the a possibility to plot the real curve, plus that points noted as 'o' in the plot?
Thank you in advance
%

Accepted Answer

Mike Garrity
Mike Garrity on 9 Nov 2015
That's all true, but we should probably stop to tell Karim that MATLAB does have a lot of tools for evaluating points on some of those many interesting functions.
For example, you might want to look at interp1:
x = 1:5;
y = randn(1,5);
plot(x,y,'-o')
xhigh = linspace(1,5,100);
yhigh = interp1(x,y,xhigh,'spline');
hold on
plot(xhigh,yhigh)
  4 Comments
Walter Roberson
Walter Roberson on 9 Nov 2015
In my experience, spline is physically realistic only for cases involving bending of physical elements, which is something I never happen to do. For anything involving electronics, chemical reactions, gravity and the other fundamental forces, statistics, or a heck of a lot of other things, using spline is a placebo (the grammatical root of which is "it pleases") that is nice to look at but distorts understanding of the underlying physical or mathematical properties. I rarely rarely find it appropriate to use spline.
I recognize that there are other people (hi John!) who find a multitude of uses for spline that they are happy with: this is my opinion.
Mike Garrity
Mike Garrity on 9 Nov 2015
I agree completely.
It's very stiff. As you can see in that picture, it's overshooting the input values by quite a bit. That can be extremely problematic in some situations. I certainly didn't mean to imply that spline was a particularly good choice for Karim's situation. I was just trying to give a concrete starting point for exploring the wonderful world of interpolation.
I'm hoping that Karim will tell us more about the use case so that we can provide more suggestions.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 8 Nov 2015
No. "real curves" have Aleph 1 points in them, the infinity of real numbers. You cannot plot a "real" curve with any finite set of points. MATLAB can only ever generate a finite set of points when operating with a finite amount of memory in a finite time. Therefore MATLAB cannot be used to plot "real" curves, only approximations of curves.
  3 Comments
Stephen23
Stephen23 on 8 Nov 2015
Edited: Stephen23 on 8 Nov 2015
What is the "real function"? We can see in the screenshot that you are plotting this by calling your MATLAB function with two vectors of four values each:
v1 = [-1,0,1,2];
v2 = [5,-2,9,-4];
And these are exactly the four points that are plotted.
So you define four points in space, and do not define any function between them. There are exactly infinite functions that will exactly fit those four points (or any finite set of points), so which one of these infinite possibilities would you select as being the "real function"?
It seems that perhaps you are asking about interpolation or perhaps smoothing, which any internet search engine can tell you about.
Walter Roberson
Walter Roberson on 9 Nov 2015
You have to be careful with the wording there, Stephen. "exactly infinite functions" implies the ability to compare infinities for equality, which you can do for countable infinity but countable infinity is not equal to uncountable infinity. The number of functions that can fit any finite set of points is uncountable infinity.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!