How can I insert two quadratic trendlines for two datatsets (one each) on the same figure?

1 view (last 30 days)
James Bain on 20 Jul 2020
Answered: Serhii Tetora on 20 Jul 2020
I would like to inert two quadratic trendlines for two datasets (y1 and y2) on the same figure.
Basic fitting only allows the addition of one trendline at a time. I can get two trendlines on excel but I tend to avoid excel for imagery.
%HELP WITH ADDING TWO TRENDLINES FOR TWO DATASETS ON ONE FIGURE
clc, clear
x = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]; % x plot
y1 = [1660.383 1665.397 1671.268 1678.238 1686.645 1696.986 1710.013 1726.927 1749.767 1782.294 1838.604]; %y var 1
y2 = [1872.307 1877.526 1883.631 1890.871 1899.593 1910.305 1923.771 1942.211 1964.68 1997.947 2059.053]; %y var 2
% Figure 1 to contain AFTs for PZ with inlet temps of 20degC and 300degC
figure
scatter(x,y1,'k^') %plot x vs y1
hold on %introduce second plot onto the same figure
scatter(x,y2,'ko') %plot x vs y2
title('TITLE') %Figure title
hold off % end holding for additional plots on figure
ylabel('Y var') %label axis
xlabel('X var')
Many thanks, Jim.

Serhii Tetora on 20 Jul 2020
clc; close all; clear;
x = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]; % x plot
y1 = [1660.383 1665.397 1671.268 1678.238 1686.645 1696.986 1710.013 1726.927 1749.767 1782.294 1838.604]; %y var 1
y2 = [1872.307 1877.526 1883.631 1890.871 1899.593 1910.305 1923.771 1942.211 1964.68 1997.947 2059.053]; %y var 2
% Figure 1 to contain AFTs for PZ with inlet temps of 20degC and 300degC
figure
scatter(x,y1,'k^') %plot x vs y1
hold on %introduce second plot onto the same figure
plot(trendline(x,y1))
scatter(x,y2,'ko') %plot x vs y2
plot(trendline(x,y2))
hold off % end holding for additional plots on figure
ylabel('Y var') %label axis
xlabel('X var')
title('TITLE') %Figure title
legend({'data set 1','trend line 1','data set 2','trend line 2'},'location','northwest')
function fitresult = trendline(x,y)
[xData, yData] = prepareCurveData( x, y );
ft = fittype( 'poly2' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
[fitresult, ~] = fit( xData, yData, ft, opts );
end