matlab实验报告模板

合集下载

MATLAB实验报告

MATLAB实验报告

MATLAB实验报告一、实验目的本次 MATLAB 实验旨在深入了解和掌握 MATLAB 软件的基本操作和应用,通过实际编程和数据处理,提高解决问题的能力,培养编程思维和逻辑分析能力。

二、实验环境本次实验使用的是 MATLAB R2020a 版本,运行在 Windows 10 操作系统上。

计算机配置为英特尔酷睿 i5 处理器,8GB 内存。

三、实验内容(一)矩阵运算1、矩阵的创建使用直接输入、函数生成和从外部文件导入等方式创建矩阵。

例如,通过`1 2 3; 4 5 6; 7 8 9` 直接输入创建一个 3 行 3 列的矩阵;使用`ones(3,3)`函数创建一个 3 行 3 列元素全为 1 的矩阵。

2、矩阵的基本运算包括矩阵的加减乘除、求逆、转置等。

例如,对于两个相同维度的矩阵`A` 和`B` ,可以进行加法运算`C = A + B` 。

3、矩阵的特征值和特征向量计算通过`eig` 函数计算矩阵的特征值和特征向量,加深对线性代数知识的理解和应用。

(二)函数编写1、自定义函数使用`function` 关键字定义自己的函数,例如编写一个计算两个数之和的函数`function s = add(a,b) s = a + b; end` 。

2、函数的调用在主程序中调用自定义函数,并传递参数进行计算。

3、函数的参数传递了解值传递和引用传递的区别,以及如何根据实际需求选择合适的参数传递方式。

(三)绘图功能1、二维图形绘制使用`plot` 函数绘制简单的折线图、曲线等,如`x = 0:01:2pi; y = sin(x); plot(x,y)`绘制正弦曲线。

2、图形的修饰通过设置坐标轴范围、标题、标签、线条颜色和样式等属性,使图形更加清晰和美观。

3、三维图形绘制尝试使用`mesh` 、`surf` 等函数绘制三维图形,如绘制一个球面`x,y,z = sphere(50); surf(x,y,z)`。

(四)数据处理与分析1、数据的读取和写入使用`load` 和`save` 函数从外部文件读取数据和将数据保存到文件中。

MATLAB实验报告(打印版)

MATLAB实验报告(打印版)

MATLAB实验报告班别:09电气3班姓名:李嘉明学号:200924122301实验一/二 MATLAB的基础操作3.自定义一个变量,然后分别用8种不同的数字显示格式显示查看。

>> a=[1.1,1.2,1.3;1.4,1.5,1.6;1.7,1.8,1.9]a =1.1000 1.2000 1.30001.4000 1.5000 1.60001.7000 1.8000 1.9000>> format long>> aa =1.100000000000000 1.200000000000000 1.3000000000000001.400000000000000 1.500000000000000 1.6000000000000001.700000000000000 1.800000000000000 1.900000000000000>> format short e>> aa =1.1000e+000 1.2000e+000 1.3000e+0001.4000e+000 1.5000e+000 1.6000e+0001.7000e+000 1.8000e+000 1.9000e+000>> format long e>> aa =1.100000000000000e+000 1.200000000000000e+000 1.300000000000000e+0001.400000000000000e+000 1.500000000000000e+000 1.600000000000000e+0001.700000000000000e+000 1.800000000000000e+000 1.900000000000000e+000 >> format hex>> aa =3ff199999999999a 3ff3333333333333 3ff4cccccccccccd3ff6666666666666 3ff8000000000000 3ff999999999999a3ffb333333333333 3ffccccccccccccd 3ffe666666666666>> format +>> aa =+++++++++>> format rat>> aa =11/10 6/5 13/107/5 3/2 8/517/10 9/5 19/10>> format short>> aa =1.1000 1.2000 1.30001.4000 1.5000 1.60001.7000 1.8000 1.90005、己举例比较给矩阵的全行赋值,取出矩阵中某一个或几个元素,然后组成一个新的矩阵。

MATLAB实验报告(四份!!!)

MATLAB实验报告(四份!!!)

0380160815880001449451490168779983287916534749293454515731] >> double(x) ans = -33.5325 60.6078 -54.7098 27.3192 实验三 MATLAB 绘图
一 实验环境 计算机 MATLAB 软件 二 实验目的 1. 掌握 MATLAB 的基本绘命令。 2. 掌握运用 MATLAB 绘制一维、二维、三维的图形方法。 3. 给图形加以修饰。 三 预备知识 特殊的二维图形函数有:直方图、柄图、阶梯图、饼图、频数累计柱状 图、极坐标图 四 实验内容和步骤
3.创建一个 3x3 矩阵,并求其转置,逆矩阵。 >> A=magic(3) A= 8 3 4 >> A' ans = 8 1 6 >> inv(A) 3 5 7 4 9 2 1 5 9 6 7 2
ans = 0.1472 -0.0611 -0.0194 -0.1444 0.0222 0.1889 0.0639 0.1056 -0.1028
3. 画出 y=的曲线(x(-5,5)) 。在这曲线上加入相同区间里的 y=的曲线,并且采用绿色折 线标识。 >> x=linspace(-5,5,100); >> y=x.^2; >> plot(x,y) >> hold on >> z=x.^(1/3); >> plot(x,z,'g--')
4. 在同一窗口不同坐标系里分别绘出 y1=sinx,y2=cosx,y3=cinh(x),y4=cosh(x)4 个图形。 >> x=linspace(0,2*pi,30); >> subplot(2,2,1);plot(x,sin(x)); >> subplot(2,2,2);plot(x,cos(x)); >> subplot(2,2,3);plot(x,sinh(x)); >> subplot(2,2,4);plot(x,cosh(x));

matlab实验报告模板

matlab实验报告模板

《高等数学》实验报告(二)实验项目名称:多元函数积分学分组第八组组员姓名学号专业班级实验软件Matlab2010b完成日期实验成绩一、实验目的:加强对Matlab 软件的基本操作,会利用符号计算中int 积分嵌套命令求二重积分、三重积分。

会合理运用int 嵌套命令求解第一类、第二类曲线积分及第一类、第二类曲面积分以及Green 公式及Gauss 公式解题。

结合已经学习的内容,学会分析上述有关内容的综合问题并利用软件给出正确的解答。

二、 实验内容、步骤与结果:8. 计算二重积分 arctan D y d xσ⎰⎰ 其中D 是由圆224x y += 、 及直线y=0, y=x 所围成的在第一象限内的闭区域。

>> syms x y rho theta>> i=int(int(atan(tan(theta))*rho,rho,1,2),theta,0,(0.25)*pi)i =(3*pi^2)/6418. 计算三重积分 e x y z dxdydz ++Ω⎰⎰⎰,其中, 是平面x+y+z=1与三个坐标面围成的立体。

>> syms x y z;>> i=int(int(int(exp(x+y+z),z,0,1),y,0,1-x),x,0,1)i =exp(1) - 128. 计算曲线积分 2()()L x y dx x y dy ++-⎰,其中,L 沿直线从(1,0) 到(0,1), 再沿直线从(0,1)到(-1,0)。

过程syms x y;p=x^2+y;q=x-y;fun=(x^2+y)*diff(p,x)+(x-y)*diff(q,y);I=int(fun,x,1,0);I=int(fun,y,0,1)I =1/2+2*x^3I=int(fun,x,0,-1);I=int(fun,y,1,0)I =-1/2-2*x^3结果由(1,0)到(0,1)I =1/2+2*x^3由(0,1)到(-1,0)I =-1/2-2*x^338求半径为a的球面面积。

程序设计实验报告(matlab)

程序设计实验报告(matlab)

程序设计实验报告(matlab)实验一: 程序设计基础实验目的:初步掌握机器人编程语言Matlab。

实验内容:运用Matlab进行简单的程序设计。

实验方法:基于Matlab环境下的简单程序设计。

实验结果:成功掌握简单的程序设计和Matlab基本编程语法。

实验二:多项式拟合与插值实验目的:学习多项式拟合和插值的方法,并能进行相关计算。

实验内容:在Matlab环境下进行多项式拟合和插值的计算。

实验方法:结合Matlab的插值工具箱,进行相关的计算。

实验结果:深入理解多项式拟合和插值的实现原理,成功掌握Matlab的插值工具箱。

实验三:最小二乘法实验目的:了解最小二乘法的基本原理和算法,并能够通过Matlab进行计算。

实验内容:利用Matlab进行最小二乘法计算。

实验方法:基于Matlab的线性代数计算库,进行最小二乘法的计算。

实验结果:成功掌握最小二乘法的计算方法,并了解其在实际应用中的作用。

实验六:常微分方程实验目的:了解ODE的基本概念和解法,并通过Matlab进行计算。

实验内容:利用Matlab求解ODE的一阶微分方程组、变系数ODE、高阶ODE等问题。

实验方法:基于Matlab的ODE工具箱,进行ODE求解。

实验结果:深入理解ODE的基本概念和解法,掌握多种ODE求解方法,熟练掌握Matlab的ODE求解工具箱的使用方法。

总结在Matlab环境下进行程序设计实验,使我对Matlab有了更深刻的认识和了解,也使我对计算机科学在实践中的应用有了更加深入的了解。

通过这些实验的学习,我能够灵活应用Matlab进行各种计算和数值分析,同时也能够深入理解相关的数学原理和算法。

这些知识和技能对我未来的学习和工作都将有着重要的帮助。

matlab实验报告

matlab实验报告

实验一1.设x=-74°,y=-27°,求22的值。

√tan⁡|x+y|+π2.当a取-3.0,-2.9,-2.8,…,2.8,2.9,3.0时,求e−0.3a sin(a+0.3)在各点的函数值。

3. 设x=24−0.455,求12In(x+√1+x ²)的值,并分析结果矩阵中各元素的含义。

4. 已知A=354234−457879015,B=1−2672874930求下面的表达式的值。

(1)A*B和A.*B。

(2)A^3和A.^3.。

(3)A/B和A\B。

(4)[A,B]和[A([1,3],:);B^2]。

实验二一、实验步骤:1)新建脚本2)在编辑器中输入相应程序3)在命令窗口执行文件,得到结果1. 根据π²6=11²+12²+13²+…+1n ²,求π的近似值。

当n 分别取100、1000、10000时,结果是多少?要求:分别用循环结构和向量运算(使用sum 函数)来实现。

1)循环结构一、实验步骤二、1)新建脚本2)在编辑器中输入相应程序3)保存文件,将文件命名为PI.m4)在命令窗口输入PI执行文件,得到结果三、实验代码四、实验结果2.根据y=1+13+15+⋯+12n−1,求(1)y<3时的最大n值(2)与(1)的n值对应的y值一、实验步骤1)打开matlab,新建脚本2)在脚本文件中输入实验代码3)保存文件,存名字为value.m4)在命令窗口中输入value,得到实验结果二、实验代码三、实验结果。

MATLAB实验报告3(1)

MATLAB实验报告3(1)

四、实验内容与步骤:1.绘制下列曲线.(1) y=x-(x^3)/6程序输入如下:fplot('x-(x^3)/6',[-5,5],'r.');程序输出:(2) x^2+2*y^2=64程序输入如下:ezplot(' x^2+2*y^2-64',[-8,8]) 程序输出:2.设y=1/(1+exp(-t)) –pi<=t<=pi在同一图形窗口采用子图的形式绘制条形图阶梯图杆图和对数坐标图等不同图形,并对不同图形加标注说明.程序输入如下:t=-pi:pi/10:pi;y=1./(1+exp(-t));subplot(2,2,1);bar(t,y,'r');title('条形图');axis([-4,4,0,1]);subplot(2,2,2);stairs(t,y,'b');title('阶梯图');axis([-4,4,0,1]);subplot(2,2,3);stem(t,y,'g');title('杆图');axis([-4,4,0,1]);subplot(2,2,4);semilogx(t,y,'k');title('对数坐标图');axis([-4,4,0,1]);程序输出:3.绘制下列极坐标图.(1) y=5*cos(x)+4(2) y=(5*sin(x)*sin(x))/cos(x) (1)程序输入:x=0:pi/50:2*pi;y=5*cos(x)+4;polar(x,y,'-*');程序输出:(2)程序输入:x=-pi/3:pi/50:pi/3;y=(5.*sin(x).*sin(x))./cos(x);polar(x,y,'-*');程序输出:4.绘制下列三维图形(1)x=exp(-t/20).*cos(t)y=exp(-t/20).*sin(t)z=t0<=t<=2*pi(2)z=5abs(x)<=5abs(y)<=5要求应用插值着色处理(1)程序输入:t=0:pi/10:2*pi;x=exp(-t/20).*cos(t);y=exp(-t/20).*sin(t);z=t;plot3(x,y,z);title('三维图形4-1');xlabel('x');ylabel('y');zlabel('z'); grid on;程序输出:(2)程序输入:[x,y]=meshgrid(-5:0.5:5); z=0*(x-y)+5;surf(x,y,z);shading interp;title('三维图形4-2');程序输出:五、实验总结:2.绘制下列曲线,(1) y=exp(-x*x/2)/(2*pi)程序输入:fplot('exp(-x*x/2)/(2*pi)',[0,5],'r.')程序输出:(2) x=t*sin(t)y=t*cos(t)程序输入:t=0:0.1:2*pi;x=t.*sin(t);y=t.*cos(t);plot(x,y);程序输出:3.在同一坐标轴中绘制下列两条曲线并标注两曲线交叉点(1) y=2*x-0.5(2) x=sin(3*t).*cos(t)y= sin(3*t).*sin(t)0<=t<=pi程序输入:t=0:pi/100:pi;x=sin(3*t).*cos(t);y2=sin(3*t).*sin(t);y1=2*x-0.5;plot(x,y1,'m',x,y2,'g');hold onk=find(abs(y2-y1)<1e-4);x1=x(k);y3=2*x1-0.5;plot(x1,y3,'bp');程序输出:4.分别用plot和fplot函数绘制函数y=sin(1/x)的曲线,分析两曲线的差别程序输入:x=-1:pi/100:1;y=sin(1./x);subplot(2,1,1);plot(x,y,'g');subplot(2,1,2);fplot('sin(1./x)',[-1,1],'m');程序输出:两曲线的差别plot函数在取数据点时一般都是等间隔采样,fplot函数可自适应地对函数进行采样,能更好的反应函数的变化规律6.绘制曲面图形(1)x=3*u*sin(v)y=2*u*cos(v)z=4*u*u程序输入:[u,v]=meshgrid(0:pi/100:2*pi);x=3*u.*sin(v);y=2*u.*cos(v);z=4*u.*u;mesh(x,y,z);程序输出:严重觉得对细节方面很重要,,差一个点就能导致整个程序的不能运行。

MATLAB实验报告(1-4)

MATLAB实验报告(1-4)

信号与系统MATLAB第一次实验报告一、实验目的1.熟悉MATLAB软件并会简单的使用运算和简单二维图的绘制。

2.学会运用MATLAB表示常用连续时间信号的方法3.观察并熟悉一些信号的波形和特性。

4.学会运用MATLAB进行连续信号时移、反折和尺度变换。

5.学会运用MATLAB进行连续时间微分、积分运算。

6.学会运用MATLAB进行连续信号相加、相乘运算。

7.学会运用MATLAB进行连续信号的奇偶分解。

二、实验任务将实验书中的例题和解析看懂,并在MATLAB软件中练习例题,最终将作业完成。

三、实验内容1.MATLAB软件基本运算入门。

1). MATLAB软件的数值计算:算数运算向量运算:1.向量元素要用”[ ]”括起来,元素之间可用空格、逗号分隔生成行向量,用分号分隔生成列向量。

2.x=x0:step:xn.其中x0位初始值,step表示步长或者增量,xn为结束值。

矩阵运算:1.矩阵”[ ]”括起来;矩阵每一行的各个元素必须用”,”或者空格分开;矩阵的不同行之间必须用分号”;”或者ENTER分开。

2.矩阵的加法或者减法运算是将矩阵的对应元素分别进行加法或者减法的运算。

3.常用的点运算包括”.*”、”./”、”.\”、”.^”等等。

举例:计算一个函数并绘制出在对应区间上对应的值。

2).MATLAB软件的符号运算:定义符号变量的语句格式为”syms 变量名”2.MATLAB软件简单二维图形绘制1).函数y=f(x)关于变量x的曲线绘制用语:>>plot(x,y)2).输出多个图像表顺序:例如m和n表示在一个窗口中显示m行n列个图像,p表示第p个区域,表达为subplot(mnp)或者subplot(m,n,p)3).表示输出表格横轴纵轴表达范围:axis([xmax,xmin,ymax,ymin])4).标上横轴纵轴的字母:xlabel(‘x’),ylabel(‘y’)5).命名图像就在subplot写在同一行或者在下一个subplot前:title(‘……’)6).输出:grid on举例1:举例2:3.matlab程序流程控制1).for循环:for循环变量=初值:增量:终值循环体End2).while循环结构:while 逻辑表达式循环体End3).If分支:(单分支表达式)if 逻辑表达式程序模块End(多分支结构的语法格式)if 逻辑表达式1程序模块1Else if 逻辑表达式2程序模块2…else 程序模块nEnd4).switch分支结构Switch 表达式Case 常量1程序模块1Case 常量2程序模块2……Otherwise 程序模块nEnd4.典型信号的MATLAB表示1).实指数信号:y=k*exp(a*t)举例:2).正弦信号:y=k*sin(w*t+phi)3).复指数信号:举例:4).抽样信号5).矩形脉冲信号:y=square(t,DUTY) (width默认为1)6).三角波脉冲信号:y=tripuls(t,width,skew)(skew的取值在-1~+1之间,若skew取值为0则对称)周期三角波信号或锯齿波:Y=sawtooth(t,width)5.单位阶跃信号的MATLAB表示6.信号的时移、反折和尺度变换:Xl=fliplr(x)实现信号的反折7.连续时间信号的微分和积分运算1).连续时间信号的微分运算:语句格式:d iff(function,’variable’,n)Function:需要进行求导运算的函数,variable:求导运算的独立变量,n:求导阶数2).连续时间信号的积分运算:语句格式:int(function,’variable’,a,b)Function:被积函数variable:积分变量a:积分下限b:积分上限(a&b默认是不定积分)8.信号的相加与相乘运算9.信号的奇偶分解四、小结这一次实验让我能够教熟悉的使用这个软件,并且能够输入简单的语句并输出相应的结果和波形图,也在一定程度上巩固了c语言的一些语法。

