📄 csfitguide.m
字号:
function varargout = CSfitGuide(varargin)
% CSFITGUIDE M-file for CSfitGuide.fig
% CSFITGUIDE, by itself, creates a new CSFITGUIDE or raises the existing
% singleton*.
%
% H = CSFITGUIDE returns the handle to a new CSFITGUIDE or the handle to
% the existing singleton*.
%
% CSFITGUIDE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CSFITGUIDE.M with the given input arguments.
%
% CSFITGUIDE('Property','Value',...) creates a new CSFITGUIDE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before CSfitGuide_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to CSfitGuide_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 CSfitGuide
% Last Modified by GUIDE v2.5 05-Nov-2005 21:56:37
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @CSfitGuide_OpeningFcn, ...
'gui_OutputFcn', @CSfitGuide_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
global A X Y S
% --- Executes just before CSfitGuide is made visible.
function CSfitGuide_OpeningFcn(hObject, eventdata, handles, varargin)
set(handles.left_edit,'string','1');
set(handles.right_edit,'string','1');
% 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 CSfitGuide (see VARARGIN)
% Choose default command line output for CSfitGuide
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes CSfitGuide wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = CSfitGuide_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 left_edit_Callback(hObject, eventdata, handles)
sp = get(gcbo,'String');
set(handles.left_edit,'string',sp);
% hObject handle to left_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of left_edit as text
% str2double(get(hObject,'String')) returns contents of left_edit as a double
% --- Executes during object creation, after setting all properties.
function left_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to left_edit (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function right_edit_Callback(hObject, eventdata, handles)
ep = get(gcbo,'String');
set(handles.right_edit,'string',ep);
% hObject handle to right_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of right_edit as text
% str2double(get(hObject,'String')) returns contents of right_edit as a double
% --- Executes during object creation, after setting all properties.
function right_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to right_edit (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Data_Edit.
function Data_Edit_Callback(hObject, eventdata, handles)
fid=fopen('XYdata.txt','a+');
open('XYdata.txt');
% hObject handle to Data_Edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Data_Read.
function [A X Y]=Data_Read_Callback(hObject, eventdata, handles)
[filename,pathname]=uigetfile('.txt','打开系数矩阵数据文件');
filepath=strcat(pathname,filename);
if ~isempty(filepath)
A=load(filepath);
X=A(:,1)';
Y=A(:,2)';
handles.A=A;
handles.X=X;
handles.Y=Y;
guidata(hObject, handles);
else
msgbox('打开文件失败','对话框','warn');
end
% hObject handle to Data_Read (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Calculation.
function Calculation_Callback(hObject, eventdata, handles)
sp = get(handles.left_edit,'String');
ep = get(handles.right_edit,'String');
sp=str2num(sp);
ep=str2num(ep);
X=handles.X;
A=handles.A;
Y=handles.Y;
if ~isempty(A)
S=CsFit(X,Y,sp,ep);
msgbox({'计算完毕','点击【查看结果】按钮查看结果'},'对话框','help');
handles.S=S;
guidata(hObject, handles);
else
msgbox('请先读入系数矩阵和右端向量数据','对话框','warn');
end
% hObject handle to Calculation (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Result.
function Result_Callback(hObject, eventdata, handles)
X=handles.X;
S=handles.S;
Y=handles.Y;
diary S(x).txt;
format short
for i=1:length(X)-1
disp(['S(x)=',num2str(S(i,1)),'*x^3 + ',num2str(S(i,2)),'*x^2 + ',...
num2str(S(i,3)),'*x + ',num2str(S(i,4)),' , ', num2str(X(1,i)),'≤x≤', num2str(X(1,i+1)) ]);
end
diary off;
open('S(x).txt');
delete S(x).txt;
% hObject handle to Result (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Plotp.
function Plotp_Callback(hObject, eventdata, handles)
hold off;
X=handles.X;
Y=handles.Y;
plot(X,Y,'o');
hold on;
xx=[X(1):(X(end)-X(1))/100:X(end)];
yy=interp1(X,Y,xx,'spline');
line(xx,yy);
% hObject handle to Plotp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in ClearF.
function ClearF_Callback(hObject, eventdata, handles)
hold off;
newplot;
% hObject handle to ClearF (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Info.
function Info_Callback(hObject, eventdata, handles)
msgbox({['作者:罗云标'],...
['单位:土木工程学院'], ...
['学号:050593'], ...
['Email:lyb-039@163.com'], ...
['水平有限,程序简陋'], ...
['不足之处,敬请原谅']
},'作者信息','help');
% hObject handle to Info (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Close.
function Close_Callback(hObject, eventdata, handles)
close;
% hObject handle to Close (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function S=CsFit(X,Y,dx0,dxn)
N=length(X)-1;
H=diff(X);
D=diff(Y)./H;
A=H(2:N-1);
B=2*(H(1:N-1)+H(2:N));
C=H(2:N);
U=6*diff(D);
B(1)=B(1)-H(1)/2;
U(1)=U(1)-3*(D(1)-dx0);
B(N-1)=B(N-1)-H(N)/2;
U(N-1)=U(N-1)-3*(dxn-D(N));
for k=2:N-1
temp=A(k-1)/B(k-1);
B(k)=B(k)-temp*C(k-1);
U(k)=U(k)-temp*U(k-1);
end
M(N)=U(N-1)/B(N-1);
for k=N-2:-1:1
M(k+1)=(U(k)-C(k)*M(k+2))/B(k);
end
M(1)=3*(D(1)-dx0)/H(1)-M(2)/2;
M(N+1)=3*(dxn-D(N))/H(N)-M(N)/2;
for k=0:N-1
S(k+1,1)=(M(k+2)-M(k+1))/(6*H(k+1));
S(k+1,2)=M(k+1)/2;
S(k+1,3)=D(k+1)-H(k+1)*(2*M(k+1)+M(k+2))/6;
S(k+1,4)=Y(k+1);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -