MATLAB: 1-d curve equation

curve fitting

I have two arrays:
1_time ->
t=(0:15);
2_measurements ->
data1=[ 0 7 20 28 35 48 57 61 72 82 96 98 111 121 129 144 ];
I have to get the estimation at t=20 ( linear method ) ,and the 'minimum & maximum' difference between the linear equation line and the data1 curve .
My method was :
1 Making a plot ->
plot(t,data1);
2 Getting the estimated at t=20 by using the numerical panel (tool-> basic fitting)
3 And finally i get the 'minimum & maximum' difference between the linear equation line and the data1 curve , by plotting the residuals
Is there any code to get what i want and not by using plotting .. ect ? I am a newbie when it come to matlab

Best Answer

If you are confident of a linear fit, just calculate the extrapolated ‘data1’ value at ‘t=20’:
t=(0:15);
data1=[ 0 7 20 28 35 48 57 61 72 82 96 98 111 121 129 144 ];
B = [t' ones(size(t'))]\data1'; % Estimate Coefficient Of Linear Fit
data1_fit = [t' ones(size(t'))]*B; % Create Regression Line To Plot
data1_20 = [20 1]*B % Extrapolate Fit To ‘t=20’
figure(1)
plot(t, data1, 'bp')
hold on
plot(t, data1_fit, '-g')
plot(20, data1_20, '*r')
hold off
grid
axis([0 21 -10 200])
This code plots the data and fit using the mldivide,\ operator, and plots and calculates the extrapolated value:
data1_20 =
186.43
Related Question