MATLAB实验报告模板

MATLAB实验报告模板

MATLAB实验报告模板实验题目:使用MATLAB进行数字信号处理实验目的:1. 学习MATLAB的基本操作和数字信号处理中常用的函数。

2. 掌握数字信号的离散化、采样、量化等处理方法。

3. 实现数字滤波器的设计和应用。

实验内容:1. 生成并绘制一个正弦波信号,包括频率、幅度和相位等参数。

2. 对信号进行采样,并绘制采样后的离散信号图像。

3. 对采样后的信号进行量化,并绘制量化后的信号图像。

4. 设计一个数字滤波器,并将滤波前后的信号图像进行对比。

实验步骤:1. 生成一个正弦波信号:```matlab% 信号频率为3Hz,幅度为2,相位为0t = 0:0.01:1;f = 3;A = 2;theta = 0;x = A * sin(2*pi*f*t + theta);```2. 绘制信号图像:```matlabplot(t, x);xlabel('时间');ylabel('幅度');title('正弦波信号');```3. 进行信号的采样:```matlab% 采样频率为20Hzfs = 20;Ts = 1/fs;n = 0:Ts:1;xs = A * sin(2*pi*f*n + theta);```4. 绘制采样信号图像:```matlabstem(n, xs);xlabel('时间');ylabel('幅度');title('采样信号');```5. 进行信号的量化:```matlab% 将信号量化为8位bits = 8;delta = (2 * A) / (2^bits);xq = floor(xs / delta + 0.5) * delta;```6. 绘制量化信号图像:```matlabstem(n, xq);xlabel('时间');ylabel('幅度');title('量化信号');```7. 设计数字滤波器:```matlab% 采用FIR滤波器,截止频率为4Hz fcut = 4;n = 100; % 滤波器阶数b = fir1(n, 2*fcut/fs);y = filter(b, 1, xq);```8. 绘制滤波前后的信号图像:```matlabfigure;subplot(2,1,1);stem(n, xq);xlabel('时间');ylabel('幅度');title('量化信号');subplot(2,1,2);stem(n, y);xlabel('时间');ylabel('幅度');title('滤波信号');```实验结果和分析:根据以上实验步骤,可以得到正弦波信号、采样信号、量化信号和滤波信号等图像。

MATLAB实验报告(8个实验)

MATLAB实验报告(8个实验)

