Similar to how you use the fit command to find the exponential fit, you can use it to find the linear fit by using 'poly1' as the fitType argument. Please see the fitType section of the fit documentation for information. To display the equations on on the plot, you can implement this workaround from a similar MATLAB Answers question. Assuming you would like to display the equation in the plots legend, please try the below code:
x = SZA15(:,9);
y = SZA15(:,5);
fits{1} = fit(x,y,'exp1');
fits{2} = fit(x,y,'poly1');
hold on
plot(x,y,'o');
plot(fits{1},'r');
plot(fits{2},'b');
str = {'Data'};
for i = 1:numel(fits)
eq = formula(fits{i});
parameters = coeffnames(fits{i});
values = coeffvalues(fits{i});
for idx = 1:numel(parameters)
param = parameters{idx};
l = length(param);
loc = regexp(eq, param);
while ~isempty(loc)
eq = [eq(1:loc-1) num2str(values(idx)) eq(loc+l:end)];
loc = regexp(eq, param);
end
end
str{i+1} = eq;
end
legend(str);
Best Answer