數值分析課程設計_第1頁
數值分析課程設計_第2頁
數值分析課程設計_第3頁
數值分析課程設計_第4頁
數值分析課程設計_第5頁
已閱讀5頁,還剩18頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、課程設計實驗報告 學院:數學與信息科學與學院 專業:信息與計算科學專業姓名:宋寶學號:201014412一、 理論基礎:本次課程設計做兩個方面的設計:1. 插值1.1:參考數值分析(第5版)上面關于拉格朗日、牛頓、三次樣條插值算法的介紹結合自己所學的MATLAB程序設計,將其程序化;通過相關matlab GUI資料的介紹設計出可視化界面進行插值的相關操作。2. 解線性方程組2.1:參考數值分析(第5版)和有關接線性方程組的資料研究里面關于列主元消元法、多利特勒分解法、改進平方根法、QR分解法、雅可比迭代法、高斯-賽德爾迭代法、超松弛迭代法解線性方程組的具體步驟,通過matlab語言編程實現,并

2、通過GUI設計實現可視化操作。二、 算法結構1. 插值:拉格朗日插值算法描述:首先求出插值節點數據的長度,然后將賦值為0,然后計算 ,通過for循環將相加賦值給,得到拉格朗日插值函數,最后通過MATLAB的化簡多項式相關的函數對進行化簡。牛頓插值算法描述:首先求出一階差商,然后以上一次差商的結果作為,求出二階、三階等更高階差商,運用下面公式即可求出牛頓插值多項式三次樣條插值算法描述:首先構造滿足條件的三次樣條插值函數,將數據結點的值帶入到插值函數中得到該函數。2. 解線性方程組:列主元消元法算法描述:使用高斯消元法,每次消元前,先求出到的絕對值最大的,然后矩陣的第行和第行進行交換,然后消元,最

3、后解系數矩陣是上三角矩陣的方程組,得出解。直接三角分解法算法描述:將 分解為,其中 1. 2.計算的第行和的第列元素3. 解系數矩陣是下三角矩陣的方程組,得出解,然后解系數矩陣是上三角矩陣的方程組,解出方程組的解改進平方根法算法描述:將分解為,其中為下三角矩陣,對角元素為1,為對角矩陣,通過矩陣之間的乘法求出和;然后解系數矩陣是下三角矩陣的方程組,得出解,然后方程組,解出方程組的解QR分解法算法描述:對線性方程組的系數矩陣進行QR分解:,方程變為其為三角方程,很容易求解解法求解。雅可比迭代法算法描述:首先將的系數矩陣進行分解:,利用公式,;解的計算公式為:高斯-賽德爾迭代法算法描述:首先將的系

4、數矩陣進行分解:,利用公式,;解的計算公式為:超松弛迭代法算法描述:首先將的系數矩陣進行分解:,利用公式,;解的計算公式為:三、 程序代碼插值MATLAB代碼:function varargout = chazhiGUI(varargin)gui_Singleton = 1;gui_State = struct('gui_Name', mfilename, . 'gui_Singleton', gui_Singleton, . 'gui_OpeningFcn', chazhiGUI_OpeningFcn, . 'gui_OutputFcn

5、', chazhiGUI_OutputFcn, . 'gui_LayoutFcn', , . 'gui_Callback', );if nargin && ischar(varargin1) gui_State.gui_Callback = str2func(varargin1);end if nargout varargout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);end% End initializatio

6、n code - DO NOT EDITglobal a x y t xx yy ff% - Executes just before chazhiGUI is made visible.function chazhiGUI_OpeningFcn(hObject, eventdata, handles, varargin)handles.output = hObject; % Update handles structureguidata(hObject, handles); % UIWAIT makes chazhiGUI wait for user response (see UIRESU

7、ME)% uiwait(handles.figure1);% - Outputs from this function are returned to the command line.function varargout = chazhiGUI_OutputFcn(hObject, eventdata, handles) varargout1 = handles.output; function edit1_shuju_Callback(hObject, eventdata, handles)% - Executes during object creation, after setting

8、 all properties.function edit1_shuju_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end % - Executes on selection change in listbox1.funct

9、ion listbox1_Callback(hObject, eventdata, handles)% - Executes during object creation, after setting all properties.function listbox1_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,

10、'BackgroundColor','white');end % - Executes on selection change in listbox2.function listbox2_Callback(hObject, eventdata, handles)% - Executes during object creation, after setting all properties.function listbox2_CreateFcn(hObject, eventdata, handles)% Hint: listbox controls usuall

