Code covered by the BSD License  

Highlights from
Least Squares Curve Fitting

image thumbnail

Least Squares Curve Fitting

by

 

01 Sep 2006 (Updated )

Graphical interfacing of least squares

curve_fitting(varargin)
function varargout = curve_fitting(varargin)
% CURVE_FITTING M-file for curve_fitting.fig
%      CURVE_FITTING, by itself, creates a new CURVE_FITTING or raises the existing
%      singleton*.
%
%      H = CURVE_FITTING returns the handle to a new CURVE_FITTING or the handle to
%      the existing singleton*.
%
%      CURVE_FITTING('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CURVE_FITTING.M with the given input arguments.
%
%      CURVE_FITTING('Property','Value',...) creates a new CURVE_FITTING or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before curve_fitting_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to curve_fitting_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help curve_fitting

% Last Modified by GUIDE v2.5 01-Sep-2006 00:14:26

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @curve_fitting_OpeningFcn, ...
                   'gui_OutputFcn',  @curve_fitting_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before curve_fitting is made visible.
function curve_fitting_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to curve_fitting (see VARARGIN)
% Choose default command line output for curve_fitting
handles.output = hObject;
axes(handles.dataaxis)
title('Original Data')
axes(handles.curvedata)
title('Data With Fitted Curve')
set(handles.xdatae,'string','')
set(handles.ydatae,'string','')
set(handles.dofe,'string','')
set(handles.fx,'string','')
set(handles.dataaxis,'nextplot','replacechildren')
set(handles.eqax,'visible','on')
set(handles.fatx,'string','')
axch=get(handles.eqax,'children');
if ~isempty(axch)
set(axch,'string','')
end
set(handles.eqax,'visible','off')
hd0=get(handles.dataaxis,'children');
if ishandle(hd0)
delete(hd0)
end
hd1=get(handles.curvedata,'children');
if ishandle(hd1)
delete(hd1)
end
% Update handles structure
guidata(hObject, handles);
figure1_CreateFcn
% UIWAIT makes curve_fitting wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = curve_fitting_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function xdatae_Callback(hObject, eventdata, handles)
% hObject    handle to xdatae (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a = str2num(get(hObject,'String'));
if ~isempty(a)
    handles.x=a;
if isfield(handles,'y')
    if ~isempty(handles.y)
    if ~(length(handles.x)==length(handles.y))
    errordlg('X and Y Data Points Must Be Equal','Acquisition error')
    end
    end
end
else
    errordlg('Invalid Data','Acquisition error')
end
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of xdatae as text
%        str2double(get(hObject,'String')) returns contents of xdatae as a double


% --- Executes during object creation, after setting all properties.
function xdatae_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xdatae (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function ydatae_Callback(hObject, eventdata, handles)
% hObject    handle to ydatae (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a = str2num(get(hObject,'String'));
if ~isempty(a)
    handles.y=a;
if isfield(handles,'x')
    if ~isempty(handles.x)
    if ~(length(handles.x)==length(handles.y))
    errordlg('X and Y Data Points Must Be Equal','Acquisition error')
    end
    end
end
else
    errordlg('Invalid Data','Acquisition error')
end
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of ydatae as text
%        str2double(get(hObject,'String')) returns contents of ydatae as a double


% --- Executes during object creation, after setting all properties.
function ydatae_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ydatae (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function dofe_Callback(hObject, eventdata, handles)
% hObject    handle to dofe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a = str2num(get(hObject,'String'));
if ~isempty(a)
    handles.n=a;
else
    errordlg('Invalid Data','Acquisition error')
end
if isfield(handles,'y') && isfield(handles,'x')
if ~(isempty(handles.x) && isempty(handles.y))
if ~(length(handles.x)==length(handles.y))
    errordlg('X and Y Data Points Must Be Equal','Acquisition error')
end
end
end
if a<1
    create.WindowStyle='replace';
    create.Interpreter='tex';
    msgbox('Degree of fit can\primet be less than 1','Fitting error','error',create)
end
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of dofe as text
%        str2double(get(hObject,'String')) returns contents of dofe as a double


% --- Executes during object creation, after setting all properties.
function dofe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dofe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a = str2num(get(hObject,'String'));
if ~isempty(a)
    handles.fx=a;
else
    errordlg('Invalid Data','Acquisition error')
end
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on button press in analyze.
function analyze_Callback(hObject, eventdata, handles)
% hObject    handle to analyze (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.dataaxis,'nextplot','replacechildren')
set(handles.eqax,'Visible','on')
axch=get(handles.eqax,'children');
if ~isempty(axch)
set(axch,'string','')
end
set(handles.eqax,'visible','off')
x=handles.x;y=handles.y;n=handles.n;
if n>2 || n==2
if isfield(handles,'msg')
   h=handles.msg;
   handles=rmfield(handles,'msg');
   close(h)
   end
end
f=polyfit(x,y,n);handles.f=f;
eq=poly2sym(f);
eq=vpa(eq,2);
if n<2
S=['$',latex(eq),'$'];
S=strrep(S,'&amp;','&amp; \quad');
S=strrep(S,'{\it','\mathrm{');
haxeq=handles.eqax;
axes(haxeq)
heq=text(0,0.5,S);
set(heq,'interpreter','latex')
set(heq,'string',S)
set(heq,'fontsize',12)
if isfield(handles,'msg')
   close(handles.msg)
    handles=rmfield(handles,'msg');
end
else
haxeq=handles.eqax;
axes(haxeq)
heq=text(0,0.5,{['Equation is displayed'];['       on dialog box']});
S=['$',latex(eq),'$'];
S=strrep(S,'&amp;','&amp; \quad');
S=strrep(S,'{\it','\mathrm{');
h=msgbox(S,'Equation');
handles.msg=h;
h1=get(h,'children');
delete(h1(2))
delete(h1(3))
h2=h1(1);
h3=get(h2,'children');
set(h3,'visible','off')
set(h3,'interpreter','latex')
set(h3,'string',S)
set(h3,'fontsize',12)
w=get(h3,'extent');
W=get(h,'position');
W(3)=max(w(3)+10,125);
W(4)=w(4)+40;
set(h,'position',W)
set(h3,'visible','on')
end
yf=polyval(f,x);handles.yf=yf;
axes(handles.dataaxis)
plot(x,y);
axes(handles.curvedata)
plot(x,y);hold on
 box off
 plot(x,yf,'-.r');hold off
title('Data With Fitted Curve')
guidata(hObject,handles)
set(h,'closerequestfcn',{@deletecallback,hObject,handles})
% --- Executes on button press in fxana.
function fxana_Callback(hObject, eventdata, handles)
% hObject    handle to fxana (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fx=handles.f;fatx=polyval(fx,handles.fx);
h11=handles.fatx;
set(h11,'string',{['x = ',num2str(fatx)]})
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
h=gcf;
set(h,'units','normalized')
set(h,'position',[0.08 .1 0.8 0.8])

function deletecallback(h,eventdata,hObject,handles)
if isfield(handles,'msg')
    handles=rmfield(handles,'msg');
    guidata(hObject,handles)
end
closereq

Contact us