Code covered by the BSD License

# MATLAB para Ensino

### Elia Matsumoto (view profile)

Slides and demo files from the webinar "MATLAB for Teaching' (Portuguese).

ajuste_oscilacao_x.m
```function [Resid,Coeff,Coeff_int] = ajuste_oscilacao_x(time,new_x_oscillations)
%ajuste_oscilacao_x   Create plot of datasets and fits
%   ajuste_oscilacao_x(TIME,NEW_X_OSCILLATIONS)
%   Creates a plot, similar to the plot in the main curve fitting
%   window, using the data that you provide as input.  You can
%   apply this function to the same data you used with cftool
%   or with different data.  You may want to edit the function to
%   customize the code and this help message.
%
%   Number of datasets:  1
%   Number of fits:  1

% Data from dataset "new_x_oscillations vs. time":
%    X = time:
%    Y = new_x_oscillations:
%    Unweighted
%
% This function was automatically generated on 12-Jul-2007 15:38:55

% Set up figure to receive datasets and fits
f_ = clf;
figure(f_);
set(f_,'Units','Pixels','Position',[280.667 159.5 680 477]);
legh_ = []; legt_ = {};   % handles and text for legend
xlim_ = [Inf -Inf];       % limits of x axis
ax_ = axes;
set(ax_,'Units','normalized','OuterPosition',[0 .5 1 .5]);
ax2_ = axes;
set(ax2_,'Units','normalized','OuterPosition',[0 0 1 .5]);
set(ax2_,'Box','on');
legrh_ = []; legrt_ = {};
set(ax_,'Box','on');
grid(ax_,'on');
grid(ax2_,'on');
axes(ax_); hold on;

% --- Plot data originally in dataset "new_x_oscillations vs. time"
time = time(:);
new_x_oscillations = new_x_oscillations(:);
h_ = line(time,new_x_oscillations,'Parent',ax_,'Color',[0.333333 0 0.666667],...
'LineStyle','none', 'LineWidth',1,...
'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(time));
xlim_(2) = max(xlim_(2),max(time));
legh_(end+1) = h_;
legt_{end+1} = 'Oscilao em X vs tempo';

% Nudge axis limits beyond data limits
if all(isfinite(xlim_))
xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
set(ax_,'XLim',xlim_)
set(ax2_,'XLim',xlim_)
end

% --- Create fit "TimeConstantFit"

% Apply exclusion rule "TimeConstantRule"
if length(time)~=1802
error('Exclusion rule "TimeConstantRule" is incompatible with time');
end
ex_ = true(length(time),1);
ex_([162 198 233 234 269 270 305 341 376 377 412 448 449 484 519 520 555 556 591 627 662 663 698 699 734 735 770 805 806 841 877 913 948 949 984 985 1020 1021 1056 1091 1092 1127 1128 1163 1164 1199 1200 1235 1271 1306 1307 1342 1378 1413 1414 1449 1450 1485 1521 1557 1592 1593 1628 1664 1699 1700 1735 1736 1771 1772]) = 0;
ok_ = isfinite(time) & isfinite(new_x_oscillations);
st_ = [0.8294321811142 -0.004034768191266 ];
ft_ = fittype('exp1');

% Fit this model using new data
if sum(~ex_(ok_))<2  %% too many points excluded
error('Not enough data left to fit ''%s'' after applying exclusion rule ''%s''.','TimeConstantFit','TimeConstantRule')
else
cf_ = fit(time(ok_),new_x_oscillations(ok_),ft_,'Startpoint',st_,'Exclude',ex_(ok_));
end
Coeff = coeffvalues(cf_);
Coeff_int = confint(cf_);

% Or use coefficients from the original fit:
if 0
cv_ = { 0.8256174043586, -0.003907634603163};
cf_ = cfit(ft_,cv_{:});
end

% Plot this fit
h_ = plot(cf_,'predobs',0.95);
legend off;  % turn off legend from plot method call
set(h_(1),'Color',[1 0 0],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
legh_(end+1) = h_(1);
legt_{end+1} = 'AjusteConstanteTempo';
if length(h_)>1
set(h_(2:end),'Color',[1 0 0],...
'LineStyle',':', 'LineWidth',1,'Marker','none');
legh_(end+1) = h_(2);
legt_{end+1} = 'Limites Pred (AjusteConstanteTempo)';
end
res_ = new_x_oscillations(~ex_) - cf_(time(~ex_));
Resid = res_;
[x_,i_] = sort(time(~ex_));
axes(ax2_); hold on;
h_ = line(x_,res_(i_),'Parent',ax2_,'Color',[1 0 0],...
'LineStyle','-', 'LineWidth',1,...
'Marker','.', 'MarkerSize',6);
axes(ax_); hold on;
legrh_(end+1) = h_;
legrt_{end+1} = 'AjusteConstanteTempo';

% Done plotting data and fits.  Now finish up loose ends.
hold off;
leginfo_ = {'Orientation', 'vertical'};
h_ = legend(ax_,legh_,legt_,leginfo_{:}); % create and reposition legend
set(h_,'Units','normalized');
t_ = get(h_,'Position');
t_(1:2) = [0.1566667,0.5804191];
set(h_,'Interpreter','none','Position',t_);
xlabel(ax_,'');               % remove x label
ylabel(ax_,'');               % remove y label
leginfo_ = {'Orientation', 'vertical', 'Location', 'SouthEast'};
legend(ax2_,legrh_,legrt_,leginfo_{:});
xlabel(ax2_,'');
ylabel(ax2_,'');