四川师范大学MATLAB语言实验报告1系级班年月日实验名称:Intro, Expressions, Commands姓名学号指导教师成绩1ObjectiveThe objective of this lab is to familiarize you with the MATLAB program development environment and to develop your first programs in this environment.2Using MATLAB2.1Starting MATLABLogon to your computer and start MATLAB by double-clicking on the icon on the desktop or by using the Start Programs menu. MATLAB Desktop window will appear on the screen.The desktop consists of several sub-windows. The most important ones are:●Command Window (on the right side of the Desktop) is used to do calculations,enter variables and run built-in and your own functions.●Workspace (on the upper left side) consists of the set of variables (arrays) createdduring the current MATLAB session and stored in memory.●Command History (on the lower left side) logs commands entered in theCommand Window. You can use this window to view previously run statements, and copy and execute selected statements.You can switch between the Launch Pad window and the Workspace window using the menu tabs under the sub-window on the upper left side. Similarly, you can switch between the Command History and Current Directory windows using the menu tabs under the sub-window on the lower left side.2.2Executing CommandsYou can type MATLAB commands at the command prompt “>>” on the Command Window.For example, you can type the formula cos(π/6)2sin(3π/8) as>>(cos(pi/6) ^ 2) * (sin(3 * pi/8))Try this command. After you finish typing, press enter. The command will be interpreted and the result will be displayed on the Command Window.Try the following by observing how the Workspace window changes:>> a = 2; (M ake note of the usage of “;”)>> b = 3;>> c = a ^ 4 ∗ b ∗ 5 + pi ^3You can see the variables a, b and c with their types and sizes on the Workspacewindow, and can see the commands on the Command History window.Spend a few minutes to practice defining array variables (i.e. vectors and matrices)usingthe square bracket (“[ ]”) and colon (“:”) operators, and zeros() and ones() functions.>> ar =[ 1 2 3 4 5 ];>> br =[ 1 2 3 ;4 5 6 ];>> cr = [1 : 3 : 15];• Set dr to first 3 elements of ar.dr=ar(1:3);• Set er to second row of br.er=br(2,:);• Set ar to [dr er]. Find the number of elements of ar.ar=[dr er]; length(ar)2.3 Getting HelpThere are several ways to get help on commands and functions in MATLAB. First ofall you can use the Help menu. You can also use the “?” button. Try to findinformation on the plot function from the help index. Also try to get information onthe same function using the help command (i.e. type help plot). Finally, experimentwith the lookfor command. This command looks for other commands related to agiven keyword.2.4 Some Useful CommandsTry the following commands and observe their results:Which : Version and location infoClear : Clears the workspaceClc : Clears the command windowwho, whos : Lists content of the workspace3 ExercisesPlease solve the following problems in MATLAB. Do not forget to keep a diary ofyour commands and their outputs.(1) Define the variables x y and z as 7.6, 5.5 and 8.1, respective ly, and evaluate:578.422.52⎪⎭⎫ ⎝⎛-x y xz(2) Compute the slope of the line that passes through the points (1,-2) and(5,8).(3) Quiz 1.1: 5(4)1.6 Exercises: 1.1, 1.4(5)2.15 Exercises: 2.6, 2.9, 2.114Quitting MATLABTyping quit on the command window will close the program. Do not forget to send your diary file and M-file to your TA.Do not forget to delete your files from the hard disk of the PC you used in the lab at the end of the lab session.四川师范大学MATLAB语言实验报告2系级班年月日实验名称:Programming, Relational and Logical Expressions姓名学号指导教师成绩1ObjectiveThe objective of this lab is to familiarize you with the MATLAB script files (M-files), subarrays, relational and logical operators.2Script FilesScript files are collections of MATLAB statements that are stored in a file. Instead of typing commands directly in the Command Window, a series of commands may be placed into a file and the entire file may be executed by typing its name in the Command Window. Such files are called script files that are also known as M-files because they have an extension of .m. When a script file is executed, the result is the same as it would be if all of the commands had been typed directly into the Command Window. All commands and script files executed in the Command Window share a common workspace, so they can all share variables in the workspace. Note that if two script files are executed successively, the second script file can use the variables created by the first script file. In this way, script files can communicate with other script files through the data left behind in the workspace. An Edit Window is used to create new M-files or to modify existing ones. The Edit Window is a programming text editor, with the features of MATLAB language highlighted in different colors. You can create a new M-file with the File/New/M-file selection and you can open an existing M-file with the File/Open selection from the desktop menu of MATLAB. (1)Create a new working directory under the current directory and change the currentdirectory to ‘TA’s suggest’.3SubarraysIt is possible to select and use subsets of MATLAB arrays. To select a subset of an array, just include a list of the elements to be selected in the parentheses after the array name. MATLAB has a special function named end that is used to create arraysubscripts. The end function always returns the highest value taken on by a givensubscript. It is also possible to use subarrays on the left-hand side of an assignmentstatement to change only some of the values in an array. If values are assigned to asubarray, only those values are changed but if values are assigned to an array, theentire contents of the array are replaced by the new values.(1) Define the following 5 x 5 array arr1 in MATLAB.⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎣⎡----=2274235421209518171651413215111012844563311arr(2) Write a MATLAB statement to select a subset of arr1 and return the subarraycontaining the values as shown.⎥⎦⎤⎢⎣⎡=22745456311arrarr11=arr1([1,5],[2 4 5]);(3) Write two MATLAB statements to select the last row and last column of arr1,separately.arr12=arr1(5,:);或arr12=arr1(end,:); arr13=arr1(:,end);或 arr13=arr1(:,5);(4) Write MATLAB statements to obtain the following array from arr1.⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎣⎡-=2257462335432112arrarr2=arr1([1 5],:)';4 Relational and Logical OperatorsRelational and logical operators are the two types of operators that produce true/falseresults in MATLAB programs. MATLAB interprets a zero value as false and anynonzero value as true. Relational operators ( ==, =,>,>=,<,<=) are operators with twooperands that produce either a true (1) or a false (0) result, depending on the values ofthe operands. Relational operators can be used to compare a scalar value with an array.They can also be used to compare two arrays or two strings only if they have the samesize. Be careful not to confuse the equivalence relational operator ( == ) with theassignment operator ( = ). Logic operators ( &, |, xor, ~ ) are operators with one ortwo operands that yield a logical result such as 0 or 1. There are three binary logicoperators: AND (& ), OR ( |), and exclusive OR ( xor ); and one unary operator: NOT( ~ ). In the hierarchy of operations, logic operators are evaluated after allarithmetic and relational operators have been evaluated. The operator is evaluatedbefore other logic operators.(1) Define the following 4 x 5 array arr4 in MATLAB.⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡------=212343212343212543214arr(2) Write an expression using arr4 and a relational operator to produce the followingresult.⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡=110001110011110111115arrarr5=arr4>0;(3) Write an expression using arr4 and a relational operator to produce the followingresult.⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡=010000010000010000016arrarr6=arr4==1;(4) Write a MATLAB program which will generate an (n-1)x(n-1) matrix from agiven nxn matrix which will be equal to given matrix with first row and firstcolumn deleted.arr44=rand(5); arr444=arr35(2:end,2:end);(5) Generalize your program above so that the program should ask the row andcolumn numbers to be deleted and then generate new (n-1)x(n-1) matrix.n=input('input n:');matrixn=rand(n)delrow=input('input row numbers to be deleted:');delcolumn=input('input column numbers to be deleted:');matrixn_1=matrixn([1:delrow-1 delrow+1:end], [1:delcolumn-1 delcolumn+1:end])(6) Quiz 3.1 (P88)5 Quitting MATLABTyping quit on the command window will close the program. Do not forget to sendyour diary file and M-file to your TA.Do not forget to delete your files from the hard disk of the PC you used in the lab atthe end of the lab session.四川师范大学MATLAB 语言实验报告3系 级 班 年 月 日实验名称:Branches and Loops, Logical Arrays.姓名 学号 指导教师 成绩 1 ObjectiveThe objective of this lab is to familiarize you with the MATLAB Branches and Loops,Logical Arrays.2 ExercisesDo not forget to add sufficient documentation and proper indentation to all programsyou write.(1) Write a program that calculates follow equation with for and while loop, and writea program without loop.63263022212+++==∑=Λi i K% for loopk1=0;for ii=1:64k1=k1+2^(ii-1);end% while loopk2=0;n=0;while n>=0&n<64k2=k2+2^n;n=n+1;end% without loopa=0:63;b=2.^a;K3=sum(b);(2) Write a program that accepts a vector of integers as input and counts the numberof integers that are multiples of 3 in that vector. You can assume that the inputcontains only integer values. An example run of your program can be as follows:Enter a vector of integers: [ 1 3 2 8 0 5 6 ]The number of multiples of 3 is 2(3) The root mean square is a way for calculating a mean for a set of numbers. The rmsaverage of a series of numbers is given as:∑==N i i x N rmsaverage 121Write a program that will accept an arbitrary number of input values and calculatethe rmsaverage of the numbers. The program should ask the user for the numberof values to be entered. Test your program with 4 and 10 set of numbers.% The root mean square is a way for calculating a mean for a set of numbers% Initializesum_x2=0;% Get the number of points to input.n=input('Enter number of points:');% Loop to read input valuesfor ii=1:n% Read in next valuex=input('Enter value:');% Calculate square sumssum_x2=sum_x2+x^2;end% Now calculate root mean squareroot_ms=sqrt(sum_x2/n);% Tell userfprintf('The number of data points is: %d\n',n);fprintf('The root mean square of this data set is: %f\n',root_ms);(4) 3.8 exercises:3.5(5) 4.7Exercises: 4.1 4.23 Quitting MATLABTyping quit on the command window will close the program. Do not forget to sendyour M-file to your TA.Do not forget to delete your files from the hard disk of the PC you used in the lab at the end of the lab session.四川师范大学MATLAB语言实验报告4系级班年月日实验名称:MATLAB/SIMULINK package姓名学号指导教师成绩1Objective●To learn how to use MATLAB/SIMULINK package●To learn how to estimate performance parameters from time-domain data2SIMULINK BasicBasic steps(1)Click on the MATLAB button to start MATLAB.(2)Once MATLAB has started up, type simulink (SMALL LETTERS!) at theMATLAB prompt (>>) followed by a carriage return (press the return key). A SIMULINK window should appear shortly, with the following icons: Sources, Sinks, Discrete, Linear, Connections, Extras.(3)Next, go to the File menu in SIMULINK window and choose New in order tobegin building the block diagram representation of the system of interest.(4)Open one or more of the block libraries and drag the chosen blocks into the active.(5)After the blocks are placed, draw lines to connect their input and output ports bymoving the mouse over a port and drag using the left button. To make a line witha right angle in it, release the button where you want the corner, then click on theend of the line and drag to create next segment. To add a second line that runs off of an existing line click the right mouse on an existing line and drag it.(6)Save the system by selecting Save from the File menu.(7)Open the blocks by double-clicking and change some of their internal parameters.(8)Adjust some simulation parameters by selecting Parameters from the Simulationmenu. The most common parameter to change is Stop Time that defines the length of time the simulation will run.(9)Run the simulation by selecting Start from the Simulation menu. You can stop asimulation before completing by selecting Stop from the Simulation menu. (10)View the behavior of the system by attaching Scope blocks to the variables ofinterest, or by using To Workspace blocks to send data to the MATLAB workspace where you can plot the results using standard MATLAB commands.3Exercises(1)Your TA has shown you how to observe and print signals from the scope. Try thisout by printing out the input signal, which should be a -1V to 1V square wave with frequency 0.1 Hz. Note the peak-to-peak voltage difference of this signal.Note to write key blocks parameters.(2) Write a Simulink model to calculate the following differential equation,0)1(222=+--x dt dx x dt x d μInitialized 1)0(=x ,0)0(=dt dx 。

Matlab数学实验报告模板

Matlab数学实验报告模板

数学实验报告
日期:20年月日
班级姓名学号
实验(一)猜数字游戏
任务描述:
首先由计算机随机产生一个[1,100]之间的一个整数,然后由用户猜测所产生的这个数。

根据用户猜测的情况给出不同的提示,如果猜测的数大于产生的数,则显示“High”,小于则显示“Low”,等于则显示“You won!”,同时退出游戏。

用户最多有7次机会。

实验原理与数学模型:
实验所用软件及版本:
主要内容(要点):
1
实验过程记录(含基本步骤、主要程序清单及异常情况记录等):实验结果报告与实验总结:
思考与深入:
教师评语:
实验(二)
问题背景描述:
实验目的:
实验原理与数学模型:
实验所用软件及版本:
主要内容(要点):
实验过程记录(含基本步骤、主要程序清单及异常情况记录等):
实验结果报告与实验总结:思考与深入:
教师评语:。

MATLAB实验报告(8个实验)

MATLAB实验报告(8个实验)

