In: Computer Science
%Purpose:
%Fit a first degree polynomial,
%a second-degree polynomial,
%and a third-degree polynomial to these data
%Plot all three plots (along with the original data points)
%together on the same plot and determine the BEST fit
%After determining the best fit add additional code to the end of
your script
%to predict the pressure at t = 11 seconds from the best-fitting
curve
%INPUT VARIABLES:
t = 1:0.05:10;
p = [26.1,27,28.2,29,29.8,30.6,31.1,31.3,31,30.5];
d1 = polyfit( t, p, 1 ); %first degree
d2 = polyfit( t, p, 2 ); %second degree
d3 = polyfit( t, p, 3 ); %third degree
t1 = 1:0.05:10;
p1 = polyval(d1,t1);
p2 = polyval(d2,t1);
p3 = polyval(d3,t1);
%
%OUTPUT VARIABLES:
%plot(t,p,'o') original data points
%plot(t, p1,)first degree
%plot(t, p2,)second degree
%plot(t, p3, '--')third degree
%title ('Fuel Line Pressure Predictions');
%xlabel('Time (sec)');
%ylabel('Pressure (psi)');
%
%OUTPUT SECTION:
plot(t,p,'o')
plot(t1,p1);
plot(t1,p2);
plot(t1,p3,'--');
title ('Fuel Line Pressure Predictions')
xlabel('Time (sec)')
ylabel('Pressure (psi)')
I am not sure what is wrong with my code. The errors that pop up are:
Error using polyfit (line 44)
The first two inputs must have the same number of elements.
Error in EngrProb_1 (line 17)
d1 = polyfit( t, p, 1 ); %first degree
Third degree is the best fit
raw_code :
%Purpose:
%Fit a first degree polynomial,
%a second-degree polynomial,
%and a third-degree polynomial to these data
%Plot all three plots (along with the original data points)
%together on the same plot and determine the BEST fit
%After determining the best fit add additional code to the end of
your script
%to predict the pressure at t = 11 seconds from the best-fitting
curve
%INPUT VARIABLES:
t = 1:1:10;
p = [26.1,27,28.2,29,29.8,30.6,31.1,31.3,31,30.5];
d1 = polyfit( t, p, 1 ); %first degree
d2 = polyfit( t, p, 2 ); %second degree
d3 = polyfit( t, p, 3 ); %third degree
p1 = polyval(d1,t);
p2 = polyval(d2,t);
p3 = polyval(d3,t);
figure
hold on
plot(t,p,'o') %plotting original points
plot(t,p1);
plot(t,p2);
plot(t,p3,'--');
title ('Fuel Line Pressure Predictions')
xlabel('Time (sec)')
ylabel('Pressure (psi)')
%third degree is best fit
%predicting pressure value at t =11
predict = polyval(d3,11);
fprintf('pressure at t = 11 is %f\n',predict)
**do comment for queries and rate me up****