MATLAB Code: Polynomial and Linear Fit for Wind
Tunnel Test
clc; clear; close all;
% Data
V = [Link];
F = [25 70 380 550 610 1220 830 1450];
%% Linear fit
[a_lin, rsq_lin] = Linrag(V, F); % your Linrag function
xp_lin = V;
yp_lin = a_lin(1) + a_lin(2) * xp_lin;
%% 2nd to 5th order polynomial fits
v1 = V'; f1 = F'; % column vectors
sf = sum(F);
% 2nd order poly
z2 = [ones(size(v1)) v1 v1.^2];
a2 = z2\f1;
yp2 = a2(1) + a2(2)*V + a2(3)*V.^2;
% 3rd order poly
z3 = [ones(size(v1)) v1 v1.^2 v1.^3];
a3 = z3\f1;
yp3 = a3(1) + a3(2)*V + a3(3)*V.^2 + a3(4)*V.^3;
% 4th order poly
z4 = [ones(size(v1)) v1 v1.^2 v1.^3 v1.^4];
a4 = z4\f1;
yp4 = a4(1) + a4(2)*V + a4(3)*V.^2 + a4(4)*V.^3 + a4(5)*V.^4;
% 5th order polyfit/polyval
a5 = polyfit(V, F, 5);
yp5 = polyval(a5, V);
%% Plot
figure
plot(V, F, 'o', 'MarkerFaceColor', 'k'); hold on
plot(xp_lin, yp_lin, '-b', 'LineWidth', 1.5)
plot(V, yp2, ':r', 'LineWidth', 1.5)
plot(V, yp3, '--g', 'LineWidth', 1.5)
plot(V, yp4, '-.m', 'LineWidth', 1.5)
plot(V, yp5, '-k', 'LineWidth', 1.5)
grid on; axis tight
%% Labels and title
set(gca, 'FontName', 'Cambria')
xlabel('\it\color{blue} Velocity (m/sec)', 'FontSize', 15)
ylabel('\it\color{blue} Force (kN)', 'FontSize', 15)
title('\color{Orange} Variations in force from Wind tunnel Test', ...
'FontWeight', 'bold', 'FontSize', 20)
%% Legend
legend({'Velocity vs Force','Linear fit','2nd poly fit','3rd poly fit','4th poly fit','5th poly fit'}
'FontSize', 12, 'TextColor', 'black', 'Location', 'best')