MATLAB实验报告(8个实验)四川师范大学MATLAB语言实验报告1系级班年月日实验名称:Intro, Expressions, Commands姓名学号指导教师成绩1ObjectiveThe objective of this lab is to familiarize you with the MATLAB program development environment and to develop your first programs in this environment.2Using MATLAB2.1Starting MATLABLogon to your computer and start MATLAB by double-clicking on the icon on the desktop or by using the Start Programs menu. MATLAB Desktop window will appear on the screen.The desktop consists of several sub-windows. The most important ones are:●Command Window (on the right side of the Desktop) is used to do calculations,enter variables and run built-in and your own functions.●Workspace (on the upper left side) consists of the set of variables (arrays) createdduring the current MATLAB session and stored in memory.●Command History (on the lower left side) logs commands entered in theCommand Window. You can use this window to view previously run statements, and copy and execute selected statements.You can switch between the Launch Pad window and the Workspace window using the menu tabs under the sub-windowon the upper left side. Similarly, you can switch between the Command History and Current Directory windows using the menu tabs under the sub-window on the lower left side.2.2Executing CommandsYou can type MATLAB commands at the command prompt “>>” on the Command Window.For example, you can type the formula cos(π/6)2sin(3π/8) as >>(cos(pi/6) ^ 2) * (sin(3 * pi/8))Try this command. After you finish typing, press enter. The command will be interpreted and the result will be displayed on the Command Window.Try the following by observing how the Workspace window changes:>> a = 2; (M ake note of the us age of “;”)>> b = 3;>> c = a ^ 4 ? b ? 5 + pi ^3You can see the variables a, b and c with their types and sizes on the Workspacewindow, and can see the commands on the Command History window.Spend a few minutes to practice defining array variables (i.e. vectors and matrices)usingthe square bracket (“[ ]”) and colon (“:”) operators, and zeros() and ones() functions.>> ar =[ 1 2 3 4 5 ];>> br =[ 1 2 3 ;4 5 6 ];>> cr = [1 : 3 : 15];Set dr to ?rst 3 elements of ar.dr=ar(1:3);Set er to second row of br.er=br(2,:);Set ar to [dr er]. Find the number of elements of ar.ar=[dr er]; length(ar)2.3 Getting HelpThere are several ways to get help on commands and functions in MATLAB. First ofall you can use the Help menu. You can also use the “?” button. Try to findinformation on the plot function from the help index. Also try to get information onthe same function using the help command (i.e. type help plot). Finally, experimentwith the lookfor command. This command looks for other commands related to agiven keyword.2.4 Some Useful CommandsTry the following commands and observe their results:Which : Version and location infoClear : Clears the workspaceClc : Clears the command windowwho, whos : Lists content of the workspace3 ExercisesPlease solve the following problems in MATLAB. Do not forget to keep a diary ofyour commands and their outputs.(1) De?ne the variables x y and z as 7.6, 5.5 and 8.1, respective ly, and evaluate:578.422.52??? ??-x y xz(2) Compute the slope of the line that passes through thepoints (1,-2) and(5,8).(3) Quiz 1.1: 5(4)1.6 Exercises: 1.1, 1.4(5)2.15 Exercises: 2.6, 2.9, 2.114Quitting MATLABTyping quit on the command window will close the program. Do not forget to send your diary file and M-file to your TA.Do not forget to delete your ?les from the hard disk of the PC you used in the lab at the end of the lab session.四川师范大学MATLAB语言实验报告2系级班年月日实验名称:Programming, Relational and Logical Expressions 姓名学号指导教师成绩1ObjectiveThe objective of this lab is to familiarize you with the MATLAB script files (M-files), subarrays, relational and logical operators.2Script FilesScript files are collections of MATLAB statements that are stored in a file. Instead of typing commands directly in the Command Window, a series of commands may be placed into a file and the entire file may be executed by typing its name in the Command Window. Such files are called script files that are also known as M-files because they have an extension of .m. When a script file is executed, the result is the same as it would be if all of the commands had been typed directly into the Command Window. All commands and script files executed in the Command Window share a common workspace, so they can all share variables in the workspace. Note that if two script files are executed successively, the second script file can use the variables created by the first script file. In this way, script files can communicate with other script files through the data left behindin the workspace. An Edit Window is used to create new M-files or to modify existing ones. The Edit Window is a programming text editor, with the features of MATLAB language highlighted in different colors. You can create a new M-file with the File/New/M-file selection and you can open an existing M-file with the File/Open selection from the desktop menu of MATLAB.(1)Create a new working directory under the current directory and change the currentdirectory to …TA?s suggest?.3SubarraysIt is possible to select and use subsets of MATLAB arrays. To select a subset of an array, just include a list of the elements to be selected in the parentheses after the array name. MATLAB has a special function named end that is used to create arraysubscripts. The end function always returns the highest value taken on by a givensubscript. It is also possible to use subarrays on the left-hand side of an assignmentstatement to change only some of the values in an array. If values are assigned to asubarray, only those values are changed but if values are assigned to an array, theentire contents of the array are replaced by the new values.(1) Define the following 5 x 5 array arr1 in MATLAB.----=2274235421209518171651413215111012844563311arr(2) Write a MATLAB statement to select a subset of arr1 and return the subarraycontaining the values as shown.=22745456311arrarr11=arr1([1,5],[2 4 5]);(3) Write two MATLAB statements to select the last row and last column of arr1,separately.arr12=arr1(5,:);或arr12=arr1(end,:); arr13=arr1(:,end);或arr13=arr1(:,5);(4) Write MATLAB statements to obtain the following array from arr1.-=2257462335432112arrarr2=arr1([1 5],:)';4 Relational and Logical OperatorsRelational and logical operators are the two types of operators that produce true/falseresults in MATLAB programs. MATLAB interprets a zero value as false and anynonzero value as true. Relational operators ( ==, =,>,>=,<,<=) are operators with twooperands that produce either a true (1) or a false (0) result, depending on the values ofthe operands. Relational operators can be used to compare a scalar value with an array.They can also be used to compare two arrays or two strings only if they have the samesize. Be careful not to confuse the equivalence relational operator ( == ) with theassignment operator ( = ). Logic operators ( &, |, xor, ~ ) are operators with one ortwo operands that yield a logical result such as 0 or 1. There are three binary logicoperators: AND (& ), OR ( |), and exclusive OR ( xor ); and oneunary operator: NOT( ~). In the hierarchy of operations, logic operators are evaluated after allarithmetic and relational operators have been evaluated. The operator is evaluatedbefore other logic operators.(1) Define the following 4 x 5 array arr4 in MATLAB.------=212343212343212543214arr(2) Write an expression using arr4 and a relational operator to produce the followingresult.=110001110011110111115arrarr5=arr4>0;(3) Write an expression using arr4 and a relational operator to produce the followingresult.=010000010000010000016arrarr6=arr4==1;(4) Write a MATLAB program which will generate an (n-1)x(n-1) matrix from agiven nxn matrix which will be equal to given matrix with first row and firstcolumn deleted.arr44=rand(5); arr444=arr35(2:end,2:end);(5) Generalize your program above so that the program should ask the row andcolumn numbers to be deleted and then generate new (n-1)x(n-1) matrix.n=input('input n:');matrixn=rand(n)delrow=input('input row numbers to be deleted:');delcolumn=input('input column numbers to be deleted:');matrixn_1=matrixn([1:delrow-1 delrow+1:end], [1:delcolumn-1 delcolumn+1:end])(6) Quiz 3.1 (P88)5 Quitting MATLABTyping quit on the command window will close the program. Do not forget to sendyour diary file and M-file to your TA.Do not forget to delete your files from the hard disk of the PC you used in the lab atthe end of the lab session.四川师范大学MATLAB 语言实验报告3系级班年月日实验名称:Branches and Loops, Logical Arrays.姓名学号指导教师成绩 1 ObjectiveThe objective of this lab is to familiarize you with the MATLAB Branches and Loops,Logical Arrays.2 ExercisesDo not forget to add sufficient documentation and proper indentation to all programsyou write.(1) Write a program that calculates follow equation with for and while loop, and writea program without loop.63263022212+++==∑= i i K% for loopk1=0;for ii=1:64k1=k1+2^(ii-1);end% while loopk2=0;n=0;while n>=0&n<64k2=k2+2^n;n=n+1;end% without loopa=0:63;b=2.^a;K3=sum(b);(2) Write a program that accepts a vector of integers as input and counts the numberof integers that are multiples of 3 in that vector. You can assume that the inputcontains only integer values. An example run of your program can be as follows:Enter a vector of integers: [ 1 3 2 8 0 5 6 ]The number of multiples of 3 is 2(3) The root mean square is a way for calculating a mean fora set of numbers. The rmsaverage of a series of numbers is given as:∑==N i i xN rmsaverage 121Write a program that will accept an arbitrary number of input values and calculatethe rmsaverage of the numbers. The program should ask the user for the numberof values to be entered. Test your program with 4 and 10 set of numbers.% The root mean square is a way for calculating a mean for a set of numbers% Initializesum_x2=0;% Get the number of points to input.n=input('Enter number of points:');% Loop to read input valuesfor ii=1:n% Read in next valuex=input('Enter value:');% Calculate square sumssum_x2=sum_x2+x^2;end% Now calculate root mean squareroot_ms=sqrt(sum_x2/n);% Tell userfprintf('The number of data points is: %d\n',n);fprintf('The root mean square of this data set is: %f\n',root_ms);(4) 3.8 exercises:3.5(5) 4.7Exercises: 4.1 4.23 Quitting MATLABTyping quit on the command window will close the program. Do not forget to sendyour M-file to your TA.Do not forget to delete your files from the hard disk of the PC you used in the lab at the end of the lab session.四川师范大学MATLAB语言实验报告4系级班年月日实验名称:MATLAB/SIMULINK package姓名学号指导教师成绩1Objective●To learn how to use MATLAB/SIMULINK package●To learn how to estimate performance parameters from time-domain data2SIMULINK BasicBasic steps(1)Click on the MATLAB button to start MATLAB.(2)Once MATLAB has started up, type simulink (SMALL LETTERS!) at theMATLAB prompt (>>) followed by a carriage return (press the return key). A SIMULINK window should appear shortly, with the following icons: Sources, Sinks, Discrete, Linear, Connections, Extras.(3)Next, go to the File menu in SIMULINK window and choose New in order tobegin building the block diagram representation of the system of interest.(4)Open one or more of the block libraries and drag the chosen blocks into the active.(5)After the blocks are placed, draw lines to connect their input and output ports bymoving the mouse over a port and drag using the left button. To make a line witha right angle in it, release the button where you want thecorner, then click on theend of the line and drag to create next segment. To add a second line that runs off of an existing line click the right mouse on an existing line and drag it.(6)Save the system by selecting Save from the File menu.(7)Open the blocks by double-clicking and change some of their internal parameters.(8)Adjust some simulation parameters by selecting Parameters from the Simulationmenu. The most common parameter to change is Stop Time that defines the length of time the simulation will run.(9)Run the simulation by selecting Start from the Simulation menu. You can stop asimulation before completing by selecting Stop from the Simulation menu. (10)View the behavior of the system by attaching Scope blocks to the variables ofinterest, or by using To Workspace blocks to send data to the MATLAB workspace where you can plot the results using standard MATLAB commands.3Exercises(1)Your TA has shown you how to observe and print signals from the scope. Try thisout by printing out the input signal, which should be a -1V to 1V square wave with frequency 0.1 Hz. Note the peak-to-peak voltage difference of this signal.Note to write key blocks parameters.(2) Write a Simulink model to calculate the following differential equation,0)1(222=+--x dt dx x dt x d μInitialized 1)0(=x ,0)0(=dt dx 。