11、y 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 pushbutton1. function x,y,a=pushb

12、utton1_Callback(hObject, eventdata, handles)%獲取文本框內容a = get(handles.edit1_shuju,'String');b=str2num(a);x=b(1,:);y=b(2,:);handles.a=a;handles.x=x;handles.y=y;t=handles.t;val1=t;if val1=1 f=Language(x,y);else if val1=2 f=Newton(x,y); else if val1=3 n=length(x); xx=x(1):0.01:x(n); yy=interp1(x,

13、y,xx,'spline'); end endendif val1=3ff=fhandles.ff=ff;n=length(x);xx=x(1):0.01:x(n);t=xx;yy=subs(f,t);endhandles.xx=xx;handles.yy=yy;guidata(hObject, handles); % - Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3

14、 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)t=handles.t;val1=t;if val1=3ff=handles.ff;gg=sym2poly(ff);hh=num2str(gg);set(handles. edit3_jieguo,'String',hh); else set(handles. edit3_jieguo,'String

15、','太復雜,在此不給出!'); end guidata(hObject, handles); %更新結構體% - Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)% hObject handle to pushbutton4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with

16、 handles and user data (see GUIDATA)% - Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)% hObject handle to pushbutton5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GU

17、IDATA)close;% - Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)function edit3_jieguo_Callback(hObject, eventdata, handles)% - Executes during object creation, after setting all properties.function edit3_jieguo_CreateFcn(hObject, eventdata, handles)%

18、 hObject handle to edit3_jieguo (see GCBO)% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end % - Executes on selection change in popupmenu1.function pop

19、upmenu1_Callback(hObject, eventdata, handles)% hObject handle to popupmenu1 (see GCBO)function popupmenu1_CreateFcn(hObject, eventdata, handles)% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'

20、;BackgroundColor','white');end % - Executes on selection change in popupmenu2.function t=popupmenu2_Callback(hObject, eventdata, handles)% hObject handle to popupmenu2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user da

