计算方法 线性方程组的解法.doc_第1页
计算方法 线性方程组的解法.doc_第2页
计算方法 线性方程组的解法.doc_第3页
计算方法 线性方程组的解法.doc_第4页
计算方法 线性方程组的解法.doc_第5页
免费预览已结束,剩余8页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

0640303233 张世兵 实验五 线性方程组的解法一 实验目的:通过本课程的学习,学会编写全主原消去法的计算程序。掌握解线性方程组的最基本算法及其运用,进一步了解该解法的功能、优缺点,领会系数矩阵对解的影响。二 实验内容:高斯消去法:要求:用随机函数随机产生一个n阶线性方程组,用不换行列的全主元法求解线性方程组,并且对该解进行验证。方法概要解线性方程组:高斯消去法分消元过程和回代过程两个环节。a. 消元过程对k=1,2,n-1进行下列运算:1) 选主元,确定r,t,使若,则系数矩阵为奇异的,停止计算,否则进入下一步。2) 交换A中的r,t两行及r,t两列,并记下交换的下标j,k.3)对i=k+1,k+2,n;j=k+1,k+2,n+1,计算b. 回代过程:对k=n,n-1,1;按下列公式计算:c. 恢复解的顺序(2) 计算框图startInput A,b,n K=1选主元素k=N?输出Endk=k+1l=k+1i=n?i=i+1从主程序来d=0?输出奇异标志Endl=k?=返回主程序三 实验结果 运行第一次运行第二次 (高斯赛德尔迭代失败)四 心得体会: 此实验在编程时由于循环比较多,所以需要格外注意。为了顺利的看懂算法,我在做实验前仔细的看了看课本,找了一个例题,根据实验步骤一步一步的解出结果这样有利于理解算法的每一步骤究竟实现什么功能,在后来的试验中证明了此举并不是白做,而是很有成效。其他的同学由于不知每一步的具体功能是干什么的,在调试过程中出现了问题却不知道是哪而出现了问题,不容易改正,建议此方法推广使用。此实验还有一个相对难实现的功能即将产生的随机方程组以人容易比较理解的方式输出来。后来通过查阅资料利用两个函数strcat和strvcat,strcat实现将每行的方程连接起来,strvcat实现将每列的方程连接起来成为一个方程组,然后通过set函数输在一个静态文本框中,最后实现功能。还有一个问题由于方程是随机生成的,所以是对角占优方程的可能性较小,所以就通过一个循环将主对角的元素扩大了5倍,这样对角占优的是可能性就大了。五 程序代码function varargout = xianxingfangcheng(varargin)% XIANXINGFANGCHENG M-file for xianxingfangcheng.fig% XIANXINGFANGCHENG, by itself, creates a new XIANXINGFANGCHENG or raises the existing% singleton*.% H = XIANXINGFANGCHENG returns the handle to a new XIANXINGFANGCHENG or the handle to% the existing singleton*.% XIANXINGFANGCHENG(CALLBACK,hObject,eventData,handles,.) calls the local% function named CALLBACK in XIANXINGFANGCHENG.M with the given input arguments.% XIANXINGFANGCHENG(Property,Value,.) creates a new XIANXINGFANGCHENG or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before xianxingfangcheng_OpeningFunction gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to xianxingfangcheng_OpeningFcn via varargin.% *See GUI Options on GUIDEs 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 xianxingfangcheng% Last Modified by GUIDE v2.5 27-Nov-2008 13:59:15% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct(gui_Name, mfilename, . gui_Singleton, gui_Singleton, . gui_OpeningFcn, xianxingfangcheng_OpeningFcn, . gui_OutputFcn, xianxingfangcheng_OutputFcn, . gui_LayoutFcn, , . gui_Callback, );if nargin & ischar(varargin1) gui_State.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 EDIT% - Executes just before xianxingfangcheng is made visible.function xianxingfangcheng_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 xianxingfangcheng (see VARARGIN)% Choose default command line output for xianxingfangchenghandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes xianxingfangcheng wait for user response (see UIRESUME)% uiwait(handles.figure1);% - Outputs from this function are returned to the command line.function varargout = xianxingfangcheng_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 structurevarargout1 = handles.output;function edit1_Callback(hObject, eventdata, handles)% hObject handle to edit1 (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 edit1 as text% str2double(get(hObject,String) returns contents of edit1 as a doublen=str2num(get(hObject,String);data=getappdata(gcbf, mydata);data.n=n;setappdata(gcbf, mydata, data);% - Executes during object creation, after setting all properties.function edit1_CreateFcn(hObject, eventdata, handles)% hObject handle to edit1 (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);enddata=getappdata(gcbf, mydata);data.n=3;setappdata(gcbf, mydata, data);% - Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)data=getappdata(gcbf, mydata);n=data.n;a=rand(n,n);b=rand(n,1);for i=1:n for j=1:n if i=j a(i,j)=a(i,j)*5; end endenddata.a=a;data.b=b;a6=;for i=1:1:n a5=; for j=1:1:n a1=num2str(a(i,j); a2=X; a3=num2str(j); if j=n a4=strcat(=,num2str(b(i); else a4=+; end a5=strcat(a5,a1,a2,a3,a4); end a6=strvcat(a6,a5);endset(findobj(tag,text3),string,a6)setappdata(gcbf, mydata, data);% - Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)data=getappdata(gcbf, mydata);n=data.n;a=data.a;b=data.b;bb=b;aa=a;k=1;for k=1:1:n % d=a(k,k); l=k; for i=k+1:1:n if abs(a(i,k)q q=p; end end if qe y;%jieguo break; end for i=1:1:n x(i)=y(i,1); end if k=N errordlg(die dai shi bai,Information); endendfor i=1:1:n m=0; for j=1:1:n m=m+a(i,j)*y(j,1); end bbb(i,1)=b(i)-m;endbbb;%wuchaa1=;a2=;b1=;b2=;for i=1:n a1=strcat(X,num2str(i),=,num2str(y(i); b1=strcat(e,num2str(i),=,num2str(bbb(i,1); a2=strvcat(a2,a1); b2=strvcat(b2,b1);end% y=num2str(y,1);% bbb=num2str(bbb);set(findobj(tag,text11),string,a2);set(findobj(tag,text13),string,b2);function edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (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 edit3 as text% str2double(get(hObject,String) returns contents of edit3 as a doubleN=str2num(get(hObject,String);data=getappdata(gcbf, mydata);data.N=N;setappdata(gcbf, mydata, data);% - Executes during object creation, after setting all properties.function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (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);enddata=getappdata(gcbf, mydata);data.N=10;setappdata(gcbf, mydata, data);function edit4_Callback(hObject, eventdata, handles)% hObject handle to edit4 (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 edit4 as text% str2double(get(hObject,String) returns contents of edit4 as a doublee=str2num(get(hObject,String);data=getappdata(gcbf, mydata);data.e=e;setappdata(gcbf, mydata, data);% - Executes during object creation, after setting all properties.function edit4_CreateFcn(hObject, eventdata, handles)% hObject handle to edit4 (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,Background

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论