matlab 实验报告

matlab 实验报告

matlab 实验报告Matlab 实验报告引言:Matlab(Matrix Laboratory)是一种强大的科学计算软件,它为科学家、工程师和研究人员提供了一个强大的计算环境。

本实验报告旨在介绍我对Matlab的实验结果和使用体验,以及对其优点和局限性的思考。

一、Matlab的基本功能和特点Matlab是一种高级编程语言和开发环境,它具有广泛的数学和工程计算功能。

通过Matlab,我可以进行矩阵运算、数值计算、数据可视化、算法开发等一系列操作。

Matlab的语法简洁易懂,可以快速实现复杂的计算任务。

此外,Matlab还提供了大量的工具箱,如信号处理、控制系统、图像处理等,使得各种领域的科学研究和工程应用变得更加便捷。

二、实验结果与应用案例在本次实验中,我选择了一个经典的数值计算问题——求解非线性方程。

通过Matlab的数值计算能力,我可以使用不同的迭代方法来求解方程的根。

在实验中,我使用了牛顿迭代法、二分法和割线法来求解方程。

通过对比这些方法的收敛速度和精度,我得出了不同方法的优缺点。

在实际应用中,Matlab可以广泛应用于信号处理、图像处理、数据分析等领域。

例如,在信号处理中,我可以使用Matlab的信号处理工具箱来进行滤波、频谱分析等操作。

在图像处理中,我可以利用Matlab的图像处理工具箱进行图像增强、边缘检测等操作。

这些应用案例充分展示了Matlab在科学计算和工程应用中的重要性和灵活性。

三、Matlab的优点1. 强大的计算功能:Matlab提供了丰富的数学和工程计算函数,可以高效地进行复杂的计算任务。

2. 简洁的语法:Matlab的语法简洁易懂,使得编程变得更加高效和便捷。

3. 丰富的工具箱:Matlab提供了大量的工具箱,覆盖了各种领域的科学计算和工程应用需求。

4. 可视化能力强:Matlab提供了丰富的绘图函数,可以直观地展示数据和计算结果。

四、Matlab的局限性1. 高昂的价格:Matlab是一款商业软件,其价格较高,对于个人用户而言可能不太容易承受。

MATLAB实验报告

MATLAB实验报告

MATLAB实验报告MATLAB 实验报告姓名:专业:学号:实验⼀ MATLAB环境的熟悉与基本运算⼀、实验⽬的:1.熟悉MATLAB开发环境2.掌握矩阵、变量、表达式的各种基本运算⼆、实验基本知识:1.熟悉MATLAB环境:MATLAB桌⾯和命令窗⼝、命令历史窗⼝、帮助信息浏览器、⼯作空间浏览器⽂件和搜索路径浏览器。

2.掌握MATLAB常⽤命令3.MATLAB变量与运算符变量命名规则如下:(1)变量名可以由英语字母、数字和下划线组成(2)变量名应以英⽂字母开头(3)长度不⼤于31个(4)区分⼤⼩写MATLAB中设置了⼀些特殊的变量与常量,列于下表。

MATLAB运算符,通过下⾯⼏个表来说明MATLAB的各种常⽤运算符表2 MATLAB算术运算符表3 MATLAB关系运算符表4 MATLAB逻辑运算符表5 MATLAB特殊运算4.MATLAB的⼀维、⼆维数组的寻访表6 ⼦数组访问与赋值常⽤的相关指令格式5.MATLAB的基本运算表7 两种运算指令形式和实质内涵的异同表6.MATLAB的常⽤函数表8 标准数组⽣成函数表9 数组操作函数三、实验内容1、学习安装MATLAB软件。

2、学习使⽤help命令,例如在命令窗⼝输⼊help eye,然后根据帮助说明,学习使⽤指令eye(其它不会⽤的指令,依照此⽅法类推)3、学习使⽤clc、clear,观察command window、command history和workspace 等窗⼝的变化结果。

4、初步程序的编写练习,新建M-file,保存(⾃⼰设定⽂件名,例如exerc1、exerc2、exerc3……),学习使⽤MATLAB的基本运算符、数组寻访指令、标准数组⽣成函数和数组操作函数。

注意:每⼀次M-file的修改后,都要存盘。

四、实验结果练习A:(1)help rand,然后随机⽣成⼀个2×6的数组,观察command window、command history和workspace等窗⼝的变化结果。

matlab实验报告

matlab实验报告

实验一:MATLAB基本操作一、实验目的1、学习掌握MA TLAB语言的基本操作方法2、掌握命令窗口的使用3、熟悉MA TLAB的数据表示、基本运算和程序控制语句4、熟悉MA TLAB程序设计的基本方法二、实验内容和要求2.b.帮助命令的使用,查找SQRT函数的使用方法按Start-help-search,输入sqrtc.矩阵运算(1)已知A=[1 2;3 4];B=[5 5;7 8];求A^2*B输入A=[1 2;3 4];B=[5 5;7 8];A^2*B结果为:ans =105 115229 251(2)矩阵除法已知A=[1 2 3 ;4 5 6;7 8 9];B=[1 0 0;0 2 0;0 0 3];求A/B,A\BA/B=1.0000 1.0000 1.00004.0000 2.5000 2.00007.0000 4.0000 3.0000A\B=1.0e+016 *0.3152 -1.2609 0.9457-0.6304 2.5218 -1.89130.3152 -1.2609 0.9457(3)矩阵的转置及共轭转置已知A=[15+i,2-i,1;6*i,4,9-i];求A.',A'A=15.0000 + 1.0000i 2.0000 - 1.0000i 1.00000 + 6.0000i 4.0000 9.0000 - 1.0000iA.'=15.0000 + 1.0000i 0 + 6.0000i2.0000 - 1.0000i 4.00001.0000 9.0000 - 1.0000iA'=15.0000 - 1.0000i 0 - 6.0000i2.0000 + 1.0000i 4.00001.0000 9.0000 + 1.0000i(4)使用冒号选出指定元素已知A=[1 2 3;4 5 6;7 8 9];求A中第三列前两个元素;A中所有第二行的元素A =1 2 34 5 67 8 9求A中第三列前两个元素:A([1,2],3)ans =36求A中所有第二行的元素:A(2,1:1:end)ans =4 5 6(5)方括号[]用magic函数生成一个4阶魔方矩阵,删除该矩阵的第四列magic(4)ans =16 2 3 135 11 10 89 7 6 124 14 15 1a=ans;a(:,[1,2,3])ans =16 2 35 11 109 7 64 14 153..多项式(1)求多项式p(X)=x^3-2x-4的根(2)已知A=[1.2 3 5 0.9;5 1.7 5 6;3 9 0 1;1 2 3 4]求矩阵A的特征多项式;求矩阵多项式中未知数为20时的值;把矩阵A作为未知数代入到多项式中;(1)a=[1,0,-2,-4]x=roots(a)x =2.0000-1.0000 + 1.0000i-1.0000 - 1.0000i(2)求特征多项式系数:A=[1.2 3 5 0.9;5 1.7 5 6;3 9 0 1;1 2 3 4]p=poly(A)p =1.0000 -6.9000 -77.2600 -86.1300 604.5500求矩阵多项式中未知数为20时的值:c=polyval(p,20)c =7.2778e+004把矩阵A作为未知数代入到多项式中:c=polyval(p,A)c =1.0e+003 *0.3801 -0.4545 -1.9951 0.4601-1.9951 0.2093 -1.9951 -2.8880-0.4545 -4.8978 0.6046 0.43530.4353 0.0840 -0.4545 -1.16174、基本程序设计(1)编写命令文件:计算1+2+3+……+n<2000;n=0;s=0;while s<2000n=n+1;s=s+n;endn=n-1n =62(2)编写函数文件:分别用for和while循环结构编写程序,求2的0到n次幂的和。

(完整word)Matlab实验报告

(完整word)Matlab实验报告

实验一:Matlab操作环境熟悉一、实验目的1.初步了解Matlab操作环境.2.学习使用图形函数计算器命令funtool及其环境。