21、ta (see GUIDATA)list=get(handles.popupmenu2,'String');val1=get(handles.popupmenu2,'Value');t=val1;handles.t=t;selectedval=str2num(listval1);guidata(hObject, handles);function popupmenu2_CreateFcn(hObject, eventdata, handles)% See ISPC and COMPUTER.if ispc && isequal(get(hObje

22、ct,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end %拉格朗日插值法function f=Language(x,y)syms t;if(length(x)=length(y) n=length(x);else disp('x和y的維數不相等!'); return;endf=0.0;for(i=1:n) l=y(i); for(j=1:i-1) l=l*(t

23、-x(j)/(x(i)-x(j); end for(j=i+1:n) l=l*(t-x(j)/(x(i)-x(j); end f=f+l; simplify(f);end%simple(f)k=expand(f);f=k;f=vpa(k); %牛頓插值function f=Newton(x,y)syms t;if(length(x)=length(y) n=length(x); c(1:n)=0.0;else disp(' x和y的維數不相等!'); return;endf=y(1);y1=0;l=1;for(i=1:n-1) for(j=i+1:n) y1(j)=(y(j)-

24、y(i)/(x(j)-x(i); end c(i)=y1(i+1); l=l*(t-x(i); f=f+c(i)*l; simplify(f); y=y1;endk=expand(f);f=k;f=vpa(k);% - Executes on button press in pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles)% hObject handle to pushbutton7 (see GCBO)x=handles.x;y=handles.y;plot(x,y,'o');hold

25、 on;xx=handles.xx;yy=handles.yy;plot(xx,yy,'b-');% - Executes on button press in pushbutton8.function pushbutton8_Callback(hObject, eventdata, handles)% hObject handle to pushbutton8 (see GCBO)hold off;newplot;解線性方程組MATLAB代碼:function varargout = jiefangchengGUI(varargin)gui_Singleton = 1;gui

26、_State = struct('gui_Name', mfilename, . 'gui_Singleton', gui_Singleton, . 'gui_OpeningFcn', jiefangchengGUI_OpeningFcn, . 'gui_OutputFcn', jiefangchengGUI_OutputFcn, . 'gui_LayoutFcn', , . 'gui_Callback', );if nargin && ischar(varargin1) gui_S

27、tate.gui_Callback = str2func(varargin1);endif nargout varargout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDITglobal t% - Executes just before jiefangchengGUI is made visible.function jiefangchengGUI_OpeningFcn(hObject,

28、eventdata, handles, varargin)handles.output = hObject;% Update handles structureguidata(hObject, handles); function varargout = jiefangchengGUI_OutputFcn(hObject, eventdata, handles) varargout1 = handles.output; function edit1_juzhen_Callback(hObject, eventdata, handles)function edit1_juzhen_CreateF

29、cn(hObject, eventdata, handles)% hObject handle to edit1_juzhen (see GCBO)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end function edit2_Callback(hObject, eventdata, handles

30、)function edit2_CreateFcn(hObject, eventdata, handles)% hObject handle to edit2 (see GCBO)% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end function ed

31、it3_Callback(hObject, eventdata, handles)function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (see GCBO)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white')

32、;end function edit4_chuzhi_Callback(hObject, eventdata, handles)% hObject handle to edit4_chuzhi (see GCBO)function edit4_chuzhi_CreateFcn(hObject, eventdata, handles)% hObject handle to edit4_chuzhi (see GCBO)% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),

33、 get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end function edit5_Callback(hObject, eventdata, handles)% - Executes during object creation, after setting all properties.function edit5_CreateFcn(hObject, eventdata, handles)% See ISPC and COMPU

34、TER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end function edit6_songci_Callback(hObject, eventdata, handles)function edit6_songci_CreateFcn(hObject, eventdata, handles)if

35、 ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end function edit7_Callback(hObject, eventdata, handles)function edit7_CreateFcn(hObject, eventdata, handles)if ispc && ise

36、qual(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end function edit8_jingdu_Callback(hObject, eventdata, handles)% - Executes during object creation, after setting all properties.function edit8_jingdu_

37、CreateFcn(hObject, eventdata, handles)% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end function edit10_result_Callback(hObject, eventdata, handles)fun

38、ction edit10_result_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end % - Executes on selection change in listbox1.function listbox1_Call

39、back(hObject, eventdata, handles)function listbox1_CreateFcn(hObject, eventdata, handles)% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor') set(hObject,'BackgroundColor','white');end% - Executes

40、on button press in pushbutton4_jisuan.function pushbutton4_jisuan_Callback(hObject, eventdata, handles)% hObject handle to pushbutton4_jisuan (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% 獲取文本框內容a = get(handl

41、es.edit1_juzhen,'String');b = get(handles.edit4_chuzhi,'String');c = get(handles.edit6_songci,'String');d = get(handles.edit8_jingdu,'String');aa=str2num(a);x0=str2num(b);x0=x0'w=str2num(c);eps=str2num(d);n=length(aa);A=aa(:,1:n-1);b=aa(:,n);t=handles.t;val1=t;swi

42、tch(val1) case 1 x,XA=GaussXQLineMain(A,b); case 2 x,L,U=Doolittle(A,b); case 3 msgbox( '方程組的系數矩陣必須對稱正定' ,'warning!','help'); x,L,D=Sympos3 (A,b); case 4 x,Q,R=qrxq(A,b); case 5 x,n=jacobi(A,b,x0,eps); case 6 x,n=gauseidel(A,b,x0,eps); case 7 x,n=SOR(A,b,x0,w,eps); endhh=num2

43、str(x');set(handles. edit10_result,'String',hh);if(val1=5|val1=6|val1=7) hh=num2str(n);set(handles. edit11_cishu,'String',hh);else set(handles. edit11_cishu,'String',' '); end guidata(hObject, handles); %更新結構體 % - Executes on button press in pushbutton5.function p

44、ushbutton5_Callback(hObject, eventdata, handles)% hObject handle to pushbutton5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) msgbox('作者:宋寶',. '學院:數學與信息科學學院', .'專業:信息與計算科學專業', . '學號

45、:201014412', . '水平有限,程序簡陋', . '不足之處,敬請原諒' ,'作者信息','help'); % - Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)% hObject handle to pushbutton6 (see GCBO)% eventdata reserved - to be defined in a future version of M

46、ATLAB% handles structure with handles and user data (see GUIDATA)close;%列主元消元法function x,XA=GaussXQLineMain(A,b)N=size(A);n=N(1);index=0;for i=1:(n-1) me=max(abs(A(1:n,i); for k=i:n if(abs(A(k,i)=me) index=k; end end temp=A(i,1:n); A(i,1:n)=A(index,1:n); A(index,1:n)=temp; bb=b(index); b(index)=b(i)

47、; b(i)=bb; for j=(i+1):n if(A(i,i)=0) disp('對角元素為0!'); return; end l=A(j,i); m=A(i,i); A(j,1:n)=A(j,1:n)-l*A(i,1:n)/m; b(j)=b(j)-l*b(i)/m; endendfor i=n:-1:1 if(i<n) s=A(i,(i+1):n)*x(i+1):n,1); else s=0; end x(i,1)=(b(i)-s)/A(i,i);endXA=A;%直接法function x,L,U=Doolittle(A,b)N=size(A);n=N(1);

48、L=eye(n,n);U=zeros(n,n);U(1,1:n)=A(1,1:n);L(1:n,1)=A(1:n,1)/U(1,1);for k=2:n for i=k:n U(k,i)=A(k,i)-L(k,1:(k-1)*U(1:(k-1),i); end for j=(k+1):n L(j,k)=(A(j,k)-L(j,1:(k-1)*U(1:(k-1),k)/U(k,k); endendN=size(L);n=N(1); for i=1:n if(i>1) s=L(i,1:(i-1)*y(1:(i-1),1); else s=0; end y(i,1)=(b(i)-s)/L(i,

49、i); end N=size(U);n=N(1);for i=n:-1:1 if(i<n) s=U(i,(i+1):n)*x(i+1):n,1); else s=0; end x(i,1)=(y(i)-s)/U(i,i);end%改進平方根法function x,L,D=Sympos3 (A,b)N=size(A);n=N(1);L=eye(n,n);d=zeros(n,1);AA=zeros(n,n);for k=1:n d(k,1)=A(k,k)-AA(k,1:(k-1)*transpose(L(k,1:(k-1); for i=(k+1):n AA(i,k)=A(i,k)-AA(i

50、,1:(k-1)*transpose(L(k,1:(k-1); L(i,k)=AA(i,k)/d(k,1); endendD=diag(d);N=size(L);n1=N(1);for i=1:n1 if(i>1) s=L(i,1:(i-1)*y(1:(i-1),1); else s=0; end y(i,1)=(b(i)-s)/L(i,i);endfor i=1:n y(i,1)=y(i,1)/d(i,1);endt=transpose(L);N=size(t);n=N(1);for i=n:-1:1 if(i<n) s=t(i,(i+1):n)*x(i+1):n,1); els

51、e s=0; end x(i,1)=(y(i)-s)/t(i,i);end%QR分解法function x,Q,R=qrxq(A,b)N=size(A);n=N(1);B=A;A(1:n,1)=A(1:n,1)/norm(A(1:n,1);for i=2:n for j=1:(i-1) A(1:n,i)=A(1:n,i)-dot(A(1:n,i),A(1:n,j)*A(1:n,j); end A(1:n,i)=A(1:n,i)/norm(A(1:n,i);endQ=A;R=transpose(Q)*B;k=transpose(Q)*b;N=size(R);n=N(1);for i=n:-1:1

52、 if(i<n) s=R(i,(i+1):n)*x(i+1):n,1); else s=0; end x(i,1)=(k(i)-s)/R(i,i);end%雅可比迭代法function x,n=jacobi(A,b,x0,eps,M)if nargin=3 eps=1.0e-6; M=10000;else if nargin=4 M=10000; endend D=diag(diag(A); L=-tril(A,-1); U=-triu(A,1); B=D(L+U); f=Db; x=x0; n=0; tol=1; while tol>=eps x=B*x0+f; n=n+1; t

53、ol=norm(x-x0); x0=x; if(n>=M) disp('Waring:迭代次數太多,可能不收斂!'); return; end end %高斯-賽德爾迭代法 function x,n=gauseidel(A,b,x0,eps,M)if nargin=3 eps=1.0e-6; M=10000;else if nargin=4 M=10000; endend D=diag(diag(A); L=-tril(A,-1); U=-triu(A,1); G=(D-L)U; f=(D-L)b; x=x0; n=0; tol=1; while tol>=eps x=G*x0+f; n=n+1; tol=norm(x-x0); x0=x; if(n>=M) disp('Waring: 迭代次數太多,可能不收斂!'); return; end end %超松弛迭代法 function x,n=SOR(A,b,x0,w,eps,M)if nargin=4 eps=1.0e-6; M=10000;else if nargin=5 M=10000; endendif(w<=0|w>=2) error; return;endD=diag(diag(A);L=-tril(A,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論