Polynomials, Curve Fitting, and Interpolation. Numerical Computing with . MATLAB for Scientists and Engineers. You will be able to. Use MATLAB to manipulate polynomials for calculating values, derivatives and for multiplications as well as divisions,
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Polynomials, Curve Fitting, and Interpolation
Numerical Computing
with .
MATLAB
for Scientists and Engineers
polynomial of degree 5
polynomial of degree 2
polynomial of degree 1
p = [ 5 0 0 6 4 2];
d = [ 2 -4 20];
p = [ -4 12];
y = polyval(p, x);
18
p = [ 5 0 0 6 4 2];
x = 0;
y = polyval(p,x)
x = [0 1];
y = polyval(p,x)
x = -1:0.01:1;
figure, plot(x, polyval(p,x));
16
14
12
10
8
6
4
2
0
-2
-1
-0.5
0
0.5
1
p = [1 0 -5 0 4 0];
x = -2.1:0.01:2.1;
% Plot the polynomial
figure(1), plot(x, polyval(p, x), 'LineWidth',2);
hold on; plot([-2.5 2.5], [0 0]);
% Find its roots and plot them using markers.
r = roots(p); ry = zeros(size(r));
h = plot(r,ry,'ro', 'MarkerSize', 10, 'LineWidth', 2);
% Display the polynomial in mathematical terms.
t = text(-0.5, 2, '{\itf}({\itx})\it=x^5-5x^3+4x');
set(t,'FontName', 'Times', 'FontSize', 16);
p1 = [1 0 -5 0 4 0];
p2 = [0 0 0 1 2 0];
pa = p1 + p2
pm = conv(p1, p2)
[q r] = deconv(p1, p2)
p1 = [1 0 -5 0 4 0];
p1d = polyder(p1)
p2 = [1 2 0];
pd = polyder(p1, p2)
[n d ] = polyder(p1, p2)
When the density
of the glass is given
as ,
plot the mass of the
cup as a function of
the thickness x.
What is the thickness x, when the mass of
the cup is 400g.
8cm
x cm
20cm
5x cm
glass_cup.m
(1,1)
(2,0)
(-2,-1)
poly_3points.m
px = [-2 1 2];
py = [-1 1 0];
A = [4 -2 1; 1 1 1; 4 2 1];
c = inv(A) * py';
x = -3:0.01:3;
y = polyval(c,x);
figure(1);
h = plot(x,y,'b-',px,py,'ro','LineWidth',2);
grid;
set(h(2),'MarkerSize',10);
2
1.5
Solution
1
0.5
0
-0.5
p = polyfit(x,y,n)
-1
-1.5
-2
-3
-2
-1
0
1
2
3
polyfit_3points.m
px = [-2 1 2];
py = [-1 1 0];
p = polyfit(px, py, 1);
x = -3:0.01:3;
y = polyval(p,x);
figure(1);
h = plot(x,y,'b-',px,py,'ro','LineWidth',2);
grid;
set(h(2),'MarkerSize',10);
hold on;
plot([-3 3],[0 0],'r-',[0 0], [-2 2],'r-');
hold off;
polyfit_6points.m
%% Experimental Data
x = [1 3 4 6 8 10];
y = 2.5*x+2+4*randn(size(x));
figure(1);
plot(x,y,'ro','LineWidth',2,'MarkerSize',10);
grid; hold on; axis([0 11 -5 30]);
%% Polyfit using polynomials
x1 = 0:0.01:11;
linespec = ['b-','r-','k-'];
forn=1:2:5
p = polyfit(x,y,n);
y1 = polyval(p,x1);
plot(x1,y1,linespec(n), 'LineWidth', 2);
end
hold off;
legend('Experiment','1st order poly', '3rd order poly',...
'5th order poly', 'Location', 'SouthEast');
x = 0:0.5:5;
y = [3.50 2.69 2.02 1.74 1.17 1.05 0.89 0.63 0.50 0.35 0.15];
unknown_fit.m
x = 0:0.5:5;
y = [3.50 2.69 2.02 1.74 1.17 1.05 0.89 0.63 0.50 0.35 0.15];
figure(1);
plot(x,y,'ro','LineWidth',2,'MarkerSize',10);
grid; hold on; axis([0 5 0 3.6]);
...
best_fit.m
x = [1.0 1.8 2.9 4.1 4.6 5.4 7.1 7.6 8.2 10.0];
y = [2.00 1.35 0.93 0.70 0.63 0.55 0.43 0.40 0.38 0.31];
figure(1);
plot(x,y,'ro','LineWidth',2,'MarkerSize',10);
grid; hold on;
xlabel('x'); ylabel('y');
...
yi = interp1( x, y, xi, 'method' )
interp1_demo.m
x = [0.1 0.2 0.4 0.5 0.9 1.0];
y = x .* sin(pi*x);
figure(1);
plot(x,y,'ro','LineWidth',2,'MarkerSize',10);
grid; hold on;
xlabel('x'); ylabel('y');
xi = 0:0.01:1;
yi_n = interp1(x,y,xi,'nearest');
yi_l = interp1(x,y,xi,'linear');
yi_s = interp1(x,y,xi,'spline');
yi_c = interp1(x,y,xi,'pchip');
plot(xi,yi_n,xi,yi_l,xi,yi_s,xi,yi_c,'LineWidth',2);
legend('Exp','Nearest','Linear','Spline','Cubic', ...
'Location','NorthWest');