How can I plot various polynomials along with some coordinate pairs in the same graph?

18 views (last 30 days)
For my matlab introductory course we were tasked to plot various coordinate pairs as well as the polynomials passing through those points so we could "draw" a figure on the plot chart. I made a test code in which I took 3 pairs of coordinate points, generated a polynomial passing trought those 3 points, plotted that and then repeated the process with another 3 pairs of coordinated points (Im doing it with 3 pairs of coordinates at a time so i can generate a polynomial per coordinate set):
%coordinate set 1
x = [0.51 1.29 2.36];
y = [0.99 1.61 2.02];
p = polyfit(x,y,2);
lx = 0.51:.1:2.36;
ly = polyval(p,lx);
%coordinate set 2
x1 = [2.36 7.37 12.12];
y1 = [2.02 2.08 2.03];
p1 = polyfit(x1,y1,2);
lx1 = 2.36:.1:12.12;
ly1 = polyval(p1,lx1);
%Plot both polynomials in 1 graph
plot(x,y,'o',lx,ly,'o',x1,y1,'o',lx1,ly1)
grid on
text(2,400,s)
%Generated polynomial display
s = sprintf('y = (%.1f) x^3 + (%.1f) x^2 + (%.1f) x',p(1),p(2),p(3));
s1 = sprintf('y = (%.1f) x^3 + (%.1f) x^2 + (%.1f) x',p1(1),p1(2),p1(3));
disp(s);
disp(s1);
When it plots those elements, the graph appears to go crazy. Im planning to do this with more than 2 coordinate sets. I'm trying to do some sort of cubic spline but without utilizing the spline function.
Thanks in advanced for your suggestions, comments and knowledge.

Accepted Answer

Voss
Voss on 27 Mar 2022
I imagine you said the graph goes crazy because the points where the first polynomial is evaluated (i.e., (lx, ly)) was plotted with marker 'o'. If instead you use marker 'o' for the 3 points that are used to fit the polynomials and use a solid line ('-') for the points where the polynomials are evaluated, I think it looks ok:
%coordinate set 1
x = [0.51 1.29 2.36];
y = [0.99 1.61 2.02];
p = polyfit(x,y,2);
lx = 0.51:.1:2.36;
ly = polyval(p,lx);
%coordinate set 2
x1 = [2.36 7.37 12.12];
y1 = [2.02 2.08 2.03];
p1 = polyfit(x1,y1,2);
lx1 = 2.36:.1:12.12;
ly1 = polyval(p1,lx1);
%Plot both polynomials in 1 graph
plot(x,y,'o',lx,ly,'-',x1,y1,'o',lx1,ly1,'-')
grid on
%Generated polynomial display
% s = sprintf('y = (%.1f) x^3 + (%.1f) x^2 + (%.1f) x',p(1),p(2),p(3));
% s1 = sprintf('y = (%.1f) x^3 + (%.1f) x^2 + (%.1f) x',p1(1),p1(2),p1(3));
s = sprintf('y = (%f) x^3 + (%f) x^2 + (%f) x',p(1),p(2),p(3));
s1 = sprintf('y = (%f) x^3 + (%f) x^2 + (%f) x',p1(1),p1(2),p1(3));
% text(2,400,s)
text(mean(x),mean(y),s)
text(mean(x1),mean(y1),s1)
disp(s);
y = (-0.222537) x^3 + (1.195439) x^2 + (0.438208) x
disp(s1);
y = (-0.002306) x^3 + (0.034409) x^2 + (1.951635) x
  2 Comments
Voss
Voss on 27 Mar 2022
You're welcome!
You can specify the line color, line style, and marker in plot like that. For instance:
hold on
plot([1 2 3],[7 6 5],'rs') % red (r) square (s) markers, no line
plot([1 2 3],[6 5 4],'bo') % blue (b) circle (o) markers, no line
plot([1 2 3],[5 4 3],'-bo') % blue (b) circle (o) markers, solid line (-)
plot([1 2 3],[4 3 2],'--kv') % black (k) triangle (v) markers, dashed line (--)
% etc., many possible combinations

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!