二、实验内容熟悉Matlab操作环境,认识命令窗口、内存工作区窗口、历史命令窗口;学会使用format 命令调整命令窗口的数据显示格式;学会使用变量和矩阵的输入,并进行简单的计算;学会使用who和whos命令查看内存变量信息;学会使用图形函数计算器funtool,并进行下列计算:1.单函数运算操作。

求下列函数的符号导数(1)y=sin(x);(2) y=(1+x)^3*(2-x);求下列函数的符号积分(1)y=cos(x);(2)y=1/(1+x^2);(3)y=1/sqrt(1—x^2);(4)y=(x1)/(x+1)/(x+2)求反函数(1)y=(x-1)/(2*x+3); (2) y=exp(x);(3) y=log(x+sqrt(1+x^2));代数式的化简(1)(x+1)*(x-1)*(x-2)/(x-3)/(x—4);(2)sin(x)^2+cos(x)^2;(3)x+sin(x)+2*x—3*cos(x)+4*x*sin(x);2.函数与参数的运算操作。

从y=x^2通过参数的选择去观察下列函数的图形变化(1)y1=(x+1)^2(2)y2=(x+2)^2(3) y3=2*x^2 (4) y4=x^2+2 (5) y5=x^4 (6) y6=x^2/2 3.两个函数之间的操作求和(1)sin(x)+cos(x) (2) 1+x+x^2+x^3+x^4+x^5乘积(1)exp(—x)*sin(x) (2) sin(x)*x商(1)sin(x)/cos(x); (2) x/(1+x^2); (3) 1/(x—1)/(x—2); 求复合函数(1)y=exp(u) u=sin(x) (2) y=sqrt(u) u=1+exp(x^2)(3) y=sin(u) u=asin(x) (4) y=sinh(u) u=-x实验二:MATLAB基本操作与用法一、实验目的1.掌握用MATLAB命令窗口进行简单数学运算。

matlab实验报告

matlab实验报告

matlab实验报告《matlab 实验报告》一、实验目的通过本次实验,熟悉 MATLAB 软件的基本操作和功能,掌握使用MATLAB 进行数学计算、数据处理、图形绘制等方面的方法和技巧,提高运用 MATLAB 解决实际问题的能力。

二、实验环境1、计算机:_____2、操作系统:_____3、 MATLAB 版本:_____三、实验内容及步骤(一)矩阵运算1、创建矩阵在 MATLAB 中,可以通过直接输入元素的方式创建矩阵,例如:`A = 1 2 3; 4 5 6; 7 8 9`,创建了一个 3 行 3 列的矩阵 A。

还可以使用函数来创建特定类型的矩阵,如全零矩阵`zeros(m,n)`、全 1 矩阵`ones(m,n)`、单位矩阵`eye(n)`等。

2、矩阵的基本运算加法和减法:两个矩阵相加或相减,要求它们的维度相同,对应元素进行运算。

乘法:矩阵乘法需要满足前一个矩阵的列数等于后一个矩阵的行数。

转置:使用`A'`来获取矩阵 A 的转置。

(二)函数的使用1、自定义函数可以在 MATLAB 中自定义函数,例如定义一个计算两个数之和的函数:```matlabfunction s = add_numbers(a,b)s = a + b;end```2、调用函数在命令窗口中输入`add_numbers(3,5)`即可得到结果 8。

(三)数据的读取和写入1、读取数据使用`load`函数可以读取数据文件,例如`load('datatxt')`。

2、写入数据使用`save`函数可以将数据保存到文件中,例如`save('resulttxt',A)`,将矩阵 A 保存到`resulttxt`文件中。

(四)图形绘制1、二维图形绘制折线图:使用`plot(x,y)`函数,其中 x 和 y 分别是横坐标和纵坐标的数据。

绘制柱状图:使用`bar(x,y)`函数。

2、三维图形绘制三维曲线:使用`plot3(x,y,z)`函数。

matlab 实验报告

matlab 实验报告

matlab 实验报告Matlab实验报告引言:Matlab是一种强大的数值计算和可视化软件,广泛应用于科学、工程和经济等领域。

本实验报告将介绍我在使用Matlab进行实验过程中的一些经验和结果。

实验一:矩阵运算在这个实验中,我使用Matlab进行了矩阵运算。

首先,我创建了一个3x3的矩阵A和一个3x1的矩阵B,并进行了矩阵相乘运算。

通过Matlab的矩阵乘法运算符*,我得到了一个3x1的结果矩阵C。

接着,我对矩阵C进行了转置操作,得到了一个1x3的矩阵D。

最后,我计算了矩阵C和矩阵D的点积,并将结果输出。

实验二:数据可视化在这个实验中,我使用Matlab进行了数据可视化。

我选择了一组实验数据,包括时间和温度两个变量。

首先,我将数据存储在一个矩阵中,并使用Matlab的plot函数将时间和温度之间的关系绘制成曲线图。

接着,我使用Matlab的xlabel、ylabel和title函数添加了横轴、纵轴和标题。

最后,我使用Matlab的legend函数添加了图例,以便更好地理解图表。

实验三:数值积分在这个实验中,我使用Matlab进行了数值积分。

我选择了一个函数f(x)进行积分计算。

首先,我使用Matlab的syms函数定义了符号变量x,并定义了函数f(x)。

接着,我使用Matlab的int函数对函数f(x)进行积分计算,并将结果输出。

为了验证结果的准确性,我还使用了Matlab的diff函数对积分结果进行了求导操作,并与原函数f(x)进行了比较。

实验四:信号处理在这个实验中,我使用Matlab进行了信号处理。

我选择了一个音频文件,并使用Matlab的audioread函数读取了该文件。

接着,我使用Matlab的fft函数对音频信号进行了傅里叶变换,并将结果绘制成频谱图。

为了进一步分析信号的特征,我还使用了Matlab的spectrogram函数绘制了信号的时频图。

通过对信号的频谱和时频图的观察,我可以更好地理解信号的频率和时域特性。

matlab实验一实验报告

matlab实验一实验报告

matlab实验一实验报告实验一:Matlab实验报告引言:Matlab是一种强大的数学软件工具,广泛应用于科学计算、数据分析和工程设计等领域。

本实验旨在通过使用Matlab解决实际问题,探索其功能和应用。

一、实验目的本次实验的主要目的是熟悉Matlab的基本操作和常用函数,了解其在科学计算中的应用。

二、实验内容1. 数值计算在Matlab中,我们可以进行各种数值计算,包括基本的加减乘除运算,以及更复杂的矩阵运算和方程求解。

通过编写相应的代码,我们可以实现这些功能。

例如,我们可以使用Matlab计算两个矩阵的乘积,并输出结果。

代码如下:```matlabA = [1 2; 3 4];B = [5 6; 7 8];C = A * B;disp(C);```2. 数据可视化Matlab还提供了强大的数据可视化功能,可以将数据以图表的形式展示出来,更直观地观察数据的规律和趋势。

例如,我们可以使用Matlab绘制一个简单的折线图,来展示某个物体在不同时间下的位置变化。

代码如下:```matlabt = 0:0.1:10;x = sin(t);plot(t, x);xlabel('Time');ylabel('Position');title('Position vs. Time');```3. 图像处理Matlab还可以进行图像处理,包括图像的读取、处理和保存等操作。

我们可以通过Matlab对图像进行增强、滤波、分割等处理,以及进行图像的压缩和重建。

例如,我们可以使用Matlab读取一张图片,并对其进行灰度化处理。

代码如下:```matlabimg = imread('image.jpg');gray_img = rgb2gray(img);imshow(gray_img);```三、实验结果与分析在本次实验中,我们成功完成了数值计算、数据可视化和图像处理等任务。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

主题:投资组合问题指导教师: 阮小娥老师制作时间:—学院:机械学院小组成员:机自07 赵磊 80机自07 周策 81机自07 邹业兵 82目录一、引言: (3)二、实验问题: (3)三、问题分析 (4)(1)、已知和股票有关的一些概念(实验所涉及的理论知识):4(2)、数学建模: (5)数学模型: (6)(3)、求解方法: (6)四、程序设计: (7)第一步: (7)第二、三、四步: (7)第五步: (8)五、程序运行结果(实验结果)为: (9)第一步: (9)A股49个周末的收盘价的变化情况 (9)B股49个周末的收盘价的变化情况 (9)C股49个周末的收盘价的变化情况 (10)第二、三、四步: (10)第五步: (11)六、问题的进一步拓展与实验 (12)实验过程 (12)1:条件假设 (12)2:建立模型 (12)3:模型求解 (13)程序设计 (14)程序运行结果 (14)七、实验的总结与体会: (19)赵磊: (19)周策: (19)邹业兵: (19)一、引言:现在社会,随着社会的发展和生产力的提升,人们的生活水平日益提高。

