MATLAB编写矩阵计算器
(word完整版)用MATLAB GUI设计简单计算器
摘要基于MATLAB GUI的计算器设计是利用GUIDE创建图形用户界面进行计算器设计。
设计计算器时,主要是考虑到计算器的易用性、功能的常用程度进行计算器界面与功能的设计。
通过调整控件和文本的布局及颜色,使界面简单大方、布局合理,达到界面友好的效果。
计算器设计时主要利用到get和set两个函数进行各个控件属性值的传递和设置。
计算器实现的功能有:数字0~9和小数点的输入显示,平方、开平方和对数的输入显示。
进行四则运算、正弦计算、余弦计算、正切计算和余切计算,可以求阶乘、求百分数和求倒数。
可以进行括号及变量x与变量y的输入,结合坐标轴编辑框和曲线颜色编辑框实现函数的曲线绘制。
最后运行调试,实现基于MATLAB GUI的计算器的设计。
MATLAB GUI介绍MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境。
MATLAB是matrix和laboratory两个词的组合,意为矩阵工厂,主要面对科学计算、可视化以及交互式程设计的高科技计算环境。
MATLAB是将数值分析、矩阵计算、科学数据可视化以及非线性动态系统的建模和仿真等诸多强大功能集成在一个易于使用的视窗环境中,为科学研究、工程设计以及必须进行有效数值计算的众多科学领域提供了一种全面的解决方案,并在很大程度上摆脱了传统非交互式程序设计语言的编辑模式,特别是所附带的30多种面向不同领域的工具箱支持,使得它在许多科学领域中成为计算机辅助设计和分析、算法研究和应用开发的基本工具和首选平台。
MATLAB的图形用户界面(Graphical User Interface,简称GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。
与早期计算机使用的命令行界面相比,图形界面对于用户来说在视觉上更易于接受。
MATLAB的图形用户界(GUI)是由光标,窗口,菜单、文字说明等对象构成一个用户界面。
如何在Matlab中使用矩阵进行计算
如何在Matlab中使用矩阵进行计算导言:在计算机科学和数学领域中,矩阵是一种非常重要的数据结构,广泛应用于各种科学计算和工程领域。
Matlab作为一种专业的数学软件,提供了丰富的矩阵计算功能,方便用户进行各种复杂的数值运算和数据分析。
本文将介绍如何在Matlab中使用矩阵进行计算,并结合实例,帮助读者更好地理解和应用。
一、矩阵的定义和创建在Matlab中,矩阵可以通过直接定义、从数据文件中读取和通过某些函数生成等方式进行创建。
以下是一些常用的创建矩阵的方法:1. 直接定义矩阵:可以通过在Matlab中直接输入元素来定义矩阵。
比如,我们可以通过下面的方式定义一个3×3的矩阵A:```A = [1 2 3; 4 5 6; 7 8 9];```2. 读取数据文件:Matlab可以读取各种格式的数据文件,并将数据存储为矩阵。
比如,可以使用`load`函数从.mat文件中加载矩阵数据,或使用`csvread`函数从.csv文件中加载矩阵数据。
3. 函数生成矩阵:Matlab提供了一些内置函数用于生成各种特殊的矩阵。
比如,可以使用`zeros`函数生成全零矩阵,`eye`函数生成单位矩阵,`rand`函数生成随机矩阵等。
二、矩阵的基本运算在Matlab中,可以使用各种运算符和内置函数对矩阵进行基本运算。
以下是一些常用的矩阵运算示例:1. 加法和减法:两个矩阵的加法和减法可以通过运算符`+`和`-`来实现,矩阵相应位置的元素进行相加或相减。
例如,假设有两个3×3的矩阵A和B,则可以通过以下方式实现矩阵的加法和减法:```C = A + B;D = A - B;```2. 数量乘法和除法:矩阵的数量乘法和除法可以通过运算符`*`和`/`来实现,矩阵中每个元素都与给定的标量进行相乘或相除。
例如,如果有一个3×3的矩阵A 和一个标量b,则可以通过以下方式实现矩阵的数量乘法和除法:```C = A * b;D = A / b;```3. 矩阵乘法和除法:对于矩阵的乘法和除法,需要使用Matlab内置的矩阵运算函数,如`mtimes`和`mldivide`。
如何使用Matlab进行矩阵运算
如何使用Matlab进行矩阵运算随着科学技术的不断发展,矩阵运算在各个领域的应用日益广泛。
Matlab作为一款功能强大的数学软件,其矩阵运算能力非常强大。
本文将介绍如何使用Matlab进行矩阵运算,希望能对读者在科学研究和工程实践中的矩阵计算有所帮助。
一、Matlab的基本矩阵运算1. 创建矩阵在Matlab中,可以使用一对方括号`[]`来创建矩阵。
例如,要创建一个3行3列的矩阵A,可以使用如下命令:A = [1 2 3; 4 5 6; 7 8 9]。
这样就创建了一个元素分别为1到9的3行3列矩阵。
2. 矩阵加法和减法Matlab中可以使用加号和减号来进行矩阵的加法和减法运算。
例如,要计算矩阵A和B的和,可以使用命令C = A + B;要计算矩阵A和B的差,可以使用命令D = A - B。
3. 矩阵乘法Matlab中使用乘号`*`来进行矩阵的乘法运算。
例如,要计算矩阵A和B的乘积,可以使用命令C = A * B。
需要注意的是,矩阵乘法是满足结合律的,即A *(B * C) = (A * B) * C。
4. 矩阵转置在Matlab中,可以使用单引号`'`来对矩阵进行转置操作。
例如,对矩阵A进行转置,可以使用命令B = A'。
需要注意的是,转置操作只能应用于二维矩阵。
5. 求逆矩阵在Matlab中,可以使用inv函数来求解矩阵的逆矩阵。
例如,要求矩阵A的逆矩阵,可以使用命令B = inv(A)。
需要注意的是,只有方阵才有逆矩阵。
6. 矩阵的特征值和特征向量Matlab中可以使用eig函数来求解矩阵的特征值和特征向量。
例如,要求矩阵A的特征值和特征向量,可以使用命令[V,D] = eig(A),其中V为特征向量矩阵,D 为特征值对角矩阵。
二、Matlab的高级矩阵运算1. 矩阵的点乘和叉乘Matlab中使用.*和.^来进行矩阵的点乘和叉乘运算。
例如,要计算矩阵A和B 的点乘,可以使用命令C = A .* B;要计算矩阵A和B的叉乘,可以使用命令D =A .^ B。
matlab 符号矩阵运算
matlab 符号矩阵运算
在MATLAB中进行符号矩阵运算,需要使用符号计算工具箱。
以下是一些常见的符号矩阵运算:
1. 转置:符号矩阵的转置可以通过符号“ ' ”或函数transpos来实现。
例如,如果A是一个符号矩阵,则A.' 是A 的转置。
2. 乘法:两个符号矩阵的乘法可以通过函数mtimes来实现。
例如,如果A和B是两个符号矩阵,则C=A*B是A和B的乘积。
3. 加法:两个符号矩阵的加法可以通过加法运算符“+”来实现。
例如,如果A和B是两个具有相同尺寸的符号矩阵,则
C=A+B是A和B的加积。
4. 逆运算:一个方阵的逆运算可以通过函数inv来实现。
例如,如果A是一个方阵,则inv(A)是A的逆矩阵。
需要注意的是,不是所有的方阵都有逆矩阵。
5. 行列式运算:一个方阵的行列式运算可以通过函数determ或det来实现。
例如,如果A是一个方阵,则det(A)或determ(A)是A的行列式。
6. 求秩运算:一个符号矩阵的求秩运算可以通过函数rank 来实现。
例如,如果A是一个符号矩阵,则rank(A)是A的秩。
7. 特征值和特征向量运算:一个符号矩阵的特征值和特征向量运算可以通过函数eig、eigensys等来实现。
例如,如果A
是一个符号矩阵,则[V,D]=eig(A)将返回特征向量V和特征值D。
以上是一些常见的符号矩阵运算,但MATLAB符号计算工具箱还提供了许多其他函数和运算符来进行符号矩阵运算。
matlab计算矩阵方程组
matlab计算矩阵方程组在数学和工程学中,矩阵方程组是一种常见的问题类型,其中包含一组线性方程,其未知数是矩阵。
MATLAB是一种功能强大的数值计算软件,可以用于求解矩阵方程组。
本文将介绍如何使用MATLAB计算矩阵方程组。
在MATLAB中,可以使用“\”操作符来求解矩阵方程组。
假设有一个矩阵方程组Ax = b,其中A是一个已知的矩阵,b是一个已知的向量,x是未知的向量。
要求解x,可以使用MATLAB的求解器。
要使用“\”操作符求解矩阵方程组,需要将方程组的系数矩阵A和右侧向量b输入到MATLAB中。
假设A是一个3x3的矩阵,b是一个3x1的向量,可以使用以下代码进行求解:```matlabA = [1, 2, 3; 4, 5, 6; 7, 8, 9];b = [1; 2; 3];x = A \ b;```在上述代码中,矩阵A被定义为一个3x3的矩阵,并且向量b被定义为一个3x1的向量。
通过将A和b输入到MATLAB中,并使用“\”操作符,可以得到解x。
解x将存储在名为“x”的变量中。
如果方程组有解,MATLAB将计算并返回x的值。
否则,MATLAB将返回一个近似解。
如果方程组无解或有无穷多解,MATLAB将给出相应的警告。
如果需要计算多个未知数的矩阵方程组,可以将矩阵A定义为一个NxN矩阵,其中N是未知数的数量。
类似地,向量b将成为一个Nx1的向量。
例如,假设有以下4x4的矩阵方程组:```matlabA = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16];b = [1; 2; 3; 4];x = A \ b;```在此示例中,矩阵A是一个4x4的矩阵,向量b是一个4x1的向量。
通过使用“\”操作符进行计算,可以得到解x。
除了使用“\”操作符外,MATLAB还提供了其他求解器,如“inv”和“linsolve”。
这些求解器提供了更多的灵活性和功能,可以用于更复杂的矩阵方程组的求解。
如何在Matlab中进行矩阵运算
如何在Matlab中进行矩阵运算矩阵运算是Matlab中非常重要的一部分,它可以让我们更方便地进行数学建模和算法实现。
在本文中,我们将介绍在Matlab中进行矩阵运算的基本知识和常用函数。
1. 矩阵的定义与表示在Matlab中,可以使用数组来表示矩阵。
我们可以使用一对方括号[],每行之间使用分号; 或者逗号, 来表示不同的矩阵元素。
例如,下面是一个3行3列的矩阵的定义:A = [1, 2, 3; 4, 5, 6; 7, 8, 9];2. 矩阵的基本运算Matlab中的矩阵运算包括加法、减法、乘法和除法等。
下面我们将逐个介绍这些运算。
2.1 矩阵的加法和减法矩阵的加法和减法是逐元素进行的,也就是对应位置的元素相加或相减。
例如,我们有两个矩阵A和B:A = [1, 2; 3, 4];B = [5, 6; 7, 8];我们可以使用矩阵加法和减法运算符+和-,计算出它们的和和差:C = A + B;D = A - B;运行结果为:D = [-4, -4; -4, -4]2.2 矩阵的乘法矩阵的乘法是按照矩阵乘法的规则进行的。
在Matlab中,我们可以使用*或者dot函数进行矩阵的乘法运算。
例如,我们有两个矩阵A和B:A = [1, 2; 3, 4];B = [5, 6; 7, 8];我们可以使用矩阵乘法运算符*,计算出它们的乘积:C = A * B;运行结果为:C = [19, 22; 43, 50]除了使用*号,我们还可以使用dot函数进行矩阵的乘法运算。
例如:D = dot(A, B);运行结果为:D = [19, 22; 43, 50]2.3 矩阵的除法矩阵的除法是矩阵乘法的逆运算。
在Matlab中,我们可以使用/或者inv函数进行矩阵的除法运算。
例如,我们有两个矩阵A和B:A = [1, 2; 3, 4];我们可以使用矩阵除法运算符/,计算出它们的除法结果:C = A / B;运行结果为:C = [-0.3333, -0.6667; -0.1667, -0.3333]除了使用/号,我们还可以使用inv函数进行矩阵的除法运算。
基于MATLAB的简易矩阵计算器
《MATLAB程序设计》程序设计报告设计题目:简易矩阵计算器班级:021231******学号:********目录1.设计目标……………………2.设计内容……………………3.设计思想……………………4.设计说明……………………5.设计步骤及程序代码……………………6.运行计算器……………………7.总结…………………………1.设计目标1.熟悉MATLAB的主要控件使用方法。
2.熟悉MATLAB的GUI设计流程。
2.设计内容设计一个简易的矩阵计算器,通过对简单应用矩阵计算器的设计,编制、调试实现矩阵相加减,矩阵的转置等简单运算,以学习应用MATLAB编写程序原理,加深对MATLAB的学习及应用。
3.设计思想基于MATLAB中GUI界面的设计,通过创建类成员函数成员变量,编辑控件创建消息映射,调用信息函数完成数据的输入输出,实现计算功能。
生成简单的应用软件。
4.设计说明1)包含的功能有:加、减、乘、转置等。
2)输入的原始数据分别为A或B矩阵,分别显示在不同标签中,运算的结果则显示在输出部分。
3)计算功能基本上是用系统内部函数。
4)程序能够自动判断输入数据的正确性,如不能输入(1)。
5.设计步骤及只要代码1)打开MATLAB,点击,出现GUI的2)设计计算器版面直至3)对功能键进行设计执行A矩阵加B矩阵,其执行代码如下A = eval((get(handles.Matrix_A,'String')));B = eval((get(handles.Matrix_B,'String')));[iA jA]=size(A);[iB jB]=size(B);if iA ~= iB | jA ~= jB |(iA ~= iB & jA ~= jB) R='error.Matrix dimensions must agree.';set(handles.Answer,'string',R)guidata(hObject, handles);elseresult=A + B ;R =num2str(result);set(handles.Answer,'string',R)guidata(hObject, handles);end执行A矩阵减B矩阵,其执行代码如下A = eval((get(handles.Matrix_A,'String')));B = eval((get(handles.Matrix_B,'String')));[iA jA]=size(A);[iB jB]=size(B);if iA ~= iB | jA ~= jB |(iA ~= iB & jA ~= jB) R='error.Matrix dimensions must agree.';set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = A - B ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);end执行A矩阵与B矩阵相乘,其执行代码如下A = eval((get(handles.Matrix_A,'String')));B = eval((get(handles.Matrix_B,'String')));[iA jA]=size(A);[iB jB]=size(B);if jA~=iBR='error.Inner matrix dimensions must agree.';set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = A * B ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);end执行A右除B矩阵,其执行代码如下A = eval((get(handles.Matrix_A,'String')));B = eval((get(handles.Matrix_B,'String')));[iA jA]=size(A);[iB jB]=size(B);if iA~=iB 、R='error.Matrix dimensions must agree.';set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = A \ B ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);end执行A.*B,其执行代码如下A = eval((get(handles.Matrix_A,'String')));B = eval((get(handles.Matrix_B,'String')));result = A .* B ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);执行A./B,其执行代码如下A = eval((get(handles.Matrix_A,'String')));B = eval((get(handles.Matrix_B,'String')));result = A ./ B ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);执行A的转置,其执行代码如下A = eval((get(handles.Matrix_A,'String')));result = A' ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);执行A右除B,其执行代码如下A = eval((get(handles.Matrix_A,'String')));B = eval((get(handles.Matrix_B,'String')));[iA jA]=size(A);[iB jB]=size(B);if iA~=iB þ R='error.Matrix dimensions must agree.';set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = A / B ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);end执行求A的行列式,其执行代码如下A = eval((get(handles.Matrix_A,'String')));[iA jA]=size(A);if iA ~=jA R='error.Matrix must be square.';set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = det(A) ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);end执行求A的逆,其执行代码如下A = eval((get(handles.Matrix_A,'String')));[iA jA]=size(A);if iA ~=jAR='error.Matrix must be square.';set(handles.Answer,'String',R)guidata(hObject, handles);elseif det(A) == 0R='error.Matrix is singular to working precision.';set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = inv(A) ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);endend执行求A的非共轭转置,其执行代码如下A = eval((get(handles.Matrix_A,'String')));result = A.' ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);执行求解系数阵为方阵且非奇异的线性方程组,其执行代码如下A = eval((get(handles.Matrix_A,'String')));B = eval((get(handles.Matrix_B,'String')));[iA jA]=size(A);[iB jB]=size(B);if iA ~= iBR='error.';set(handles.Answer,'String',R)guidata(hObject, handles);elseif iA ~=jAR='error.Matrix must be square.';set(handles.Answer,'String',R)guidata(hObject, handles);elseif det(A) == 0R='error.Matrix is singular to working precision.';set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = A\B ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);endendend执行求A的秩,其执行代码如下A = eval((get(handles.Matrix_A,'String')));result = rank(A) ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);执行求A的平方,其执行代码如下A = eval((get(handles.Matrix_A,'String')));[iA jA]=size(A);if iA ~=jAR='error.Inputs must be a scalar and a square matrix.';set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = A^2 ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);end执行求A的立方,其执行代码如下A = eval((get(handles.Matrix_A,'String')));[iA jA]=size(A);if iA ~=jAR='error.Inputs must be a scalar and a square matrix.'; set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = A^3 ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);end执行求A的特征值,其执行代码如下A = eval((get(handles.Matrix_A,'String')));[iA jA]=size(A);if iA ~=jAR='error.Matrix must be square.';set(handles.Answer,'String',R)guidata(hObject, handles);elseresult = eig(A) ;R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);end执行求A开平方根,其执行代码如下A = eval(get(handles.Matrix_A,'String'));result = sqrt(A);R = num2str(result);set(handles.Answer,'String',R)执行计算矩阵范围(默认为2范数),其执行代码如下A = eval(get(handles.Matrix_A,'String'));result = norm(A);R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);执行对A的分解,其执行代码如下A = eval(get(handles.Matrix_A,'String'));[L U] = lu(A);result = [L;U];R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);执行将矩阵A化为最简阶梯矩阵,其执行代码如下A = eval(get(handles.Matrix_A,'String'));result = rref(A);R = num2str(result);set(handles.Answer,'String',R)guidata(hObject, handles);4)对输出进行设计输出框内有新添加的,使经过计算后的式子通过输出框显示。
矩阵计算器
用Matlab GUI编写一个简单的矩阵计算器摘要:矩阵是线性代数的一个主要内容,又是解决众多问题的主要工具。
而矩阵运算是矩阵理论的基本内容之一。
目前,普通的计算器只能够进行数的运算,而不能够实现矩阵的运算。
随着科技的不断发展,人们对矩阵的运用也会不断的增多。
因此,制作一个矩阵计算器对当今科技的发展有一定的推动作用。
关键字:矩阵运算、Matlab GUI、计算器正文:一、矩阵的相关知识在数学上,矩阵是指纵横排列的二维数据表格,最早来自于方程组的系数及常数所构成的方阵。
这一概念由19世纪英国数学家凯利首先提出。
矩阵,是由个数组成的一个行列的矩形表格,通常用大写字母表示,组成矩阵的每一个数,均称为矩阵的元素,通常用小写字母其元素表示,其中下标都是正整数,他们表示该元素在矩阵中的位置。
比如,或表示一个矩阵,下标表示元素位于该矩阵的第行、第列。
元素全为零的矩阵称为零矩阵。
当一个矩阵的行数与烈数相等时,该矩阵称为一个阶方阵。
对于方阵,从左上角到右下角的连线,称为主对角线;而从左下角到右上角的连线称为付对角线。
若一个阶方阵的主对角线上的元素都是,而其余元素都是零,则称为单位矩阵,记为,即:。
如一个阶方阵的主对角线上(下)方的元素都是零,则称为下(上)三角矩阵,例如,是一个阶下三角矩阵,而则是一个阶上三角矩阵。
今后我们用表示数域上的矩阵构成的集合,而用或者表示数域上的阶方阵构成的集合。
矩阵是高等代数学中的常见工具,其中的一个重要用途是解线性方程组。
线性方程组中未知量的系数可以排成一个矩阵,加上常数项,则称为增广矩阵。
另一个重要用途是表示线性变换,即是诸如f(x) 4x之类的线性函数的推广。
同时也常见于统计分析等应用数学学科中。
在物理学中,矩阵于电路学、力学、光学和量子物理中都有应用;计算机科学中,三维动画制作也需要用到矩阵。
矩阵的运算是数值分析领域的重要问题。
将矩阵分解为简单矩阵的组合可以在理论和实际应用上简化矩阵的运算。
如何使用Matlab进行矩阵计算
如何使用Matlab进行矩阵计算使用Matlab进行矩阵计算概述:矩阵计算在科学与工程领域中起着举足轻重的作用。
Matlab作为一种常用的科学计算软件,具备强大的矩阵计算能力。
本文将介绍如何使用Matlab进行矩阵计算,包括矩阵的创建、运算符的应用、线性方程组求解、特征值与特征向量计算以及常见的矩阵分解方法等。
一、矩阵的创建与存储在Matlab中,可以使用多种方式创建矩阵。
最简单的方式是使用方括号将元素排列起来,以分号分隔不同的行。
例如,创建一个3×3的矩阵可以使用以下代码:A = [1, 2, 3; 4, 5, 6; 7, 8, 9];Matlab还提供了一些特殊的函数来创建特定形状或者特定元素的矩阵。
例如,可以使用eye函数创建单位矩阵,zeros函数创建全零矩阵,ones函数创建全一矩阵,rand函数创建随机矩阵等。
创建好矩阵后,可以使用变量名存储矩阵。
在Matlab中,变量名是区分大小写的。
为了更好地组织代码,可以将相关的矩阵存储在不同的变量中,并且使用有意义的变量名。
二、矩阵运算符的应用Matlab中支持各种矩阵运算符的应用,包括加法、减法、数乘、矩阵乘法以及逐元素乘法等。
这些运算符可以方便地用于矩阵计算。
例如,两个矩阵相加可以使用加法运算符"+",两个矩阵相乘可以使用乘法运算符"*",数乘可以使用数值乘法运算符"*"。
需要注意的是,矩阵运算符在运算时需要满足相应的尺寸要求,否则会引发错误。
除了基本的运算符,Matlab还提供了一些特殊的运算符,例如."表示逐元素的乘法(对应位置相乘),./表示逐元素的除法,.^表示逐元素的乘方等。
三、线性方程组求解线性方程组的求解是矩阵计算的重要应用之一。
在Matlab中,可以使用“\”或者inv函数来求解线性方程组。
例如,假设有如下线性方程组 Ax = b,其中A是一个已知的矩阵,b是一个已知的列向量,x是未知的列向量。
用MATLAB做的 一个计算器
一、布局GUI。
1.打开Matlab,输入Guide 回车或者在工具栏上点击图标出现Guide 窗口:2.然后双击“Blank GUI(Default)”出现GUI窗口3.添加按钮4.根据按钮的作用及视觉效果做一定的修改把按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在静态文本框下面。
最终设置的静态文本框为白色,其他按钮均为分红色。
5.保存、添加功能函数把做好的按钮及静态文本框保存后自动弹出Editor的M文本,对然后对相应的pushbutton添加功能函数。
以下是相应按钮的功能函数。
(1)数字按键编写。
在function pushbutton1_Callback(hObject, eventdata, handles)下输入:textString = get(handles.text1,'String');textString =strcat(textString,'0');set(handles.text1,'String',textString)这是使用句柄handles指向对象text1,并以字符串形式来存储数据文本框text1的内容,并存储数个“0”,然后由set(handles.text1,'String','textString'在text1中输出。
同理,分别在function pushbutton2~10_Callback(hObject, eventdata, handles)下给1~9数字按键下编写此类程序。
(2).符号键:function pushbutton12_Callback(hObject, eventdata, handles)textString = get(handles.text1,'String');textString =strcat(textString,'+');set(handles.text1,'String',textString)strcat的作用是将两个字符串连接起来,就是在已输入的存储数据textString 后添加“+”进行运算。
matalb矩阵计算(MATLAB矩阵计算)
matalb矩阵计算(MATLAB矩阵计算)matalb矩阵计算(MATLAB矩阵计算)4.1 array operations and matrix operationsFrom the appearance of the shape and structure of the data matrix, two-dimensional array and no difference in mathematics. However, as the embodiment of a matrix transformation or mapping operator matrix with mathematical rules, clear and strict. And array operation is defined by the software of MATLAB rules, its purpose is for data management, simple operation. The instruction form nature and perform calculations effectively. Therefore, when using MATLAB, in particular to a clear distinction between clear array operations and matrix operations. Table 4.1.1 lists the similarities and differences between the essence and connotation of two kinds of operation instruction.4.1.1 array operations and matrix operations, instruction forms and substantive meaningArray operationMatrix operationinstructionsMeaninginstructionsMeaningA.'Non conjugate transposeA'Conjugate transposeA=sAssign scalar s to each element of the array AS+BAdd scalar s to each element of the array B, respectivelyS-B, B-sThe difference between the scalar s and the elements of the array B S.*AScalar s is the product of the elements of the array A, respectively S*AThe product of the scalar s and the elements of the matrix A, respectivelyS./B, B.\sScalar s is separated by the elements of array B, respectivelyS*inv (B)Inverse multiplicative scalar B of matrix sA.^nThe N sub order of each element of the array AA^nWhen A is a square matrix, the n power of the matrix AA+BThe addition of an array corresponding elementA+Bmatrix additionA-BSubtracting elements of an arrayA-BMatrix subtractionA.*BMultiplication of an array corresponding elementA*BThe product of the same matrix NevilleA./BThe elements of A are excluded by the corresponding elements of B A/BA right, except BB.\AIt must be the sameB\AA left exceptB (usually different from right division)Exp (A)For the end to e respectively with A elements as index, exponentiationExpm (A)Matrix exponential function of ALog (A)The logarithm of the elements of ALogm (A)The matrix logarithmic function of ASqrt (A)The square root of the elements of the product of ASqrtm (A)A's square function of matricesFrom the above we can see that the array operations such as multiplication, addition, multiplication, transpose, add "points". Therefore, we should pay special attention to "multiply, divide, involution, trigonometric and exponential function", two operations are fundamentally different. In addition, in the implementation of the array and array operation when the array must be involved in computing Tongwei, the result is always with the original array array dimensions.4.2 basic operations of an arrayIn MATLAB, an array operation is a.MATLAB that uses the same calculation for multiple numbers to process arrays in a very intuitive way4.2.1 point transpose and conjugate transpose` - dot transpose. Non conjugate transpose, equivalent to conj (A') A=1:5;'b=a.'= bOneTwoThreeFourFive'c=b.'= C12345This shows that the two transpose of the row vector yields the original row vectorConjugate transpose. Transpose the vectors and conjugate each element:D=a+i*a= DColumns 1 through 31 + 1.0000i2 + 2.0000i3 + 3.0000iColumns 4 through 54 + 4.0000i5 + 5.0000iE=d'= e1 - 1.0000i2 - 2.0000i3 - 3.0000i4 - 4.0000i5 - 5.0000i4.2.2 scalar (scalar) and four operations of an arraySimple mathematical operations can be performed between scalar and array, such as addition, subtraction, multiplication, division, and mixing operations"G=[1 234"567891011 12]G=g-2= g-1 0123456789102*g-1= ans-3 -1 135791113151719Four operations between 4.2.3 arraysIn MATLAB, the array between the four operations, an array of participating in operation must have the same dimension, subtraction, multiplication, addition, operation is carried out according to the elements and elements forms. Among them, the array between the addition, subtraction and matrix addition, subtraction to the same operator, as: "+" "-". However, the array multiplication, division and matrix multiplication, division operation symbol is completely different, thereare differences, between the array multiplication and division operators: "*", "/" or ".".1. array by element add, minus"G=[1 234"567891011 12]"H=[1 111; 2222; 333; 3]"> g+h% added by element= ans23457891012131415> ans-h%, subtract by element= ans123456789 10 11 12> > > > > > > 2 g / h) 混合运算years.1 3 5 78 10 12 1415 17 19 212. 按元素乘> > > > > > > g * hyears.1 2 3 410 12 14 1627 30 33 363. 按元素除数组间的除法运算符有两个, 即左除 "/" 和右除 "." 它们之间的关系是.a /b = b.> > > > > > > g / hyears.1.00002.00003.00004.00002.50003.00004.1000 4.00003.0000 3.3333 3.66674.0000"h \ gyears.1.00002.00003.00004.00002.50003.00004.1000 4.00003.0000 3.3333 3.66674.0000幂运算.在matlab中, 数组的幂运算的运算为 "." 表示每一个元素进行幂运算. > > > > > > > (2% 数组g每个元素的平方.years.1 4 9 425, 36, 49, 6481% (144> > > > > > > g ^ (- 1)) 数组g的每个元素的倒数years.1.0000 0.5000 0.3333 0.25000.2000 0.1667 0.1429 0.12500.1111 0.1000 0.0909 0.0833> > > > > > > 2 g%, 以g的每个元素为指数对2进行乘方运算.years.2 4 8 1632, 64, 128, 256512 1024, 2048, 4096> > > > > > > "h% 以h的每个元素为指数对g中相应元素进行乘方运算.years.1 2 3 425, 36, 49, 64729 1000, 1728> > > > > > > (h - 1).years.1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 6 7 881% (144- 数组的指数, 对数和开方运算在matlab中, 所谓数组的运算实质是是数组内部每个元素的运算, 因此, 数组的指数, 对数和开方运算与标量的运算规则完全是一样的, 运算符函数分别为: exp () log (), () must 等.> > a = [1, 3, 4, 5, 2, 6, 3, 2, 4].> > > > > > > c = exp (a)c =2.7183 20.0855 54.59827.3891 403.4288 148.413220.0855 7.3891 54.5982> > > > > > > > > > > >数组的对数, 开方运算与数组的指数运算, 其方式完全一样, 这里不详述.向量运算.对于一行或一列的矩阵, 为向量, matlab有专门的函数来进行向量点积, 叉积和混合积的运算.the 向量的点积运算在高等数学中, 我们知道, 两向量的点积指两个向量在其中一个向量方向上的投影的乘积, 通常用来定义向量的长度.在matlab中, 向量的点积用函数 "dot"来实现, 其调用格式如下.c = dot (a, b) - 返回向量a与b的点积, 结果存放于c中.c = dot (a, b, d) - 返回向量a与b在维数为dim的点积, 结果存放于c中.> > a = [4 2 5 3 1).> > b = (3 8 10 12 13].> > > > > > > c = (a, b).c =137> > > > > > > c = dot (a, b, (4)c =32 50 36 13 6a 向量的叉积运算在高等数学中, 我们知道, 两向量的叉积返回的是与两个向量组成的平面垂直的向量.在matlab中, 向量的点积用函数 cross 来实现, 其调用格式如下.c = cross (a, b) - 返回向量a与b的叉积, 即, 结果存放于c中.c = cross (a, b, d) - 返回向量a与b在维数为dim的叉积, 结果存放于c 中.> > a = [2, 5].> > b = [3, 10].> > > > > > > c = cross (a, b)c =0 - 5 4these 向量的混合运算> > > > > > > d = dot (a, cross (b, c))d =41上例表明, 首先进行的是向量b与c的叉积运算, 然后再把叉积运算的结果与向量a进行点积运算.4.4 矩阵的基本运算如果说matlab的最大特点是强大的矩阵运算功能, 此话毫不为过.事实上, matlab中所有的计算都是以矩阵为基本单元进行的.matlab对矩阵的运算功能最全面, 也是最为强大的.矩阵在形式上与构造方面是等同于前面所述的数组的, 当其数学意义却是完全不同的.矩阵的基本运算包括矩阵的四则运算, 矩阵与标时的运算, 矩阵的幂运算, 指数运算, 对数运算, 开方运算及以矩阵的逆运算, 行列式运算等.it 矩阵的四则运算矩阵的四则运算与前面介绍的数组的四则运算基本相同.但也有一些差别.1. 矩阵的加减The addition and subtraction of a matrix is exactly the same as the addition and subtraction of an array, which requires the exact size of the two matrix to be exactly the same"A=[1 2; 35; 2; 6]";"B=[2 4; 18; 9; 0]";C=a+b= C36413116Multiplication of 2. matricesFor the multiplication of matrices, from linear algebra, we know that the two matrices required to be multiplied have the same common dimension as:"A=[1 2; 35; 2; 6]";"B=[2 41; 89, 0]";C=a*b= C182214657352622A A matrix is a matrix order, will requireB matrix multiplication and must be an order, get the matrix order. That is, only when the first matrix (left column matrix) is equal to the number of second matrices (right matrix) the number of rows, the product of two matrices is meaning.Division of 3. matricesFor the division of a matrix, there are two arithmetic symbols, namely "left" except "symbol" and "right" except "sign" / "." the right division of the matrix is slower, and the "left" operation can avoid the influence of singular matrixFor the equation, if this equation is overdetermined equation, then use division can automatically find the solution of minimizing square. If the equation is Diophantine equation, is used to solve the division operator at least at most rank (A)(rank of matrix A) non zero elements, and the solution of this type of in the solution of a minimum norm."A=[21 3420; 57820; 211417; 3431; 38]";"B=[10 2030, 40]'";X=b\a= x0.7667, 1.1867, 0.8767The above equation is overdetermined. Note that the resulting matrix X is column vector,"A=[21 34205; 78202114; 173431; 38]";"B=[10 20, 30]'";X=b\a= x1.6286, 1.2571, 1.1071, 1.0500The equation above is an indeterminate equationFour operations between 4. matrices and scalarsThe four operations and the four operations of scalar matrixand array and scalar between identical, i.e. each element in the matrix and scalar add, subtract, multiply, with the exception of the four operations. That is, when a division operation is performed, the scalar can only do the dividend.Power operation of 5. matricesThe power operation of a matrix is different from the power operation of a scalar. Using the symbol ^, it is not a power operation for each element of the matrix, but is related to some decomposition of the matrix"B=[21 3420; 782021; 1734; 31]";C=b^2= C3433207417543555376626313536231220156. matrix index, logarithmic and square root operationThe index matrix calculation, logarithmic and square root operations and an array of the corresponding operations are different. It is not a single element in the matrix operations, but for the whole matrix. These operations function as follows:Expm, expm1, expm2, expm3 - exponential arithmetic functions;Logm - logarithmic function;Sqrtm - root operation function."A=[1 34; 265; 32; 4]";> c=expm (a)= C1.0e+004 *0.4668, 0.7694, 0.92000.7919, 1.3065, 1.56130.4807, 0.7919, 0.9475> c=logm (a)= C0.5002 + 2.4406i 0.5960 - 0.6800i 0.7881 - 1.2493i0.4148 + 0.4498i 1.4660 - 0.1253i 1.0108 - 0.2302i0.5780 - 1.6143i 0.4148 + 0.4498i 1.0783 + 0.8263i> c=sqrtm (a)= C0.6190 + 0.8121i 0.8128 - 0.2263i 1.1623 - 0.4157i0.3347 + 0.1497i 2.3022 - 0.0417i 1.1475 - 0.0766i1.0271 - 0.5372i 0.3347 + 0.1497i 1.6461 + 0.2750iTranspose, inverse operation and determinant operation of 7.matricesThe transpose operator of the matrix is', 'and the inverse uses the arithmetic function: inv (), and uses the function: det () to determine the size of the determinant of the matrix"A=[1 20; 25-1; 410; -1]";C=a'= C12425100-1 -1> b=inv (a)= b52 -2-2 -1 10 -2 1> d=det (a)= DOneSpecial operations of 4.5 matricesThe special operations of matrix include matrix eigenvalue operation, conditional number operation, singular value operation, norm operation,rank operation, orthogonalization operation, trace operation, pseudo inverse operation, etc. these operations can be conveniently given by MATLABEigenvalue operations of 4.5.1 matricesIn linear algebra, it is very complicated to compute the eigenvalues of matrices. In MATLAB, the matrix eigenvalue operations are computed only by the function eig () or eigs (). The format is as follows E=eig (X) - generates a column vector consisting of the eigenvalues of the matrix X;[V, D]=eig (X) - generates two matrices V and D, where V is a matrix consisting of the eigenvectors of the matrix X as the column vectors, and D is the diagonal matrix made up of the eigenvalues of the matrix X as the main diagonal elementsThe eigs () function uses the iterative method to solve the eigenvalues and eigenvectors of the matrixD=eigs (X) - generating a column vector consisting of eigenvalues of matrix.X, X must be square, preferably large sparse matrix;[V, D]=eigs (X) - generates two matrices V and D, where V is a matrix consisting of the eigenvectors of the matrix X as the column vectors, and D is the diagonal matrix made up of the eigenvalues of the matrix X as the main diagonal elements"A=[1 20; 25-1; 410; -1]";[b, c]=eig (a)= b-0.2440 -0.9107 0.4472-0.3333 0.3333 0-0.9107 -0.2440 0.8944= C3.7321000.267901Norm operations of 4.5.2 matrices (vectors)In order to reflect the characteristics of some matrix (vector), the norm of the concept of linear algebra, it is divided into 2- norm, 1- norm, infinite norm and Frobenius norm. In MATLAB, norm (or normest)with the function () matrix (vector) norm. The format is as follows.Norm (X) -- the 2- norm of the matrix (vector) X;Norm (X, 2) - ibid;Norm (X, 1) - compute the 1- norm of the matrix (vector) X;Norm (X, INF) -- the infinite norm of the matrix (vector) X;Norm (X,'fro') - compute the Frobenius norm of the matrix (vector) X;Normest (X) -- only the 2- norm of the matrix (vector) X is computed, and the estimate of the 2- norm is applied to the computation of norm (X), which is time-consuming"X=hilb (4)"= X1, 0.5000, 0.3333, 0.25000.5000, 0.3333, 0.2500, 0.20000.3333, 0.2500, 0.2000, 0.16670.2500, 0.2000, 0.1667, 0.1429"Norm (4)"= ansFour> norm (X)= ansOne point five Zero Zero two"Norm (X, 2)= ansOne point five Zero Zero two"Norm (X, 1)= ansTwo point zero eight three three> norm (X, INF)= ansTwo point zero eight three three> norm (X,'fro')= ansOne point five zero nine seven> normest (X)= ansOne point five Zero Zero twoConditional number operation of 4.5.3 matrixThe condition number of a matrix is a quantity of the "morbid" degree of the judgement matrix. The larger the condition number of the matrix A, the more morbid the A is, and conversely, the more "good" state of the A, for example, the Hilbert matrix is a well-known ill conditioned matrixCond (X) - the condition number of the 2- norm of the return matrix X;Cond (X, P) - returns the conditional number of the P- norm of the matrix X, where P is 1,2, inf or fro;Rcond (X) - the inverse value used to compute the condition numberof a matrix. When the matrix X is ill conditioned, rcond (X) is close to 0 and X is "good", and rcond (X) is close to 1.Condest (X) - calculates the estimate of the condition number of the 1- norm of the matrix X"M=magic (3)"= M816357492"H=hilb (4)"= H1, 0.5000, 0.3333, 0.25000.5000, 0.3333, 0.2500, 0.20000.3333, 0.2500, 0.2000, 0.16670.2500, 0.2000, 0.1667, 0.1429> c1=cond (M)= C1Four point three three zero one> c2=cond (M)= C2Four point three three zero one> c3=rcond (M)= C3Zero point one eight seven five> c4=condest (M)= C4Five point three three three three > h1=cond (H)= H11.5514e+004> h2=cond (H, INF)= H22.8375e+004> h3=rcond (H)= H33.5242e-005> h4=condest (H)= H42.8375e+004From the calculation, we can see that the Rubik's cube matrix is better, and the Hilbert matrix is ill conditionedRank of 4.5.4 matrixRank is one of the important concepts in linear algebra, usually by elementary determinant or matrix can be transformed into the column transform, row echelon matrix, the number of rows and the ladder matrix contains nonzero row is a constant, the determination of the non zero line number is the rank of matrix matrix. The rank function (rank) to calculate."T=rand (6)"= T0.9501, 0.4565, 0.9218, 0.4103, 0.1389, 0.01530.2311, 0.0185, 0.7382, 0.8936, 0.2028, 0.74680.6068, 0.8214, 0.1763, 0.0579, 0.1987, 0.44510.4860, 0.4447, 0.4057, 0.3529, 0.6038, 0.93180.8913 0.6154 0.9355 0.8132 0.2722 0.46600.7621 0.7919 0.9169 0.0099 0.1988 0.4186> r =秩(t)R =六由上计算可知,矩阵T为满秩矩阵。
怎么用matlab进行矩阵运算?
怎么⽤matlab进⾏矩阵运算?MATLAB具有⼏乎所有类型的通⽤矩阵计算功能。
有函数可以得到特征值。
命令⾏键⼊:eig(A)点击“Enter”键。
如图12所⽰。
以及单值。
命令⾏键⼊:svd(A)点击“Enter”键。
如图13所⽰。
“多”函数⽣成⼀个包含特征多项式系数的向量。
矩阵a的特征多项式是:det(λI-A)。
命令⾏键⼊:p = round(poly(A))点击“Enter”键。
如图14所⽰。
我们可以很容易地找到⼀个多项式的根使⽤根函数。
这些实际上是原始矩阵的特征值。
命令⾏键⼊:roots(p)点击“Enter”键。
如图15所⽰。
MATLAB在矩阵计算之外有许多应⽤。
要卷积两个向量。
.命令⾏键⼊:q = conv(p,p)点击“Enter”键。
如图16所⽰。
...或再次卷积并绘制结果。
命令⾏键⼊:r = conv(p,q)plot(r);点击“Enter”键。
如图17、18所⽰。
在任何时候,我们都可以使⽤who或whos命令获得存储在内存中的变量列表。
命令⾏键⼊:whos 点击“Enter”键。
如图19所⽰。
通过键⼊特定变量的名称,可以获得该变量的值。
命令⾏键⼊:A点击“Enter”键。
如图20所⽰。
通过⽤逗号或分号分隔每个语句,可以在⼀⾏上有不⽌⼀条语句。
如果您没有分配⼀个变量来存储⼀个操作的结果,那么结果将存储在⼀个名为ans的临时变量中。
命令⾏键⼊:sqrt(-1)点击“Enter”键。
如图21所⽰。
正如你所看到的,MATLAB在计算中很容易处理复数。
以上就是⼩编为⼤家带来的怎么⽤matlab进⾏矩阵运算全部内容,希望对⼤家有所帮助更多内容请继续关注。
上⼀页1 2下⼀页阅读全⽂。
如何在Matlab中进行矩阵运算和线性代数计算
如何在Matlab中进行矩阵运算和线性代数计算矩阵运算和线性代数计算在科学计算中起着至关重要的作用。
Matlab是一种强大的科学计算软件,其中内置了丰富的矩阵运算和线性代数函数,使得我们能够轻松地进行各种复杂的数学计算。
本文将介绍如何在Matlab中进行矩阵运算和线性代数计算。
一、矩阵的表示与生成在Matlab中,我们可以使用一对方括号来表示矩阵。
例如,下面的代码可以生成一个3×3的矩阵A:```A = [1, 2, 3; 4, 5, 6; 7, 8, 9];```我们也可以使用内置函数来生成一些特殊矩阵,如零矩阵、单位矩阵和对角矩阵。
下面的代码分别生成一个3×3的零矩阵B、一个3×3的单位矩阵C和一个对角元素为1、2、3的对角矩阵D:```B = zeros(3);C = eye(3);D = diag([1, 2, 3]);```二、矩阵的基本运算1. 矩阵的加法和减法在Matlab中,我们可以使用运算符"+"和"-"对矩阵进行加法和减法运算。
下面的代码演示了如何对两个3×3的矩阵A和B进行加法和减法运算:```A = [1, 2, 3; 4, 5, 6; 7, 8, 9];B = [9, 8, 7; 6, 5, 4; 3, 2, 1];C = A + B; % 矩阵相加D = A - B; % 矩阵相减```2. 矩阵的乘法矩阵的乘法在Matlab中使用运算符"*"表示。
下面的代码演示了如何对两个矩阵A和B进行乘法运算:```A = [1, 2, 3; 4, 5, 6]; % 2×3 矩阵B = [7, 8; 9, 10; 11, 12]; % 3×2 矩阵C = A * B; % 矩阵相乘```需要注意的是,矩阵的乘法是满足结合律的,但不满足交换律。
也就是说,如果A是m×n的矩阵,B是n×p的矩阵,那么A*B是m×p的矩阵,而B*A是n×n 的矩阵。
matlab矩阵运算程序
matlab矩阵运算程序Matlab 是一种强大的数学计算软件,特别适用于矩阵运算。
以下是一个简单的 Matlab 程序,演示了一些基本的矩阵运算,包括矩阵的创建、加法、乘法、转置等。
% 创建矩阵 A 和 BA = [1, 2, 3; 4, 5, 6; 7, 8, 9];B = [9, 8, 7; 6, 5, 4; 3, 2, 1];% 显示矩阵 A 和 Bdisp('Matrix A:');disp(A);disp('Matrix B:');disp(B);% 矩阵加法C = A + B;disp('Matrix Sum (A + B):');disp(C);% 矩阵乘法D = A * B;disp('Matrix Product (A * B):');disp(D);% 矩阵转置E = A';disp('Transpose of Matrix A:');disp(E);% 矩阵元素相乘F = A .* B;disp('Element-wise Product of A and B:'); disp(F);% 矩阵求逆G = inv(A);disp('Inverse of Matrix A:');disp(G);% 求矩阵行列式detA = det(A);disp('Determinant of Matrix A:');disp(detA);这个程序演示了一些常见的矩阵运算,你可以通过复制粘贴到Matlab 中执行。
请注意,矩阵乘法使用 *,而矩阵元素相乘使用 .*。
矩阵求逆和行列式的计算要求矩阵是可逆的,因此程序中使用的矩阵A 需要是可逆矩阵。
Matlab实现的简易计算器程序代码
function varargout = jisuanqi(varargin)% JISUANQI M-file for jisuanqi.fig% JISUANQI, by itself, creates a new JISUANQI or raises the existing % singleton*.%% H = JISUANQI returns the handle to a new JISUANQI or the handle to % the existing singleton*.%% JISUANQI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in JISUANQI.M with the given input arguments. %% JISUANQI('Property','Value',...) creates a new JISUANQI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before jisuanqi_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application % stop. All inputs are passed to jisuanqi_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% Edit the above text to modify the response to help jisuanqi% Last Modified by GUIDE v2.5 20-Jul-2011 09:45:20% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @jisuanqi_OpeningFcn, ...'gui_OutputFcn', @jisuanqi_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before jisuanqi is made visible.function jisuanqi_OpeningFcn(hObject, eventdata, handles, varargin) handles.current_str='';%´ËΪ´æ´¢µ±Ç°µÄ×Ö·û´®handles.L1_str='';handles.L2_str='';%´ËΪ´æ´¢ÉÏÒ»¸öÊý×ÖµÄ×Ö·û´®% 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 jisuanqi (see VARARGIN)% Choose default command line output for jisuanqihandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes jisuanqi wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. function varargout = jisuanqi_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 structurevarargout{1} = handles.output;% --- Executes on button press in Number_0.function Number_0_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ0£¬Èç¹û°´ÁË0¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'0');handles.L1_str=strcat(handles.L1_str,'0');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_0 (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 xiaoshudian.function xiaoshudian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'.');guidata(hObject, handles);%СÊýµã% hObject handle to xiaoshudian (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 jia_jian.function jia_jian_Callback(hObject, eventdata, handles)% hObject handle to jia_jian (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 Number_1.function Number_1_Callback(hObject, eventdata, handles)% hObject handle to Number_1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ1£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'1');handles.L1_str=strcat(handles.L1_str,'1');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% --- Executes on button press in Number_2.function Number_2_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ2£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'2');handles.L1_str=strcat(handles.L1_str,'2');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_2 (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 Number_3.function Number_3_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ3£¬Èç¹û°´ÁË3¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'3');handles.L1_str=strcat(handles.L1_str,'3');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_3 (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 Number_4.function Number_4_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ4£¬Èç¹û°´ÁË4¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'4');handles.L1_str=strcat(handles.L1_str,'4');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_4 (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 Number_5.function Number_5_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ5£¬Èç¹û°´ÁË5¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'5');handles.L1_str=strcat(handles.L1_str,'5');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_5 (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 Number_6.function Number_6_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ6£¬Èç¹û°´ÁË6¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'6');handles.L1_str=strcat(handles.L1_str,'6');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_6 (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 Number_7.function Number_7_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ7£¬Èç¹û°´ÁË7¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'7');handles.L1_str=strcat(handles.L1_str,'7');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_7 (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 Number_8.function Number_8_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ8£¬Èç¹û°´ÁË8¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'8');handles.L1_str=strcat(handles.L1_str,'8');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_8 (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 Number_9.function Number_9_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ9£¬Èç¹û°´ÁË9¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'9');handles.L1_str=strcat(handles.L1_str,'9');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_9 (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 jiahao.function jiahao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'+');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jiahao (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 denghao.function denghao_Callback(hObject, eventdata, handles)st = get(handles.edit1,'String');%¼ÆËã½á¹û£¬²¢´æ·Åµ½µÚ¶þ¸ö±à¼-¿òÖС£val = eval(st);s = num2str(val);set(handles.edit2,'String',s);guidata(hObject, handles);% hObject handle to denghao (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 jianhao.function jianhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'-');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jianhao (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 X_daoshu.function X_daoshu_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'\1');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_daoshu (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 chenghao.function chenghao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'*');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chenghao (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 baifenhao.function baifenhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/100');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);handles.L2_str=handles.L1_str;handles.L1_str = '';% hObject handle to baifenhao (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 chuhao.function chuhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chuhao (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 Sqrt.function Sqrt_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sqrt');handles.L1_str=strcat(handles.L1_str,'sqrt');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Sqrt (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 X_lifang.function X_lifang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^3');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_lifang (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 x_pingfang.function x_pingfang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^2');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to x_pingfang (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 zhengqie.function zhengqie_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'tan');handles.L1_str=strcat(handles.L1_str,'tan');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengqie (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 zhengxian.function zhengxian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sin');handles.L1_str=strcat(handles.L1_str,'sin');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengxian (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 CE.function CE_Callback(hObject, eventdata, handles)% hObject handle to CE (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 C.function C_Callback(hObject, eventdata, handles)% hObject handle to C (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.current_str = '';set(handles.edit1,'String','0.');set(handles.edit2,'String','0.');guidata(hObject, handles);% --- Executes on button press in Backspace.function Backspace_Callback(hObject, eventdata, handles)% hObject handle to Backspace (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)s1 = handles.current_str;handles.current_str = s1(1:length(s1)-1);set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);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 double% --- 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 && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endif ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')) ;end% --- Executes on button press in shujujiazai.function shujujiazai_Callback(hObject, eventdata, handles)% hObject handle to shujujiazai (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global YSData;[filename,pathname,FILTERINDEX]=uigetfile({'*.xls';'*.dat';'*.*'},'Ñ¡ÔñÊý¾ÝÎļþ');if(FILTERINDEX==0)return;endstr_filename=[pathname,filename];fid=fopen(str_filename,'rt');if(fid==-1)errordlg('Open file error!','Open error');return;endYSData=xlsread(str_filename);set(handles.lujingxianshi,'String',str_filename);set(handles.tuxingxianshi,'Enable','on');function lujingxianshi_Callback(hObject, eventdata, handles)% hObject handle to lujingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.lujingxianshi,'String',pathname);% Hints: get(hObject,'String') returns contents of lujingxianshi as text % str2double(get(hObject,'String')) returns contents of lujingxianshi as a double% --- Executes during object creation, after setting all properties. function lujingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to lujingxianshi (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 zhuxingtu.function zhuxingtu_Callback(hObject, eventdata, handles)% hObject handle to zhuxingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',1);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhuxingtu% --- Executes on button press in zhexiantu.function zhexiantu_Callback(hObject, eventdata, handles)% hObject handle to zhexiantu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',1);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhexiantu% --- Executes on button press in yuanbingtu.function yuanbingtu_Callback(hObject, eventdata, handles)% hObject handle to yuanbingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',1);% Hint: get(hObject,'Value') returns toggle state of yuanbingtu% --- Executes on button press in tuxingxianshi.function tuxingxianshi_Callback(hObject, eventdata, handles)global YSData;%Åжϡ®Í¼ÏñÀàÐÍ¡¯ÏÂ×éºÏ¿òÖеĸ÷µ¥Ñ¡°´Å¥µÄÑ¡ÖÐÇé¿ö£¬²¢´æ´¢ÔÚIndex_rad iobuttonÖÐIndex_radiobutton=get([handles.zhuxingtu,handles.zhexiantu,handles.yuan bingtu],'Value');%½øÐÐͼÐÎÉú³Éif(Index_radiobutton{1}==1)bar(YSData);title('¸÷Êý¾ÝÖ±·½Í¼');endif(Index_radiobutton{2}==1)plot(YSData);title('¸÷Êý¾ÝÕÛÏßͼ');endif(Index_radiobutton{3}==1)pie(YSData);title('¸÷Êý¾ÝÔ²±ýͼ');end% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% global YSData;% --- Executes during object creation, after setting all properties. function tuxingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called function edit2_Callback(hObject, eventdata, handles)% hObject handle to edit2 (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 edit2 as text% str2double(get(hObject,'String')) returns contents of edit2 as a double% --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles)% hObject handle to edit2 (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 Quit.function Quit_Callback(hObject, eventdata, handles)% hObject handle to Quit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)close(gcf);% --- Executes during object deletion, before destroying properties. function zhengxian_DeleteFcn(hObject, eventdata, handles)% hObject handle to zhengxian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)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 double% --- 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 && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end。
MATLAB设计矩阵计算器
MATLAB程序设计语言大作业——矩阵计算器题目:矩阵计算器学号:02123010姓名:石星宇指导教师:汤建龙一、实验目的1、通过GUI(Graphical User Interface,简称GUI,图形用户界面,又称图形用户接口)设计矩阵计算器,熟悉MATLAB中GUI工具的使用和编程。
2、加深对MATLAB GUI中句柄控制变量的理解。
3、巩固线性代数中矩阵的基本运算。
二、实验内容1、利用MATLAB中的GUIDE工具箱快速生成GUI界面。
2、在生成的GUI界面中设计能够实现矩阵基本运算的矩阵计算器。
三、实验结果通过GUIDE快速生成GUI界面设计的矩阵计算器如下图所示:图1 Matrix Calculator基本界面该矩阵计算器包含四模块,分别为:输入区、输出区、功能区及其他运算区。
四、功能描述1、功能概述本矩阵计算器能够实现一些基本的矩阵运算,包括对单个矩阵的运算以及对两个矩阵的运算。
其中,对于两个矩阵的运算有:加、减、乘、除(左除、右除)、按元素乘、按元素除以及求解线性方程组;对于単个矩阵的运算有:转置(共轭与非共轭)、求逆、计算行列式、求秩、平方、立方、开方、求特征值、求2范数、LU 分解、最简阶梯阵化简等。
计算过程中,矩阵的输入方式与MATLAB 中矩阵输入方式类似。
即①矩阵行中的元素以空格或逗号间隔;②矩阵行之间以分号间隔;③整个元素列表用方括号括起来。
2、功能详细描述 2.1矩阵相加(减)输入A=[1 2 3;4 5 6;7 8 9],B=[1 4 7;2 5 8;3 6 9],按下“+”按钮,输出区显示计算结果如图2所示:输入的A 、B 矩阵必须是维数相同,否则不能实现相加运算。
如输入不同维数的矩阵且进行相加运算,则会提示出错如图3所示:两矩阵相减的运算过程与相加运算类似,在此不再赘述。
图2 矩阵相加图3 矩阵相加错误提示2.2矩阵相乘输入A=[1 2 3;4 5 6;7 8 9],B=[1 4 7;8 5 2;3 9 6],按下“*”按钮,输出区显示计算结果如图4所示:输入的A 、B 矩阵必须满足前者的列数等于后者的行数,否则不能实现矩阵的乘法运算,出现错误提示如图5所示:2.3按元素乘(除)输入同2.2。
MATLAB做简易计算器
MATLAB做简易计算器1、数字键的编写:textstring=get(handles.text1,'string');textstring=strcat(textstring,'0');set(handles.text1,'string',textstring);这是使用句柄handles指向对象text1,并以字符串形式来存储数据文本框text1的内容,并存储数个“0”,然后由set(handles.text1,'String','textString')在text1中输出。
同理,1~9也是这样实现。
2、符号键:textstring=get(handles.text1,'string');textstring=strcat(textstring,'+');set(handles.text1,'string',textstring);strcat的作用是将两个字符串连接起来,就是在已输入的存储数据textString后添加“+”进行运算。
然后执行set(handles.text1,'String',textString)。
符号键‘-’、‘*’、‘/’与‘+’的运算函数类似。
3、等于键:textstring=get(handles.text1,'string');s=eval(textstring);set(handles.text1,'string',s);“eval”的作用是将符号表达式转换成数值表达式。
再由set(handles.text1,'String',s)输出4、清除键:set(handles.text1,'string',' ');计算器使用:‘+’‘=’后:源程序:function varargout = jisuanqi(varargin)% JISUANQI MATLAB code for jisuanqi.fig% JISUANQI, by itself, creates a new JISUANQI or raises the existing % singleton*.%% H = JISUANQI returns the handle to a new JISUANQI or the handle to % the existing singleton*.%% JISUANQI('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in JISUANQI.M with the given input arguments.%% JISUANQI('Property','Value',...) creates a new JISUANQI or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before jisuanqi_OpeningFcn gets called. An% unrecognized property name or invalid value makes propertyapplication% stop. All inputs are passed to jisuanqi_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% Edit the above text to modify the response to help jisuanqi% Last Modified by GUIDE v2.5 04-May-2015 16:44:30% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @jisuanqi_OpeningFcn, ...'gui_OutputFcn', @jisuanqi_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before jisuanqi is made visible.function jisuanqi_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 jisuanqi (see VARARGIN)% Choose default command line output for jisuanqihandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes jisuanqi wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. function varargout = jisuanqi_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 structurevarargout{1} = handles.output;% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'1');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'2');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'3');set(handles.text1,'string',textstring);% hObject handle to pushbutton3 (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 pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'+');set(handles.text1,'string',textstring);% hObject handle to pushbutton4 (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 pushbutton9.function pushbutton9_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'4');set(handles.text1,'string',textstring);% hObject handle to pushbutton9 (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 pushbutton10.function pushbutton10_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'5');set(handles.text1,'string',textstring);% hObject handle to pushbutton10 (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 pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'6');set(handles.text1,'string',textstring);% hObject handle to pushbutton11 (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 pushbutton12.function pushbutton12_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'-');set(handles.text1,'string',textstring);% hObject handle to pushbutton12 (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 pushbutton13.function pushbutton13_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'7');set(handles.text1,'string',textstring);% hObject handle to pushbutton13 (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 pushbutton14.function pushbutton14_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'8');set(handles.text1,'string',textstring);% hObject handle to pushbutton14 (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 pushbutton15.function pushbutton15_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'9');set(handles.text1,'string',textstring);% hObject handle to pushbutton15 (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 pushbutton16.function pushbutton16_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'*');set(handles.text1,'string',textstring);% hObject handle to pushbutton16 (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 pushbutton17.function pushbutton17_Callback(hObject, eventdata, handles);set(handles.text1,'string',' ');% hObject handle to pushbutton17 (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 pushbutton18.function pushbutton18_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'.');set(handles.text1,'string',textstring);% hObject handle to pushbutton18 (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 pushbutton19.function pushbutton19_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');s=eval(textstring);set(handles.text1,'string',s);% hObject handle to pushbutton19 (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 pushbutton20.function pushbutton20_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'/');set(handles.text1,'string',textstring);% hObject handle to pushbutton20 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)。
如何在Matlab中进行矩阵操作和计算
如何在Matlab中进行矩阵操作和计算在Matlab中进行矩阵操作和计算Matlab是一种用于数值计算和可视化的高级程序语言,广泛应用于科学计算、工程设计、统计分析等领域。
其中,矩阵操作和计算是Matlab的核心功能之一。
在本文中,我们将探讨如何利用Matlab进行矩阵操作和计算的一些基本技巧和高级功能。
一、创建矩阵在Matlab中创建矩阵非常简单。
我们可以使用特定的语法来定义一个矩阵,并赋予其初值。
例如,我们可以使用方括号将矩阵的元素排列成行或列的形式,用逗号或空格分隔开每个元素。
```MatlabA = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % 创建一个3x3的矩阵B = [10 11 12; 13 14 15; 16 17 18]; % 创建一个3x3的矩阵```除此之外,我们还可以使用内置函数来创建特殊类型的矩阵,如单位矩阵、零矩阵、对角矩阵等。
```MatlabC = eye(3); % 创建一个3x3的单位矩阵D = zeros(2, 4); % 创建一个2x4的零矩阵E = diag([1 2 3]); % 创建一个对角矩阵,对角线元素分别为1、2、3```二、矩阵运算Matlab提供了丰富的矩阵运算函数,方便我们进行各种矩阵操作。
例如,我们可以使用加法、减法、乘法、除法等运算符对矩阵进行基本的运算。
```MatlabF = A + B; % 矩阵相加G = A - B; % 矩阵相减H = A * B; % 矩阵相乘I = A / B; % 矩阵相除```此外,Matlab还提供了求转置、求逆、求行列式等常用的矩阵运算函数,可以通过调用这些函数来完成相应的操作。
```MatlabJ = transpose(A); % 求矩阵A的转置K = inv(A); % 求矩阵A的逆矩阵L = det(A); % 求矩阵A的行列式```三、矩阵索引与切片在Matlab中,我们可以使用索引和切片操作来访问矩阵的特定元素或子矩阵。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
一、GUI开发环境简介MATLAB提供了一套可视化的创建图形窗口的工具,使用用户界面开发环境可方便的创建GUI应用程序, 它可以根据用户设计的GUI布局,自动生成M文件的框架,用户使用这一框架编制自己的应用程序。
图形用户界面(Graphical User Interfaces ,GUI)则是由窗口、光标、按键、菜单、文字说明等对象(Objects)构成的一个用户界面。
用户通过一定的方法(如鼠标或键盘)选择、激活这些图形对象,使计算机产生某种动作或变化,比如实现计算、绘图等。
二、矩阵计算器介绍2.1 程序使用的控件在矩阵计算器设计中主要用到三种控件,文本编辑框(edit text),静态文本框(Static text),命令按钮(push button),边框(panel)。
然后在通过各个按钮的回调函数,实现简单的计算功能。
2.2 程序功能介绍(1)具有用户图形界面。
实现矩阵的加、减、乘、左除、右除等简单计算。
(2)有清空键,能清除操作。
2.3 矩阵计算器界面及主要控件功能注释:text1文本编辑框控件功能:输出要计算公式。
如[12 45;78 56]+[659 238;79 56] MatA文本编辑框功能:输入参与计算矩阵AMatB文本编辑框功能:输入参与计算矩阵BMatC静态文本框功能:输出计算结果矩阵C三、部分计算操作结果(1) 加法+MatAtext1 pushbutton22pushbutton23pushbutton24pushbutton20(2)求逆(3)转置(4)群运算.*四、部分模块功能算法的实现(1)按键’加法+’响应:a=get(handles.matA,'string');textString=strcat(a,'+');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);(2)按键’转置’响应a=get(handles.matA,'string');textString=strcat(a,'''');set(handles.text1,'String',textString);(3)按键’计算’响应textString=get(handles.text1,'String');f=eval(textString);set(handles.matC,'string',num2str(f,'%15.5g'))(5)按键’清空’响应set(handles.text1,'String','')set(handles.matA,'String','[]')set(handles.matB,'String','[]')set(handles.matC,'String','')注释:其余模块功能算法与以上代码较为相似,不在一一举例。
五、其余传参方法本程序通过文本编辑框(edit text)达到传参的目的,下面介绍两种常用的其他传参方式。
(1)运用global定义全局变量传递参数这种方法适用于gui内控件间以及不同gui间,是最简单的方式。
使用方法:gloal xxxx在每一个要到该全局变量的地方,都要添一句gloal xxxx。
缺点:全局变量破坏了程序的封装性,所以,全局变量是能少用尽量少用。
(2)运用UserData传递参数直接通过对象的userdata属性进行各个callback之间的数据存取操作。
首先必须将数据存储到一个特定的对象中,假设对象的句柄值为handles.text1,需要存储的值为value,则输入以下程序即可:set(' handles.text1,'UserData',Value);此时,value数据就存在句柄值为ui_handle的对象内,在执行的过程中若要取回变量可以通过以下方式在任意callback中获取该数据值:value=get('' handles.text1,'UserData');缺点:每个对象仅能存取一个变量值,因此当同一对象存储两次变量时,先前的变量值就会被覆盖掉,因此都用UserData存储简单与单一的数据。
六、心得这次作业在MATLAB GUI编程中是较为简单的,虽然老师当天布置的作业我当天完成,但是这期间还是遇到一些困难,比如起初我的程序计算结果矩阵C用文本编辑框(edit text),我的代码是正确的,但是却得不到结果。
最后通过上网查资料,解决了该问题。
同时我对MATLAB有了新的认识。
1、通过MATLAB矩阵计算器的设计,初步了解了关于MATLAB图形用户界面的部分控件的使用方法。
2、MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。
3、Matlab具有强大、丰富的内置函数和工具箱,界面设计时更加简洁、快捷与直观。
七、详细代码function varargout = untitled1(varargin)gui_Singleton = 1;gui_State = struct('gui_Name', m, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @untitled1_OpeningFcn, ...'gui_OutputFcn', @untitled1_OutputFcn, ...'gui_LayoutFcn', [], ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_mainfcn(gui_State, varargin{:});endfunction untitled1_OpeningFcn(hObject, eventdata, handles, varargin) handles.output = hObject;guidata(hObject, handles);function varargout = untitled1_OutputFcn(hObject, eventdata, handles) varargout{1} = handles.output;function calculate_Callback(hObject, eventdata, handles)textString=get(handles.text1,'String');f=eval(textString);set(handles.matC,'string',num2str(f,'%15.5g'));function button1_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'+');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);function button2_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'-');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);function button3_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'*');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);function button4_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'\');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);function button5_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'/');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);function button6_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'^-1');set(handles.text1,'String',textString);function button7_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'''');set(handles.text1,'String',textString);function matA_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction matB_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction matC_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction text10_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction text1_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction pushbutton11_Callback(hObject, eventdata, handles)set(handles.text1,'String','');set(handles.matA,'String','[]');set(handles.matB,'String','[]');set(handles.matC,'String','');function pushbutton20_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'.*');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);function pushbutton21_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'./');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);function pushbutton22_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'.\');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);function pushbutton23_Callback(hObject, eventdata, handles)a=get(handles.matA,'string');textString=strcat(a,'.^');b=get(handles.matB,'string');textString=strcat(textString,b);set(handles.text1,'String',textString);function pushbutton24_Callback(hObject, eventdata, handles) a=get(handles.matA,'string');textString=strcat(a,'.''');set(handles.text1,'String',textString);。