%% main3.m % This is a Matlab Script % Execute it by typing its name in the Matlab Command Window %% Define Test Data myx = (1:.1:10)'; % Generate data using a model function mydata = model3([1 3 10], myx); % Generate normally distributed noise noise = 3*randn(size(mydata)); % Add noise to the sample data mydata = mydata + noise; %% Analyze Data % Polynomial fit p = polyfit(myx, mydata, 3) % Non-linear fit fit = lsqcurvefit(@model3, [1 1 1], myx, mydata) %% Plot the Sample Data plot(myx, mydata, 'Marker', '.', 'LineStyle','none'); xlabel({'Time'}); ylabel({'Measurement'}); title({'Sample Data'}); %% Plot the Polynomial fit hold on values = polyval(p, myx); plot(myx, values, 'color', 'r'); hold off %% Plot the Non-Linear Fit hold on values = model3(fit, myx); plot(myx, values, 'color', 'black'); hold off