人们不仅仅满足于吃饱穿暖,人们对财富的追求也愈来愈强烈,此时股票作为社会化大生产的产物,股票至今已有将近似400年的历史,它的发展与人类经济的发展可以说是和谐统一的,而它集惊险刺激和可能存在的巨额经济效益的特点更是牢牢的牵住了现代人的心,毕竟,在已经不愁衣食的前提下,谁不想像股神巴菲特一样,动动脑筋便收拢全世界的财富呢所以,研究股票自然而然就成了现代人生活中极为重要的一部分。

二、实验问题:企业(或政府)或个人进行投资的目的是为了取得利润,在现代市场经济中,由于投资环境瞬息万变,任何一项投资的收益总是不能确定的,也就是说具有一定的风险,如何度量收益和风险呢下面给出一个实际问题进行分析:表9-1给出3只股票(A、B、C股)49个周末的收盘价,表9-1 股票49个周末的收盘价问题:(1) 分别计算这三只股票的周收益率的时间序列,平均收益率和他们收益率分布的方差;(2)允许卖空的条件下给出这3只股票风险最小的投资策略。

三、问题分析(1)、已知和股票有关的一些概念(实验所涉及的理论知识):记AtP为证券A在第t周末的价格,定义11---=At AtAt P PPrAt为证券A当周的证券收益率,由于投资初期不可能知道将来的收益率,因此收益率是一个随机变量,At A A A r r r r ........3,2,1为证券A 在某一时间段内收益率分布,且定义该收益率分布的均值T r E A ∑==T 1t At r )(为证券A 的期望收益率,并用该收益率分布的方差T r E r T t At At A ∑=-=122))((σ度量该证券A 期望收益的偏差称为风险,同样可以计算证券B 的期望收益率)(B r E 及方差2B σ两证券收益率分布间的协方差为∑=---==T t B Bt A At B A AB r E r r E r T r r 1))())(((11),cov(σ 协方差给出了证券A 和证券B 收益率分布相互影响的程度,协方差的大小依赖于收益所的单位,相关系数B A AB B A B A AB r r σσσσσρ==),cov(反映了证券A 和证券B 投资比率分为多少时风险比率最小,受益最大,这就是证券A 和证券B 的投资组合问题。

(2)、数学建模:用决策变量X1,X2,X3分别表示投资股票A ,B ,C 的投资比率,记股票A ,B ,C 的投资比率,记股票A ,B ,C 的收益率分别为Ea 、Eb 、Ec,根据题意, Ea、Eb、Ec是随机变量,投资的总收益率为S=X1*Ea+X2*Eb+X3*Ec也是随机变量,用E和D分别表示随机变量的数学期望和方差,则有(以下结果是经过第一、二、三、四步操作所得)Ea = , Eb= , Ec=Da = , Db= , Dc=Dab=、 Dac= 、 Dbc= ;由概率论知识可知covAC=Dac*Da^*Dc^covAB=Dab*Da^*Db^covBC=Dbc*Db^*Dc^投资的总期望收益率为:y=E=x1*Ea+x2*Eb+x3*Ec用总收益的方差衡量投资风险是,投资总收益的方差为:Z=D(x1*Ea+x2*Eb+x3*Ec)=Da*x1^2+Db*x2^2+Dc*x3^2+2covAB*x1*x2+2covAC*x1*x3+2covBC*x2*x3由于x1、x2、x3分别表示投资股票A、B、C的投资比例,所以x1+x2+x3=1数学模型:Minz(x1,x2,x3)=*x1^2+*x2^2+*x3^2+*x1*x2++*x2*x3x1+x2+x3=1(3)、求解方法:第一步:画出A、B、C三只股票49个周末收盘价的变化情况的图像(便于分析问题);第二步:求出A、B、C三只股票49个周末收盘价的期望收益率Ea、Eb、Ec ;第三步:求出A、B、C三只股票49个周末收盘价的方差Da、Db、Dc ;第四步:求出A、B、C三只股票收益率分布直间的协方差:Dab、Dac、Dbc;第五步:求出投资风险最小和此时的收益率。

四、程序设计:第一步:1、A股:x = 1:1:49;y = [,,,,,,,,11,,,,,,,,12,,,,,,,12,,,,,,,,,,,,,,,,,12,,12,12,12 ,,12,12,];plot(x,y)2、B股:x = 1:1:49;y = [,,,,,,,,1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]plot(x,y)3、C股:x = 1:1:49;y = [,,,,,11,,,,,,,,,,,,,15,,,14,,,,,,,,,,,,,,,14,,,,14,,,,,,,,]plot(x,y)第二、三、四步:PA=[ 11 12 12 12 12 1212 12 12 ];PB=[ .9 .93 .961 ];PC=[ 11 15 14 14 14 ];for i=2:1:49ra(i-1)=(PA(i)-PA(i-1))/(PA(i-1));rb(i-1)=(PB(i)-PB(i-1))/(PB(i-1));rc(i-1)=(PC(i)-PC(i-1))/(PC(i-1));endrA=[ra];rB=[rb];rC=[rc];T=48;Ea=sum(rA(1:T))/T;Eb=sum(rB(1:T))/T;Ec=sum(rC(1:T))/T;Dab=(1/(T-1))*sum((rA(1:T)-Ea).*(rB(1:T)-Eb)); Dac=(1/(T-1))*sum((rA(1:T)-Ea).*(rC(1:T)-Ec)); Dbc=(1/(T-1))*sum((rC(1:T)-Ec).*(rB(1:T)-Eb)); Da=sum((rA(1:T)-Ea).^2)/T;Db=sum((rB(1:T)-Eb).^2)/T;Dc=sum((rC(1:T)-Ec).^2)/T;covAB=Dab*((Da)^*((Db)^;covAC=Dab*((Da)^*((Dc)^;covBC=Dab*((Db)^*((Dc)^;第五步:h=[2*Da^2 2*covAB 2*covAC;2*covAB 2*Db^2 2*covBC;2*covAC 2*covBC 2*Dc^2];c=[0,0,0];a1=-[Ea,Eb,Ec];b1=0;A2=[1,1,1];b2=1;[x,fv,ef,out,lag]=quadprog(h,c,a1,b1,A2,b2)F=sqrt(fv)S=-a1*x五、程序运行结果(实验结果)为:第一步:A股49个周末的收盘价的变化情况.B股49个周末的收盘价的变化情况C股49个周末的收盘价的变化情况第二、三、四步:Ea= Eb = Ec =Dab =Dac =Dbc =Da =Db =Dc =covAB =covAC =covBC =第五步:x =fv =ef =1out =iterations: 1 constrviolation:algorithm: 'medium-scale: active-set' firstorderopt:cgiterations: []message: 'Optimization terminated.' lag =lower: [3x1 double]upper: [3x1 double]eqlin:ineqlin: 0F =S =由此可知(1)A、B、C的平均收益率分别为Ea= , Eb= , Ec= 收益率分布的方差分别为 Da= , Db= , Dc=(2)、投资A、B、C股票的投资比例分别为:%、% 、%,此时风险最小为%,收益率为%六、问题的进一步拓展与实验类似地,还可以引入四种以上数量的股票及股票不同时间的投资问题。

一般来说,投资人希望投资风险小而收益大,是一个多目标的优化问题。

我们可以将风险和收益的变化关系图像画出来。

给投资者最感官的认识。

最佳投资组合示例5 如表8-7给出4只股票在同一时期内的平均收益率r%,购买股票是交易率为p%,风险损失率q%,投资越分散,总的风险越小。

假定总风险用投资中最大一种股票的风险来度量,且同期银行存款利率是r%=5%,既无交易费有无风险,由于投资者承担风险的程度不一样,若给定风险一个界限a,季最大风险不超过a的情况下,为投资者建议一种投资策略,使其尽可能获得最大收益。

表8-7 股票信息实验过程为了建立最佳投资组合模型。

我们分以下三个步骤来完成次实验。

1:条件假设假设投资四种股票的资金分别为x,总投资金额为M.并设四种购票之间是相互独立的,且在投资的同一时间内都为定值。

不受意外因素影响。

2:建立模型投资四种股票的风险度为,i=1,2,3,4;购买四种股票时所付交易费分别为,i=1,2,3,4,则购买四种股票的收益分别为(),i=1,2,3,4,为使投资者获的更大利益,在总风险度不超过a的情况下,可建立如下模型:max∑=-4) (iiiixpr;.∑=+4) (iiiiixxpx= M iqix/M≤a, i=1,2,3,4ix≥0, i=0,1,2,3 3:模型求解模型简化为max∑=-4) (iiiixpr,.∑=+4) 1(iiixp= 1i q i x ≤a ,i=0,1,2,3,4 i x ≥0, i=0,1,2,3,4其中 i x =i X /M , i=0,1,2,3,4 将表8-7给定的数据带入模型的 minF=0x 1x 2x 3x 4x . 0x +1x +2x +3x +4x =1 1x ≤a 2x ≤a 3x ≤a 4x ≤ai x ≥0, i=0,1,2,3,4如何给定风险度a 没有一定的准则,不同的投资者承受风险度的能力不同,本次试验从a=0开始,以步长△a=编写下面的程序进行循环搜索。

相关文档
最新文档