东南大学2015短学期MATLAB作业3
matlab作业3
信息科学与工程学院Matlab作业**: ***专业: 电子信息工程2班学号: ********** 日期 2015 年 6 月 28 日1.线性方程求解:使用“\”,求解下面的线性系统,计算并显示误差向量。
364152773a b c a b b c ++=+=+= sy3_1.mA=[3,6,4;1,5,0;0,7,7]; B=[1;2;3]; X=A\B w=A*X-B 运行结果:X = -0.5824 0.5165 -0.0879 w =1.0e-15 *-0.1110 0 02.数值积分:使用trapz 或者quad 计算积分5/3x xedx -⎰,我们知道根据分部积分法可以计算这个积分得到/35/355/3003|9|249x x xe ee -----=-+,那么请比较实用trapz 计算得到的结果和直接使用积分后数值计算的结果差异,并显示在命令行窗口中。
Sy3_2.mx=0:0.0001:5; y=x.*exp(-x/3); z0=trapz(x,y) z1=-24*exp(-5/3)+9 w=z0-z1运行结果:z0 = 4.4670z1 = 4.4670 w =-9.3826e-10 3.矩阵求逆:计算矩阵1234⎡⎤⎢⎥⎣⎦的逆,使用inv函数。
然后将结果乘以原始矩阵,检查结果是否为单位阵。
Sy3_3.ma=[1,2;3,4];b=inv(a)c=b*a运行结果:b =-2.0000 1.00001.5000 -0.5000c =1.0000 00.0000 1.00004.多项式拟合:使用xlsread函数分别载入数据randomDatax.xls和randomDatay.xls,使用ployfit函数对该两对变量分别做1,2,3,4,5阶拟合,为了得到比较好的拟合效果,使用polyfit的中心与标尺模式,具体使用方法为[p,S,mu] = polyfit(x,y,n),它会返回三个参数,接下来使用对应的[y,delta] = polyval(p,x,S,mu)来根据x对y进行拟合。
matlab大作业实验报告,《Matlab程序设计》期末实验报告-大作业2015.doc
matlab⼤作业实验报告,《Matlab程序设计》期末实验报告-⼤作业2015.doc《MATLAB程序设计》实验报告学院: 学号: 姓名:⼀、题⽬:1、(10分)已知矩阵,⽤Matlab代码实现以下要求:(1)将矩阵赋给变量A,并在屏幕上显⽰A;(2)将A按列进列逆序重排,重排后的矩阵赋给变量B,并在屏幕上显⽰B;(3)⽤reshape命令将A重排为⼀个2⾏6列矩阵并赋给变量C;(4)将A重排为⼀个列向量,将其赋给变量D,求D的平均值,在屏幕上显⽰D和它的平均值;(5)⽤命令查看变量A的维数,并显⽰运⾏结果。
2、(10分)写代码实现以下要求:构造菜单项‘Plot’,菜单项Plot有两个⼦菜单项Plot sin(选择此项后执⾏画出曲线,线型为虚线,线条颜⾊为红⾊)和Plot cos(选择此项后执⾏画出曲线 ,线型为实线,线条宽度为2)。
3、(20分)已知,实现下列操作:(1)在同⼀个图形窗⼝,同⼀坐标系下⽤不同的颜⾊和线型绘制三条曲线,并添加图例来区分三条曲线(5分)。
(2)⽤subplot命令,以⼦图的⽅式绘制三条曲线,图形排列⽅式为三⾏⼀列(5分)。
(3) 分别⽤直⽅图(bar)、棒状图(stem)和填充图(fill)绘制三条曲线,以⼦图⽅式绘制,排列⽅式为3⾏3列,共9幅⼦图(10分)。
4、(10分)⽤surf命令绘制曲⾯图形,⽤shading interp命令进⾏插值着⾊处理并添加垂直颜⾊棒。
5、(15分)⾃2011年9⽉1⽇起,我国实⾏新的个⼈所得税征收办法,起征点为3500元,请⽤If-else if-else-end结构实现⼈⼯输⼊⽉收⼊后能计算出个⼈所得税的缴纳额并显⽰⽉收⼊10000元时应缴纳的税款。
级数应纳税所得额x(元)税率备注1x<=15003%x指⽉收⼊扣除起征点3500元之后的余额;215008000045%同上6. (10分)⽤while-end循环结构计算级数和的值,输⼊n值,能计算出f的值,并显⽰结果。
MATLAB习题及参考答案经典.doc
习题:1, 计算⎥⎦⎤⎢⎣⎡=572396a 与⎥⎦⎤⎢⎣⎡=864142b 的数组乘积。
2, 对于B AX =,如果⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=753467294A ,⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=282637B ,求解X 。
3, 已知:⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=987654321a ,分别计算a 的数组平方和矩阵平方,并观察其结果。
4, 角度[]604530=x ,求x 的正弦、余弦、正切和余切。
(应用sin,cos,tan.cot)5, 将矩阵⎥⎦⎤⎢⎣⎡=7524a 、⎥⎦⎤⎢⎣⎡=3817b 和⎥⎦⎤⎢⎣⎡=2695c 组合成两个新矩阵: (1)组合成一个4⨯3的矩阵,第一列为按列顺序排列的a 矩阵元素,第二列为按列顺序排列的b 矩阵元素,第三列为按列顺序排列的c 矩阵元素,即 ⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡237912685574(2)按照a 、b 、c 的列顺序组合成一个行矢量,即 []2965318772546, 将(x -6)(x -3)(x -8)展开为系数多项式的形式。
(应用poly,polyvalm)7, 求解多项式x 3-7x 2+2x +40的根。
(应用roots)8, 求解在x =8时多项式(x -1)(x -2) (x -3)(x -4)的值。
(应用poly,polyvalm)9, 计算多项式9514124234++--x x x x 的微分和积分。
(应用polyder,polyint ,poly2sym)10, 解方程组⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡66136221143092x 。
(应用x=a\b)11, 求欠定方程组⎥⎦⎤⎢⎣⎡=⎥⎦⎤⎢⎣⎡5865394742x 的最小范数解。
(应用pinv) 12, 矩阵⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡-=943457624a ,计算a 的行列式和逆矩阵。
(应用det,inv)13, y =sin(x ),x 从0到2π,∆x =0.02π,求y 的最大值、最小值、均值和标准差。
东南大学matlab第三次大作业
Matlab Worksheet 3Part A1. Using function conv_m.m to make convolution between the following to functions (x and h):x=[3, 11, 7, 0, -1, 7, -5, 0, 2]; h=[11, 9, 0, -7, -3, 2, 0 -1]; nx=[-2:6]; nh=[0:7];Plot the functions and convolution results.x=[3, 11, 7, 0, -1, 7,5,0, 2]; nx=[-2:6];h=[11, 9, 0, -7, -3,2,0,-1]; nh=[0:7];[y, ny]=conv_m(x,nx,h,nh); subplot(3,1,1); stem(nx,x); ylabel('x[n]');axis([-6 10 -20 20]); subplot(3,1,2); stem(nh,h); ylabel('h[n]');axis([-4 10 -20 20]); subplot(3,1,3); stem(ny,y); xlabel('n'); ylabel('y[n]');axis([-6 15 -200 200]);2. Plot the frequency response over π≤Ω≤0for the following transfer function by lettingΩ=j e z , where Ωis the frequency (rad/sample)., with appropriate labels and title.9.06.1)(2++=z z zz H .delta=0.01;Omega=0:delta:pi;H= (exp(j .* Omega)) ./ ((exp(j .* Omega)).^2+1.6*exp(j .* Omega)+0.9); subplot(2,1,1);plot(Omega, abs(H)); xlabel('0<\Omega<\pi'); ylabel('|H(\Omega)|');axis([0 pi 0 max(abs(H))]); subplot(2,1,2);plot(Omega,atan2(imag(H),real(H))); xlabel('0<\Omega<\pi');ylabel(' -\pi < \Phi_H <\pi') axis([0 pi -pi pi]);3. Use fft to analyse following signal by plotting the original signal and its spectrum.⎪⎭⎫ ⎝⎛+⎪⎭⎫ ⎝⎛+⎪⎭⎫ ⎝⎛=10244672sin 10241372sin 1024322sin ][n n n n x πππ.% fft N=1024; dt=1/N;t=0:dt:1-dt;x=sin(2*pi*32 .*t) +sin(2*pi*137 .*t)+sin(2*pi*467 .*t) ; subplot(2,1,1);plot(t,x), xlabel('t sec'),ylabel('x'); title('Signal and its Fourier Transform'); axis([min(t) max(t) 1.5*min(x) 1.5*max(x)]); X=fft(x); f=0:N-1;subplot(2,1,2);stem(f,abs(X)), xlabel('Hz'), ylabel('|X|'); axis([ 0 N/2 0 1.5*max(abs(X))]);4. Use the fast Fourier transform function fft to analyse following signal. Plot the original signal,and the magnitude of its spectrum linearly and logarithmically. Apply Hamming window to reduce the leakage.⎪⎭⎫ ⎝⎛+⎪⎭⎫ ⎝⎛+⎪⎭⎫ ⎝⎛=10247.4672sin 10244.1372sin 10245.322sin ][n n n n x πππ.The hamming window can be coded in Matlab asfor n=1:Nhamming(n)=0.54+0.46*cos((2*n-N+1)*pi/N); end;where N is the data length in the FFT.%Examine signal components %sampling rate 1024Hz N=1024; dt=1/N;t=0:dt:1-dt;%****define the signal**x=0.1*sin(2*pi*32.5 .*t ) + ... 0.2*sin(2*pi*137.4 .*t )+ ... 0.15*sin(2*pi* 467.7 .*t ) ; %*********************** subplot(3,1,1); plot(t,x);axis([0 1 -1 1]);xlabel('nT (seconds)'); ylabel('x[n]');%hamming window for n=1:Nw(n)=0.54+0.46*cos((2*n-N+1)*pi/N); end ;x1=x .*w;subplot(3,1,2); plot(t,x1);axis([0 1 -5 5]);xlabel('nT (seconds)'); ylabel('x[n] Hamming win'); X=fft(x1);df=1;f=0:df:N-1;subplot(3,1,3);plot(f,20*log10(abs(X))); axis([0 N/2 -60 60]);xlabel('k (Hz)');ylabel('|X[k]| (db)');Part BSimulation Using SIMULINKINTRODUCTIONThe objective of this laboratory is to learn about various properties of signals and systems by doing simulations in SIMULINK.PART 1. BASICS OF SIMULINK1.1 UNDERSTANDING SIMPLE WAVEFORMS AND INTEGRATIONCreate a pulse of height 2 units from time 0 to 4 seconds by subtracting two unit steps and adding a gain. Connect this pulse to an integrator with a gain of 0.5 and a zero initial condition. Connect oscilloscopes to show the pulse and the output of the integrator. You may wish to name your simulation (block) diagram; to do so use the save as feature under edit. Your block diagram should be similar as below.Before simulating you need to pull down the simulation header and double click on parameters. Unless told otherwise always assume that you can use the ode 45 integration algorithm shown in this window and the other default parameters. Typically you will only alter the start and stop times but for this first simulation you can use the default values of 0 and 10 seconds. Double click on the oscilloscopes to get the windows in which the traces will appear, pull down the simulate menu and click on run. Plot below the integrator input and output waveforms.Repeat the experiment but with an initial condition of –2 on the integrator. Again draw the results.Repeat the experiment but with an initial condition of –2 on the integrator. Again draw the results.1.2 FIRST ORDER SYSTEMA single time constant may be simulated using the transfer function block in which you enter the coefficients of the numerator and denominator polynomials. Set up the configuration in a new SIMULINK window to realise the transfer function 1/(s+2) with the input unit step and an oscilloscope connected to the output of the transfer function block. Plot the block diagram in the space below. Simulate the system for 5 seconds and plot the response.1.3 SECOND ORDER SYSTEMFor the second order all pole transfer function )2/(200220ωωζω++s s you will recall that if a time scale of ω0t is used for plotting the step response, the response shape will only be affected by changes in the damping ratio ζ. This can also be shown if we normalise the transfer function by replacing (s/ω0) by s n togive )12/(12++n n s s ζ. To study the effect of varying ζ on the step response we will therefore use the transfer function 1/(s 2+2ζs+1). Set up the following configuration for a simulation study.i) A unit step input. ii) Connect the unit step to a transfer function of 1/(s 2+2ζs+1) with ζ = 1.0. iii) Take a summing block and connect the input step to a +ve input and the transfer functionoutput to a –ve input.iv) Connect the output of this summer to a square function. This is obtained by using f(u) in thenonlinear blocks.v)Connect the output of this squarer to an integrator.vi)Connect two oscilloscopes to the circuit, one to the transfer function output and the other to the output of the integrator.vii)Also connect simout blocks to the same signals as the oscilloscopes. Rename the one connected to the integrator output simout1.Simulate the system with the values of ζ listed in the table below and fill in the other figures from the simulation results. To get an accurate value at the end of each run you can type simout1 in the MATLAB window. You can also measure the overshoot by making use of the maximum command: simply type max(simout) in the MATLAB window.% overshoot 2.1076 1.2500 1.1778 1.0571 1.0000 1.1285 2.1431PART 2. SIMULATION OF AIRCRAFT PITCH ANGLE AND ALTITUTEThe purpose IS to use "SIMULINK" to simulate a (much simplified) model of the longitudinal motion of a fighter aircraft.The "angle of attack", is the angle between the direction a plane is pointing, and the direction in which it actually moves through the air. For a plane flying at approximately constant altitude, this is equivalent to the "pitch angle", φ, as illustrated in Fig.2.1. This angle is important because it produces a lift force perpendicular to the axis of the plane, and hence a "normal acceleration", a n , (also shown in the figure).vThe pilot wants to be able to control the pitch angle (倾斜角度), and does so ultimately by rotating the front fins, and tail elevators of the aircraft, shown in Fig. 2.2. The first task is therefore to model theeffect of these movements on the "pitch rate" q ()=φ, and "normal acceleration" a n .Fig. 2.2 Illustration of control surfaces2.1: MODELLING THE AIRCRAFT AND ACTUATOR DYNAMICSi) Normal Acceleration, a nThe acceleration of the aircraft in a direction perpendicular to its axis, (the "normal accel.", a n ), is determined mainly by the angle θ1 of the tail elevators of the aircraft shown in Fig. 2.2. Indeed aerodynamic modelling shows that this relationship can be described by the differential equation,11116342.364.1982.81.3θθθ++=-+ n n n a a aConvert this relationship into a transfer function form:-ii) Pitch Rate, qThe rate at which the pitch angle changes, (the "pitch rate", q ), is determined mainly by the angle θ2 of the front fins of the aircraft shown in Fig. 2.2. Indeed aerodynamic modelling shows that this relationship can be described by the differential equation,2227.1125.782.81.3θθ++=-+ q q qConvert this relationship into a transfer function form:-iii) Actuator Dynamics + GearsThe tail elevators of the aircraft are driven directly by a hydraulic actuator which has a transfer function,G s s ()()=-+1414/ (3)To a unit step input U(s)=1/s , the response R(s) = G(s)⋅U(s)= 1411)14(14++-=+-s s s s . What form ofstep response r(t) would you expect from this actuator, and what is its time constant?The front fins and the tail elevators are both driven from the actuator by the same drive shaft through a gear box. The same gear on this drive shaft connects to the tail elevator gear wheel, which has 500 teeth, and the front fin gear wheel, which has 100 teeth, the relationship between θ1 and θ2 is52112==t t θθ(4) 2.2: SIMULATING THE AIRCRAFT AND ACTUATOR DYNAMICSi) F or the aircraft model you will need from the Continuous or Maths Library:-∙a Transfer Fcn block to represent the "tail elevator angle → normal accel" relationship (eqn (1))∙ a Transfer Fcn block to represent "front fin angle → pitch rate" relationship, (see eqn (2)) ∙ a Transfer Fcn block to represent the hydraulic "actuator dynamics", (see eqn (3)) ∙ a Gain block to represent the "gears", (see eqn (4))from the Sources Library:-∙ a Step Input block to act as a test input - (set Step time = 5sec, and Final value = 0.01 rad)from the Sinks Library:- ∙a ‘Scope block to monitor the pitch rate output - (set Horizontal Range = 20sec).ii) Now connect the blocks together, using the step input to drive the actuator, and the actuator to drive the tail elevators. The actuator is also used to drive the front fins via the gearbox. The 'Scope should be connected to the output of the "front fin angle → pitch rate" block. Note: you can take a branch from a signal line by dragging with the right mouse button from the point on the line at which you want the branch to start.Plot the block diagram you have created in the box below:-iii) From the pull-down menus of your simulation window, select Simulation/Parameters , in order to define how the simulation is to be performed. Set:-Stop Time = 20 sec; Initial Step Size = 0.001; Max Step Size = 0.01;iv) Finally double click on your ‘scope, so that you can watch the progress of the simulation, and then select Simulation/Start from the pull-down menu of your simulation window. You may want to adjustthe Vertical Range of your 'Scope, and re-run the simulation to get a good picture. Plot the response of the aircraft pitch rate below, and comment on these results. Note that it can be shown that if a transfer function has a denominator polynomial with a zero or negative coefficient then it must have a root with a zero or positive real part.2.3: SIMULATING THE PITCH RATE CONTROL SYSTEMThe designers are quite glad they simulated the response of the aircraft, before trying it for real. They now decide to improve the response by measuring the output (ie. what is actually happening to the pitch rate), and subtracting this from the input (ie. what they would like to happen), to produce an error signal. The error signal will be amplified, and then used to drive the actuator (see Fig. 2.3). In this way the actuator will automatically act so as to reduce any differences between the input demand, and the output response.Fig. 2.3 Feedback control of pitch ratei) Before you simulate the control system described above, you will probably find that you are short of space in your simulation window. Besides it would be nice to package the aircraft + actuator dynamics as a single block, so that they are distinct from anything added later as shown in Fig. 2.3.If you press the left mouse button in an empty space of the window, you get a "rubber band" box which you can drag to surround a group of elements. When you release the button, all elements inside are "selected". Use this technique to select all the aircraft + actuator dynamics, (but leave out the step input and ‘scope monitor). From the pull-down menu under Edit select Create a Subsystem, and your diagram will suddenly look as shown in Fig. 2.4.Fig. 2.4 New diagramii) Rename the "Subsystem" as "Actuator + Aircraft Dynamics" as shown in Fig. 2.3 by editing the text below the block. You can see what is inside by double clicking on the block itself, - try it! Notice that the input and output connections are labelled "in_1", "out_1" etc. Edit these to give them more meaningful names.iii) Now construct the planned control system, as illustrated in Fig. 2.3. Notice the pilot is now represented by a Signal Generator (from the Sources library), which should be set to produce a Square Wave, of frequency 0.1 Hz and peak amplitude 0.5.iv) The designers are unsure what value to use for the Gain. Open the Pitch Rate 'Scope, and run the simulation first for the Gain = -0.5 (as shown above), and then for the Gain = -5. Plot the two responses you obtain, and comment on the relative advantages and disadvantages of each:-Comments on responses (in terms of overshot and response time etc):PART 3: SIMILATION OF NONLINEAR SHIP ROLL DYNAMICSThe rolling motion of ships is of considerable interest to naval architects because even today around 50% of ships lost at sea, sink as a result of a capsize. The aim of this laboratory is to study such behaviour by:-∙constructing a nonlinear differential equation model for the system∙converting this model into a phase variable block diagram form suitable for simulation∙simulating the (nonlinear) response to sinusoidal excitation∙calculating the steady state response for a ship whose cargo has shifted dangerously to one sideFig. 3.1 Ship schematic3.1: MODELLING AND SIMULATION OF SHIP DYNAMICSi) Consider the ship sketch shown in Fig. 3.1. Given that:-∙ The effective inertia of the ship about its roll axis = J (1) ∙ The damping moment / torque due to friction between the body of the ship and the water, and due to turbulence round the "bilge keels" = c c 123 θθ+ (2) ∙ The restoring moment / torque due to the buoyancy and shape of the ship, as it is pushed to oneside = k k k 12335θθθ++ (3) ∙ The input / forcing moment due to the wave forces acting on the ship = u t ()- Using Newton’s second law of motion ∑=Torque Moment J / θ , the nonlinear differential equation which describes the rolling motion of the ship can be written as:-()()53321321)(θθθθθθk k k c c t u J ++-+-= (4)(ii) Re-arrange equation (4) to obtain an expression for the highest derivative of the output signal θ:-()()()53321321)(1θθθθθθk k k c c t u J++-+-= (5)iii) Hence complete the drawing of the SIMULINK block diagram for the ship roll system:-3.2: SIMULATING THE SHIP DYNAMICSi) For the ship model you will need:-from the Continuous and Maths Library:-∙ a few Integrator blocks - (assume zero initial conditions for now)∙ a few Gain blocks (not absolutely necessary)from the Nonlinear Library:-∙ a couple of Fcn blocks to implement the damping and stiffness terms (eqns (2), and (3)) from the Sources Library:- ∙ a Sine Wave block to simulate the sea waves u(t) - (set Ampl = 0.3 , and Freq = 0.6 rad/s)from the Sinks Library:-∙ a To Workspace block for the output ship roll angle, θ, of the simulation. This stores the results in a MATLAB variable which we can plot later, -(set the Variable name = theta, and the Maximum number of timesteps (ie points stored) = 5000)∙ an oscilloscope if you wish to see a waveform whilst the simulation is taking placeii) Drag the appropriate blocks over into your simulation window using the mouse, and double click on them to enter the appropriate parameter values. The numerical values for the coefficients of eqn (4) are:-;150;20;8.0;8.0;02.0;132121======k k k c c J(6)Note these values are given in consistent units so that θ is in radians. Finally by clicking on the text under each block, you can also enter a suitable description for each.iii) Now connect the blocks together, according to the phase variable block diagram you have plotted in section A. Note that some blocks will need changing so they "face the other way"; you can do this by selecting the block and "Flipping Horizontally " using the Options pulldown menu. Remember too that you can take a branch from a signal line by dragging with the right mouse button from the point on the line at which you want the branch to start.iv) From the pull-down menus of your simulation window, select Simulation/Parameters , in order to define how the simulation is to be performed. Set:-Stop Time = 10*pi (sec); Initial Step Size = 0.001; Max Step Size = 0.1;v) Now run the simulation by selecting Simulation/Start from the pull-down menu of your simulation window. If you do not use an oscilloscope you probably won't see much happening because the results are being stored in the MATLAB variable called 'theta', instead of being displayed immediately. After a few seconds however, you should hear a slight beep indicating the simulation is complete. Use of an oscilloscope is, however, recommended so that you can see if things ‘look all right’.vi)Now plot your results by typing plot(theta)at the MATLAB prompt in the MATLAB command window. Plot the response below, and comment on how it differs from what you would expect from a linear system if this were also excited with a sine wave input.。
matlab练习题和答案
matlab练习题和答案控制系统仿真实验Matlab部分实验结果目录实验一 MATLAB基本操作 ............................................................................................ 1 实验二 Matlab编程 .................................................................................................... 5 实验三Matlab底层图形控制 ..........................................................................................6 实验四控制系统古典分析.............................................................................................12 实验五控制系统现代分析 . (15)实验六 PID控制器的设计 ...........................................................................................19 实验七系统状态空间设计.............................................................................................23 实验九直流双闭环调速系统仿真 (25)实验一 MATLAB基本操作1 用MATLAB可以识别的格式输入下面两个矩阵1233,,1443678,i,,,,2357,,,,2335542,i,,,, A,1357B,,,2675342,i,,3239,,,,189543,,,,1894,,再求出它们的乘积矩阵C,并将C矩阵的右下角2×3子矩阵赋给D矩阵。
数值分析上机题Matlab(东南大学)3
0.22 0.24 0.26 0.28 0.30 0.32 0.34 0.36 0.38 0.40 0.42 0.44 0.46 0.48 0.50 0.52 0.54 0.56 0.58 0.60 0.62 0.64 0.66 0.68 0.70 0.72
152 139 128 119 110 103 96 90 85 80 76 72 68 65 62 59 56 53 51 49 47 45 43 41 39 38
========================================================================================================================
======================================================================================================================================================================== 习题 3_36 ======================================================================================================================================================================== Omega n x1 x2 x3 x4 x5 x6 x7 x8 x9
-0.71279 -0.71280 -0.71280 -0.71280 -0.71280 -0.71280 -0.71280 -0.71280 -0.71280 -0.71280 -0.71280 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281 -0.71281
matlab作业3参考答案
matlab作业3参考答案Matlab作业3参考答案Matlab作业3是一个综合性的编程任务,要求学生运用Matlab的各种功能和工具来解决实际问题。
本文将提供Matlab作业3的参考答案,并对其中的关键步骤和思路进行详细解释。
一、问题描述在本次作业中,学生需要解决一个关于图像处理的问题。
具体来说,给定一张彩色图像,学生需要编写Matlab代码来实现以下功能:1. 将彩色图像转换为灰度图像;2. 对灰度图像进行高斯滤波;3. 对滤波后的图像进行边缘检测;4. 对边缘图像进行二值化处理。
二、解决方案1. 将彩色图像转换为灰度图像首先,我们需要读取彩色图像。
可以使用Matlab的imread函数来实现。
然后,使用rgb2gray函数将彩色图像转换为灰度图像。
代码如下:```matlabrgbImage = imread('image.jpg');grayImage = rgb2gray(rgbImage);```2. 对灰度图像进行高斯滤波接下来,我们需要对灰度图像进行高斯滤波。
高斯滤波是一种常用的图像平滑方法,可以有效地去除图像中的噪声。
Matlab提供了fspecial函数来生成高斯滤波器。
代码如下:```matlabh = fspecial('gaussian', [3 3], 1);filteredImage = imfilter(grayImage, h);```3. 对滤波后的图像进行边缘检测在这一步中,我们需要对滤波后的图像进行边缘检测。
边缘检测可以帮助我们找到图像中的边缘和轮廓。
Matlab提供了多种边缘检测算法,如Sobel算子和Canny算子。
代码如下:```matlabedgeImage = edge(filteredImage, 'canny');```4. 对边缘图像进行二值化处理最后,我们需要对边缘图像进行二值化处理,将图像中的边缘转换为黑白两种颜色。
《Matlab语言及其应用》形考作业1-3答案
《Matlab语言及其应用》形考作业1-3答案形考作业1一、判断题(8小题,每小题2分,共16分)1.Matlab提供了两种创建符号变量和表达式的函数:sym和syms。
(A)判断题(2 分) 2分A.对B.错2.xy12_是合法的Matlab变量名。
(A)判断题(2 分) 2分A.对B.错3.Matlab中的变量必须遵循先定义后使用的原则。
(B)判断题(2 分) 2分A.对B.错4.Matlab的含义为矩阵实验室(Matrix Laboratory)。
(A)判断题(2 分) 2分A.对B.错5.Matlab中注释行的开头是#。
(B)判断题(2 分) 2分A.对B.错6.plot( )是Matlab最基本的绘图函数。
(A)判断题(2 分) 2分A.对B.错7.双纵坐标图形的绘制函数是plotyy()。
(A)判断题(2 分) 2分B.错8.在Matlab中,二维饼图由函数pie( )来实现。
(A)判断题(2 分) 2分A.对B.错二、单选题(9小题,每小题3分,共27分)9.清空Matlab工作空间内所有变量的指令是(C)。
单选题(3 分) 3分A.clcB.clsC.clearD.clf10.在循环结构中跳出循环,执行循环后面代码的命令为(B)。
单选题(3 分) 3分A.returnB.breakC.continueD.keyboard11.图形窗口的分割函数subplot(2,2,[1 3])可以把一个窗口分割为(B)个子窗口。
单选题(3 分) 3分A.2B.3C.4D.无法确定12.一般情况下,绘图命令每执行一次就刷新当前图形窗口,图形窗口原有图形将不复存在。
若希望在已绘图形上叠加新的图形,可以使用(A)命令。
单选题(3 分) 3分A.hold onB.hold offD.plot13.Matlab中求极限的函数为(B)。
单选题(3 分) 3分A.diff( )B.limit( )C.int( )D.solve( )14.Matlab中求导数的函数为(A)。
Matlab作业3(数值分析)答案
Matlab作业3(数值分析)机电工程学院(院、系)专业班组学号姓名实验日期教师评定1.计算多项式乘法(x2+2x+2)(x2+5x+4)。
答:2. (1)将(x-6)(x-3)(x-8)展开为系数多项式的形式。
(2)求解在x=8时多项式(x-1)(x-2) (x-3)(x-4)的值。
答:(1)(2)3. y=sin(x),x从0到2π,∆x=0.02π,求y的最大值、最小值、均值和标准差。
4.设x=[0.00.30.8 1.1 1.6 2.3]',y=[0.500.82 1.14 1.25 1.351.40]',试求二次多项式拟合系数,并据此计算x1=[0.9 1.2]时对应的y1。
解:x=[0.0 0.3 0.8 1.1 1.62.3]'; %输入变量数据xy=[0.50 0.82 1.14 1.25 1.35 1.40]'; %输入变量数据yp=polyfit(x,y,2) %对x,y用二次多项式拟合,得到系数px1=[0.9 1.2]; %输入点x1y1=polyval(p,x1) %估计x1处对应的y1p =-0.2387 0.9191 0.5318y1 =a) 1.29095.实验数据处理:已知某压力传感器的测试数据如下表p为压力值,u为电压值,试用多项式dcpbpappu+++=23)(来拟合其特性函数,求出a,b,c,d,并把拟合曲线和各个测试数据点画在同一幅图上。
解:>> p=[0.0,1.1,2.1,2.8,4.2,5.0,6.1,6.9,8.1,9.0,9.9];u=[10,11,13,14,17,18,22,24,29,34,39];x=polyfit(p,u,3) %得多项式系数t=linspace(0,10,100);y=polyval(x,t); %求多项式得值plot(p,u,'*',t,y,'r') %画拟和曲线x =0.0195 -0.0412 1.4469 9.8267。
2015年大作业
eqh 值为
Q eQh n
11400 0.77597 0.67021 0.56394 0.49529 0.46465
11800 0.56612 0.46763 0.44646 0.45345 0.47042
12200 0.46281 0.45715 0.45139 0.50363 0.47879
12600 0.51965 0.46299 0.45068 0.44123 0.45796
(t ), y(t ), y (t ) 绘制在一个图形窗口的四个子图中去, 将 y(t ), y
要求坐标轴加标注,图形加网格; (5) 线条粗细设定为 2 个单位。
2. 已知某非线性系统状态空间描述如下 状态方程
1 = −3x1 + x2 ⎧x ⎧ y1 = x1 + x2 ⎨ 3 ,输出方程 ⎨ x = − x − x − x ⎩ y2 = x1 − x2 ⎩ 2 1 2 2 ⎧ x1 (0) = 20 系统初始状态 ⎨ x (0) = −20 ⎩ 2
= 2y +x− x
μ ∗ ( x + μ) μ( x − μ ∗ )
r
3 1
−
r
3 2
= −2x + y− y ,
μ ∗ y μy
r13 − r23
,
其中, μ = 1 / 82.45, μ ∗ = 1 − μ , r1 =
( x + μ ) 2 + y 2 , r2 = ( x − μ ∗ ) 2 + y 2
2015 年《基于 MATLAB/SIMULINK 的系统建模与仿真》大作业
1. 使用积分器、 加法器、 数乘器等基础部件搭建 simulink 仿真模型,计算如下系统的动态行为。
2015matlab第一次作业
第一次作业(2015.10.27)目的:熟悉MA TLAB 的基本应用要求:用editor 对程序进行编辑和调试,形成相应的脚本文件(*.m )。
1.1若已知某电路参数,输入电压,绘制其输出电压的波形如图1,已知:R1=3Ω,R3=6Ω,C=1uF ;us=18V ,is=3A 。
在零初始条件下,开关s 开路,当t=0时,开关S 闭合,求uc ,并画出波形;图1下列程序仅供参考(注:本例中的RC 电路非图1所示的电路):已知一阶RC 电路的端电压的表达式为 10s ~0 ,62==-t e u t ,试绘制电压波形u 。
t=0:0.1:10;u=6*exp(-2*t);plot(t,u);title('RC response');xlabel('t/s');ylabel('V/V')grid2. 已有两组测试数据不等的曲线,对其进行拟合和插值处理。
2.1给例程2.1加入注释,说明程序的用途和调试结果,尝试修改x ,y ,或polyfit 的不同设置。
熟悉fitting 工具例程2.1:clear;clc;close;x=0:0.1:2*pi;y = sin(x)+0.5*rand(size(x));p = polyfit(x,y,4)y1= polyval(p,x);plot(x,y, '+',x,y1, '-r')title('fitting')2.2 给例程2.2加入注释,说明程序的用途和调试结果,尝试修改xs,ys,或xi,或interp1的不同设置,熟悉插值的用法。
例程2.2:clear;clc;close;xs=0:0.2:2*pi;ys= sin(xs);xi=0: 0.1: 2*pi;y1=interp1(xs,ys,xi,'linear');plot(xs,ys,'+k')hold onplot(xi,y1,':r')legend('sampled point','linear','spline');title('INTERP')3. 试用solve求解常系数微分方程。
东南大学matlab课程作业
Matlab Worksheet 1Part A1. Get into Matlab: Use the diary command to record your activity in to a file: diary mydiary01.doc before you start your work. (And diary off to switch off your diary when you finish your work.)At the Command Window assign a v alue x=10, then use the Up Key ↑ to repeat the expression, editing it to show the effect of using a semicolon after the 10, namely x=10;Answers:>> x=10x =10>> x=10;2. Confirm whether the following names of variables are acceptable: a) Velocity Yes Nob) Velocity1 Yes Noc) Velocity.1 Yes Nod) Velocity_1 Yes Noe) Velocity-01 Yes Nof) velocityONE Yes Nog) 1velocity Yes No3. Assign two scalar variables x and y values x=1.5, y=2.3, then construct Matlab expressions for the following: a) y x y x z +=35 b) ()3/27y x z = c) ()y e x x z 25106/111log -⋅⎥⎦⎤⎢⎣⎡+-= d) )2cos()2sin(x e y x z x ππ+-=Answers:>> x=1.5;y=2.3z1=5*x^3*y/(x+y)z2=(x^7*y^0.5)^(2/3)z3=(x^(1/6)/(log10(x^5-1))+1)*exp(-2*y)z4=sin(2*pi*x-y)+exp(x).*cos(2*pi*x)z1 =10.2138z2 =8.7566z3 =0.0232z4 =-3.73604. Assign two variables with complex values u=2+3j and v=4+10j and then construct expression for:a) v u b) j uv 2+ c) ju 2 d) u j ve π2- Answers:>> u=2+3j;v=4+10j;z1=u/vz2=u*v+2jz3=u/2jz4=v*exp(-j*2*pi*u)z1 =0.3276 - 0.0690iz2 =-22.0000 +34.0000iz3 =1.5000 - 1.0000iz4 =6.1421e+08 + 1.5355e+09ie the colon operator : to assign numerical values between 0 and 1 to vector arrayvariable a in steps of 0.1.Answer:>> V=0:0.1:1V =Columns 1 through 100 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.80000.9000Column 111.0000e linspace function to assign 20 values to vector variable y between 20 and 30.Answer:>> V=linspace(20,30,20)V =Columns 1 through 1020.0000 20.5263 21.0526 21.5789 22.1053 22.6316 23.1579 23.684224.2105 24.7368Columns 11 through 2025.2632 25.7895 26.3158 26.8421 27.3684 27.8947 28.4211 28.947429.4737 30.00007.Assign 20 values to a variable h increasing logarithmically between 10 and 1000.Next, use the colon operator to assign the first 10 elements of h to a variable p.Answers:>> v=logspace(1,3,20)v =1.0e+03 *Columns 1 through 100.0100 0.0127 0.0162 0.0207 0.0264 0.0336 0.0428 0.05460.0695 0.0886Columns 11 through 200.1129 0.1438 0.1833 0.2336 0.2976 0.3793 0.4833 0.61580.7848 1.0000>> p=v(1:10)p =10.0000 12.7427 16.2378 20.6914 26.3665 33.5982 42.8133 54.5559 69.519388.58678.Create 6 element row vector z with values 1.0 1.2 1.6 -1.7 1.8 1.9, then constructan expression for the sum of the 2nd 4th and 6th elements of z.Answers:>> z=[1.0 1.2 1.6 -1.7 1.8 1.9]x=z(2)+z(4)+z(6)z =1.0000 1.2000 1.6000 -1.7000 1.8000 1.9000x =1.4000e the colon operator to create a vector array x between 10 and -10in steps of -1,and second, an array vector y between 20 and -20 in steps -2 .a)Add x and y, b) subtract 10x from 5y.Answers:>> x=10:-1:-10y=20:-2:-20x =Columns 1 through 1710 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6Columns 18 through 21-7 -8 -9 -10y =Columns 1 through 1720 18 16 14 12 10 8 6 4 2 0 -2 -4 -6 -8 -10 -12Columns 18 through 21-14 -16 -18 -20>> x+yans =Columns 1 through 1730 27 24 21 18 15 12 9 6 3 0 -3 -6 -9 -12 -15 -18Columns 18 through 21-21 -24 -27 -30>> 5*y-10*xans =Columns 1 through 170 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0Columns 18 through 210 0 0 0e the size,length, who and whos commands to establish the size and length of xand y from Question 9, and use transpose operator ’ to convert vector x fromQuestion 9.Answers:>> size(x)length(x)ans =1 21ans =21>> whoYour variables are:ans x y>> whosName Size Bytes Class Attributesans 1x1 8 doublex 1x21 168 doubley 1x21 168 double>> z=x'z =10987654321-1-2-3-4-5-6-7-8-9-1011.Show that if w=[ 2i 3i 3+i] the .’ operator creates the transpose. What effect does theoperator ’ applied to w have on its own?Answer:>> w=[ 2i 3i 3+i]w =0 + 2.0000i 0 + 3.0000i 3.0000 + 1.0000i>> x=w'x =0 - 2.0000i0 - 3.0000i3.0000 - 1.0000i>> y=w.'y =0 + 2.0000i0 + 3.0000i3.0000 + 1.0000ie the ones function to create a 4 by 6 array of 1’s. Considering just the shape ofthe resulting array, what do the expression ones(3), ones(5) and ones(7) all have in common?Answer:>> v=ones(4,6)v =1 1 1 1 1 11 1 1 1 1 11 1 1 1 1 11 1 1 1 1 1>> ones(3)ans =1 1 11 1 11 1 1>> ones(5)ans =1 1 1 1 11 1 1 1 11 1 1 1 11 1 1 1 11 1 1 1 1Ones(n)是n阶全为1的方阵13.Create using the rand function a 5 by 4 random matrix and assign it to matrix arrayvariable A and observe carefully what A(:,3) A(1:2) and A(3,[2 4]) mean.Answer:>> A=rand(5,4)A =0.8147 0.0975 0.1576 0.14190.9058 0.2785 0.9706 0.42180.1270 0.5469 0.9572 0.91570.9134 0.9575 0.4854 0.79220.6324 0.9649 0.8003 0.9595>> A(:,3)ans =0.15760.97060.95720.48540.8003>> A(1:2)ans =0.8147 0.9058>> A(3,[2 4])ans =0.5469 0.9157ing array subscripts, create an expression for the sum of the element in the topright-hand-corner of A and the bottom left-hand-corner of A. Also assign the 2nd column of A to a column vector b, and assign the 3rd row of A to a row vector d.Answer:>> A=rand(5,4)A =0.7513 0.9593 0.8407 0.35000.2551 0.5472 0.2543 0.19660.5060 0.1386 0.8143 0.25110.6991 0.1493 0.2435 0.61600.8909 0.2575 0.9293 0.4733>> x=A(1,4)+A(4,1)x =1.0491>> b=A(:,2)b =0.95930.54720.13860.14930.2575>> d=A(3,:)d =0.5060 0.1386 0.8143 0.2511>>diary off to switch off your diary now.15. Using the colon operator, assign a row vector array t, values between 0 and 10 in steps of 0.01. Use the ; operator to prevent displaying the information. Obtain the term-by-term values of functions:a) t e z 05.0-= b) )sin(05.0t e z t π-= c) )2cos()sin(05.0t t e z t ππ-=Answers:0.5874 0.6186 0.6473 0.6731 0.6958 0.7153 0.7314 0.7439 0.75280.7581Columns 551 through 5600.7596 0.7573 0.7513 0.7417 0.7284 0.7117 0.6916 0.6684 0.64210.6131Columns 561 through 5700.5815 0.5476 0.5117 0.4741 0.4350 0.3948 0.3538 0.3123 0.27060.2291Columns 571 through 5800.1880 0.1477 0.1085 0.0706 0.0344 0.0000 -0.0322 -0.0621-0.0895 -0.1141Columns 581 through 590-0.1359 -0.1548 -0.1705 -0.1832 -0.1928 -0.1992 -0.2025 -0.2027 - 0.2000 -0.1944Columns 591 through 600-0.1861 -0.1753 -0.1621 -0.1467 -0.1295 -0.1105 -0.0901 -0.0686 -0.0462 -0.0232Columns 601 through 610-0.0000 0.0232 0.0461 0.0684 0.0898 0.1099 0.1287 0.1457 0.1608 0.1737Columns 611 through 6200.1843 0.1923 0.1976 0.2001 0.1997 0.1962 0.1897 0.1801 0.1675 0.1518Columns 621 through 6300.1332 0.1117 0.0875 0.0607 0.0315 -0.0000 -0.0335 -0.0687 -0.1055 -0.1435Columns 631 through 640-0.1824 -0.2221 -0.2621 -0.3022 -0.3420 -0.3813 -0.4197 -0.4569 -0.4927 -0.5267Columns 641 through 650-0.5587 -0.5885 -0.6157 -0.6403 -0.6619 -0.6804 -0.6957 -0.7076 -0.7161 -0.7211Columns 651 through 660-0.7225 -0.7204 -0.7147 -0.7055 -0.6929 -0.6770 -0.6579 -0.6358 -0.6108 -0.5832Columns 661 through 670-0.5532 -0.5209 -0.4868 -0.4510 -0.4138 -0.3756 -0.3366 -0.2971 -0.2574 -0.2179Columns 671 through 680-0.1788 -0.1405 -0.1032 -0.0672 -0.0327 -0.0000 0.0307 0.05910.0851 0.1085Columns 681 through 6900.1293 0.1472 0.1622 0.1743 0.1834 0.1895 0.1926 0.1928 0.1902 0.1849Columns 691 through 7000.1771 0.1667 0.1542 0.1396 0.1231 0.1051 0.0857 0.0652 0.0439 0.0221Columns 701 through 7100.0000 -0.0221 -0.0439 -0.0650 -0.0854 -0.1046 -0.1224 -0.1386 - 0.1530 -0.1653Columns 711 through 720-0.1753 -0.1829 -0.1880 -0.1903 -0.1899 -0.1866 -0.1805 -0.1713 - 0.1593 -0.1444Columns 721 through 730-0.1267 -0.1063 -0.0832 -0.0577 -0.0299 0.0000 0.0318 0.06540.1003 0.1365Columns 731 through 7400.1735 0.2113 0.2493 0.2874 0.3253 0.3627 0.3992 0.4346 0.4686 0.5010Columns 741 through 7500.5315 0.5598 0.5857 0.6090 0.6296 0.6472 0.6618 0.6731 0.68120.6859Columns 751 through 7600.6873 0.6853 0.6798 0.6711 0.6591 0.6440 0.6258 0.6048 0.5810 0.5548Columns 761 through 7700.5262 0.4955 0.4630 0.4290 0.3936 0.3573 0.3201 0.2826 0.2449 0.2073Columns 771 through 7800.1701 0.1336 0.0981 0.0639 0.0311 0.0000-0.0292 -0.0562 -0.0809 -0.1033Columns 781 through 790-0.1230 -0.1400 -0.1543 -0.1658 -0.1744 -0.1802 -0.1832 -0.1834 -0.1810 -0.1759Columns 791 through 800-0.1684 -0.1586 -0.1467 -0.1328 -0.1171 -0.1000 -0.0815 -0.0621 -0.0418 -0.0210Columns 801 through 810-0.0000 0.0210 0.0417 0.0619 0.0812 0.0995 0.1164 0.1318 0.1455 0.1572Columns 811 through 8200.1667 0.1740 0.1788 0.1811 0.1807 0.1775 0.1717 0.1630 0.1516 0.1374Columns 821 through 8300.1205 0.1011 0.0792 0.0549 0.0285 0.0000 -0.0303 -0.0622 -0.0954 -0.1298Columns 831 through 840-0.1651 -0.2010 -0.2372 -0.2734 -0.3094 -0.3450 -0.3797 -0.4134 - 0.4458 -0.4766Columns 841 through 850-0.5055 -0.5325 -0.5571 -0.5793 -0.5989 -0.6157 -0.6295 -0.6403 - 0.6480 -0.6525Columns 851 through 860-0.6538 -0.6518 -0.6467 -0.6384 -0.6270 -0.6126 -0.5953 -0.5753 - 0.5527 -0.5277Columns 861 through 870-0.5005 -0.4714 -0.4405 -0.4081 -0.3744 -0.3398 -0.3045 -0.2688 - 0.2329 -0.1972Columns 871 through 880-0.1618 -0.1271 -0.0934 -0.0608 -0.0296 -0.0000 0.0277 0.05350.0770 0.0982Columns 881 through 8900.1170 0.1332 0.1468 0.1577 0.1659 0.1714 0.1743 0.1745 0.1721 0.1673Columns 891 through 9000.1602 0.1509 0.1395 0.1263 0.1114 0.0951 0.0776 0.0590 0.0398 0.0200Columns 901 through 9100.0000 -0.0200 -0.0397 -0.0589 -0.0773 -0.0946 -0.1108 -0.1254 - 0.1384 -0.1495Columns 911 through 920-0.1586 -0.1655 -0.1701 -0.1722 -0.1718 -0.1689 -0.1633 -0.1550 - 0.1442 -0.1307Columns 921 through 930-0.1147 -0.0962 -0.0753 -0.0522 -0.0271 -0.0000 0.0288 0.05910.0908 0.1235Columns 931 through 9400.1570 0.1912 0.2256 0.2601 0.2944 0.3281 0.3612 0.3932 0.4240 0.4533Columns 941 through 9500.4809 0.5065 0.5300 0.5511 0.5697 0.5856 0.5988 0.6091 0.6164 0.6207Columns 951 through 9600.6219 0.6200 0.6151 0.6072 0.5964 0.5827 0.5663 0.5472 0.5257 0.5020Columns 961 through 9700.4761 0.4484 0.4190 0.3882 0.3562 0.3233 0.2897 0.2557 0.2216 0.1876Columns 971 through 9800.1539 0.1209 0.0888 0.0578 0.0281 0.0000-0.0264 -0.0509 -0.0732 -0.0934Columns 981 through 990-0.1113 -0.1267 -0.1396 -0.1500 -0.1578 -0.1631 -0.1658 -0.1660 -0.1637 -0.1592Columns 991 through 1000-0.1524 -0.1435 -0.1327 -0.1201 -0.1060 -0.0905 -0.0738 -0.0562 -0.0378 -0.0190Column 1001-0.000016.For Question 15 c), use the plot function to plot the value z against t. And usexlabel and ylabel to label the t axis ‘time’ and z axis ‘Response’ and use title to give your figure a title. Copy the figure into your mydiary01.doc (from the figure edit menu, select copy, then open mydiary01.doc and paste in the figure as an object).Answers:>> plot(t,z3,'m','LineWidth',2)xlabel('time')ylabel('Response')title('z=exp(-0.05*t)*sin(πt)*cos(2πt))')Part B1.Create a new Script m-file, called myfirst.m, to assign values to three vector arraysa, b, and c; add each component together and assign to array d, whereA=[ 1, 2, 3], b=[4 5 6], c=[7 8 9]. Run myfirst.m. Examine the variable values in the WorkSpace . Edit myfirst.m to change variable b to b=[10 20 30]. Re-run myfirst.mAnswers:myfirst.mA=[ 1, 2, 3];B=[4 5 6];C=[7 8 9];D=A+B+Cresult:>> myfirstD =12 15 18myfirst.mA=[ 1, 2, 3];B=[10 20 30];C=[7 8 9];D=A+B+C(2-2)result:>> myfirstD =18 30 422.Create a function m-file, called myfun.m, that accepts two arrays x and y asarguments, and produces the sum of x and y, and gives as the function output, the maximum of x+y.Answer:(1)myfun.mfunction[M] = myfun(x,y)%MAXIMUMsum=x+y;t=length(x)-1;fori=1:t;ifsum(1+i)>sum(i)max=sum(1+i);sum(1+i)=sum(i);endendM=maxend(2)result>> x=[1 2 3]y=[4 5 6]x =1 2 3y =4 5 6>> myfun(x,y)M =9ans =93.Modify your script m-file myfirst.m by calling in it myfun.m twice, namely with aand b as parameters in the argument list, and second with parameters b and c. Run myfirst.mand afterwards. Examine the WorkSpace variable values.Answers:(1)myfirst_modify.mA=[ 1, 2, 3];B=[10 20 30];myfun(A,B);disp('calling myfun again')C=[7 8 9];myfun(B,C);(2)result>> myfirst_modifyM =33calling myfun againM =394. Modify your script m-file myfirst.m by introducing variables A and B. Assignscalar values A=1, B=2. Declare A and B as global variables both in myfirst.m and myfun.m. Modify myfun.m by adding A and B to the maximum value of x+y. Answers:(1)myfirst.mglobalA B;A=1;a=[ 1, 2, 3];b=[10 20 30];myfun(a,b);disp('calling myfun again')c=[7 8 9];d=a+b+c;;myfun(c,d);(2)myfun.mfunction[M] = myfun(x,y)%MAXIMUMglobalA B;A=1;B=2;sum=x+y;t=length(x)-1;fori=1:t;ifsum(1+i)>sum(i)max=sum(1+i);sum(1+i)=sum(i);endM=max+A+Bend(3)result>> myfirstM =36calling myfun againM =545.Create a script m-file and, using a pair of nested for loops, assign to a 5 by 5matrix array A, values equal to the sum of the subscripts. Ensure that the array is first cleared before the loops and afterwards display the array values. Run the m-file.Answer:(1)home_work_1_B_5.mclearall;closeall;fori=1:5forj=1:5A(i,j)=i+j;endendA(2)result>> home_work_1_B_5A =2 3 4 5 63 4 5 6 74 5 6 7 85 6 7 8 96 7 8 9 106. Relational Operators:- Assign variables x=1 and y=2, then examine the values of the expressions:a) x>y, b)x<y, c) x==y. What is the value of z=(x>y)*(x==y)?Answer:(1)home_work_1_B_6.mx=1;y=2;u1=x>yu2=x<yu3=(x==y)z=(x>y)*(x==y)(2)result>> home_work_1_B_6u1 =u2 =1u3 =z =7. While loop:- Create a script m-file, and using a ‘while loop’, generate the partial sum for the series: ...31211+++πππ which stops adding-on terms when they are less than 1e-6 times the total sum.Answer:(1)home_wor_1_B_7.mclear all;close all;i=1;sum=0;t=1/pi;whilet>=exp(-6)sum=sum+t;i=i+1;t=1/(i*pi);enddisp('多项式之和sum') sumdisp('循环次数i')idisp('多项式最后一项t') t(2)result>> >> home_work_1_B_7 多项式之和sumsum =1.7294循环次数ii =129多项式最后一项tt =0.00258. While loop:- Use a ‘while loop’ to keep adding array A to itself until just one ofthe elements in A is greater than, but not equal to 40 where, initially ⎥⎦⎤⎢⎣⎡=4321A .Answer:(1)home_work_1_B_8.mA=[1 2 3 4 ];m=max(A(:));whilem<=40;A=A+A;m=max(A(:));endA(2)result>> home_work_1_B_8A =16 3248 649. If statements:- Create a function m-file that receives variable x and y as arguments, then uses an ‘if’ statement internally to execute the condition that: ‘if x is greater than y the function takes the value x, otherwise it takes the value y’. Test it on 2 numbers x=1 and y=1.Answer(1)greater.mfunction[ max ] = greater( x,y )%lbs_home_work_1_B_9ifx>y max=x;elsemax=y;endend(1) result>> x=1;y=1;>> greater(x,y)ans =110.If statements:- Create a function m-file that receives variable x, y and z asarguments, then uses an ‘if elseif and else’ statements, generate the output value to be =x if first of all x>1, otherwise =y if y>10, otherwise =z if z>20, otherwise =1 if none of those conditions hold. Test it on values (100,100,100) and (0, 0,0).Answer:(1)compare.mfunction[max] = compare( x,y,z )%lbs_home_work_1_B_10ifx>1 max=x;elseify>10 max=y;elseifz>2 max=z;elsemax=1;endendendmaxend(2)result>> compare(100,100,100)max =100ans =100>> compare(0,0,0)max =1ans =111.Switch Statement:- Create a function with one input argument x to test whether theexpression: real(x^2) is equal to 0,1, or -1, which then displays a string stating appropriately whether: ‘x is equal to zero’, ‘x is equal to +1 or -1’ or ‘x is equal to +j or –j’; otherwise display ‘test failed’. Test your m-file on three values: x=0, x=10 and x=j (1j).-=(1)test.mfunctionf = test( x%lbs home work 1 partB 11t=real(x^2);Switch tcase 0, disp('x is equal to zero');case1, disp('x is equal to +1');case-1, disp('x is equal to -1');otherwisedisp('test failed');end(2) result>> test(10)test failed>> test(0)x is equal to zero>> test(j)x is equal to -112. Polynomials:- Create a script m-file to generate the 5th degree polynomial:104322345+++++x x x x x and find all the roots. Use the plot function to plot the polynomial between x=-2 and x=2. Give the plot a title ‘A 5th degree polynomial for different domains’.Answer:(1)home_work_1_B_12.mclear all;close all;x=-2:2;y=x.^5+2*x.^4+3*x.^3+4*x.^2+x+10plot(x,y,'m','LineWidth',2)axis([-2 2 -2 120])p=[1 2 3 4 1 10]roots(p)(2)result:home_work_1_B_12y =0 11 10 21 116p =1 2 3 4 1 10-2.0000-0.6566 + 1.6409i-0.6566 - 1.6409i0.6566 + 1.0815i0.6566 - 1.0815i(3)home_work_1_B_12.fig。
MATLAB作业3参考答案
MA TLAB 作业三参考答案1、 请将下面给出的矩阵A 和B 输入到MA TLAB 环境中,并将它们转换成符号矩阵。
若某一矩阵为数值矩阵,另以矩阵为符号矩阵,两矩阵相乘是符号矩阵还是数值矩阵。
576516535501232310014325462564206441211346,39636623515212107600774101201724407734737812486721711076815A B ⎡⎤⎡⎤⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥==⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥---⎢⎥⎢⎥⎢⎥⎢⎥--⎣⎦⎣⎦【求解】矩阵的输入与转换是很直接的。
>> A=[5,7,6,5,1,6,5; 2,3,1,0,0,1,4; 6,4,2,0,6,4,4; 3,9,6,3,6,6,2; 10,7,6,0,0,7,7; 7,2,4,4,0,7,7; 4,8,6,7,2,1,7]; A=sym(A) A =[ 5, 7, 6, 5, 1, 6, 5] [ 2, 3, 1, 0, 0, 1, 4] [ 6, 4, 2, 0, 6, 4, 4] [ 3, 9, 6, 3, 6, 6, 2] [ 10, 7, 6, 0, 0, 7, 7] [ 7, 2, 4, 4, 0, 7, 7] [ 4, 8, 6, 7, 2, 1, 7]>> B=[3,5,5,0,1,2,3; 3,2,5,4,6,2,5; 1,2,1,1,3,4,6; 3,5,1,5,2,1,2; 4,1,0,1,2,0,1; -3,-4,-7,3,7,8,12; 1,-10,7,-6,8,1,5]; B=sym(B) B =[ 3, 5, 5, 0, 1, 2, 3] [ 3, 2, 5, 4, 6, 2, 5] [ 1, 2, 1, 1, 3, 4, 6] [ 3, 5, 1, 5, 2, 1, 2] [ 4, 1, 0, 1, 2, 0, 1][ -3, -4, -7, 3, 7, 8, 12] [ 1, -10, 7, -6, 8, 1, 5]2、 利用MA TLAB 语言提供的现成函数对习题1中给出的两个矩阵进行分析,判定它们是否为奇异矩阵,得出矩阵的秩、行列式、迹和逆矩阵,检验得出的逆矩阵是否正确。
东南大学-数值分析上机题作业-MATLAB版
end
S=zeros(1,n-1);
fori=1:n-1
x=xn(i)+0.5;
S(i)=yn(i)+(f1(i)-(M(i)/3+M(i+1)/6)*h(i))*(x-xn(i))+M(i)/2*(x-xn(i))^2+(M(i+1)-M(i))/(6*h(i))*(x-xn(i))^3;
index=index+i-1;
T=C(index,:);
C(index,:)=C(i,:);
C(i,:)=T;
fork=i+1:n
ifC(k,i)~=0
C(k,:)=C(k,:)-C(k,i)/C(i,i)*C(i,:);
end
end
end
%%回代求解
x=zeros(n,1);
x(n)=C(n,n+1)/C(n,n);
可见,在 内取初值,Newton序列收敛,且收敛于根
3.Chapter 3
3.1题目
对于某电路的分析,归结为求解线性方程组RI=V,其中
(1)编制解n阶线性方程组 的列主元高斯消去法的通用程序;
(2)用所编程序线性方程组 ,并打印出解向量,保留5位有效数字;
(3)本题编程之中,你提高了哪些编程能力?
东南大学-数值分析上机题作业-MATLAB版
D
1.3运行结果
1.4结果分析
按从大到小的顺序,有效位数分别为:6,4,3。
按从小到大的顺序,有效位数分别为:5,6,6。
可以看出,不同的算法造成的误差限是不同的,好的算法可以让结果更加精确。当采用从大到小的顺序累加的算法时,误差限随着N的增大而增大,可见在累加的过程中,误差在放大,造成结果的误差较大。因此,采取从小到大的顺序累加得到的结果更加精确。
信号与系统matlab 实验 东大短学期
信号与系统MATLAB 实践作业一Ps:所有源代码均在附加.m文件中,实验一二没有粘程序,实验三粘了程序。
实验一:熟悉MATLAB基本操作三:基本序列运算1.数组的加减乘除和乘方运算2.粗略描绘各函数波形(1)(2)(3):(4):(5):(6):(7)四:利用MATLAB求解线性方程组1.x =1.00002.00003.00002.x =3.00001.0000-2.00003.x =121518实验二:信号的运算二,编写离散信号运算函数,并画出波形2.4.七,编写两个离散序列的卷积和的程序,并用其计算卷积和1,ans =1.0000 1.5000 1.7500 1.8750 0.9375 0.4375 0.1875 0.0625 2,ans =3 5 6 6 3 1以上为用conv函数得出。
下面为编写程序做法:1.%new11.ma=[1 1 1 1];b=[1 0.5 0.25 0.125 0.0625];for k=1:7c(k)=fun11(a,b,k,4,5);endc%fun11.mfunction y=fun11(a,b,k,max1,max2)y=0;for i=1:100if (k-i<1) ||(i<1)||(i>max1)||(k-i>max2)break;elsey=y+a(i)*b(k-i)endend(.m文件也在附件中)结果:1.y =1y =0.5000y =1.5000y =0.2500 y =0.7500 y =1.7500 y =0.1250 y =0.3750 y =0.8750 y =1.8750 y =0.0625 y =0.1875y =0.4375y =0.9375同理,第二个程序为%new12.ma=[1 1 1 1];b=[3 2 1];for k=1:10c(k)=fun12(a,b,k,4,3);endc%fun12.mfunction y=fun12(a,b,k,max1,max2)y=0;for i=1:100if (k-i<1) ||(i<1)||(i>max1)||(k-i>max2) break;elsey=y+a(i)*b(k-i)endend结果:y =3y =2y =5y =1y =3y =6实验三:信号的谱分析二:1,clc;clear all;x=[1,2,3,4,5,6,6,5,4,3,2,1];dtft=zeros(70);for i=1:70w(i)=(i-35)/10;for k=1:12dtft(i)=dtft(i)+x(k)*exp(-j*(k-1)*w(i));endendsubplot(1,2,1);plot(w,abs(dtft));xlabel('w');ylabel('DTFT');title('幅频特性'); subplot(1,2,2);plot(w,angle(dtft));xlabel('w');ylabel('DTFT');title('相频特性');2.clc;clear all;for i=1:120if i==10x(i)=1;elseif i==20x(i)=2;elseif i==30x(i)=3;elseif i==40x(i)=4;elseif i==50x(i)=5;elseif i==60x(i)=6;elseif i==70x(i)=6;elseif i==80x(i)=5;elseif i==90x(i)=4;elseif i==100x(i)=3;elseif i==110x(i)=2;elseif i==120x(i)=1;else x(i)=0;endend% x=[1,2,3,4,5,6,6,5,4,3,2,1];%dtft=zeros(150);for i=1:120w(i)=(i-40)/10;endG=fft(x);subplot(1,2,1);plot(w(1:80),abs(G(1:80)));xlabel('w');ylabel('DFT');title('幅频特性'); subplot(1,2,2);plot(w(1:80),angle(G(1:80)));xlabel('w');ylabel('DFT');title('相频特性');3.%a31clc;clear all;x=[1,2,3,4,5,6,6,5,4,3,2,1];dtft=zeros(70);for i=1:70w(i)=(i-35)/10;for k=1:12dtft(i)=dtft(i)+x(k)*exp(-j*(k-1)*w(i));endendfigure;plot(w,abs(dtft));xlabel('w');ylabel('DTFT');title('幅频特性'); hold on%a32clc;clear all;for i=1:120if i==10x(i)=1;elseif i==20x(i)=2;elseif i==30x(i)=3;elseif i==40x(i)=4;elseif i==50x(i)=5;elseif i==60x(i)=6;elseif i==70x(i)=6;elseif i==80x(i)=5;elseif i==90x(i)=4;elseif i==100x(i)=3;elseif i==110x(i)=2;elseif i==120x(i)=1;else x(i)=0;endend% x=[1,2,3,4,5,6,6,5,4,3,2,1];%dtft=zeros(150);for i=1:120w(i)=(i-40)/10;endG=fft(x);hold on;plot(w(1:80),abs(G(1:80)));xlabel('w');ylabel('DFT');title('幅频特性');得到图如下:可见DFT是其DTFT 的取样(有点偏离是因为两种计算方法的差异),但从周期性延拓上可看出是正确的。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Answer:
N=1024;
dt=1/N;
t=0:dt:1-dt;
x=sin(2*pi*32.*t) + sin(2*pi*137.*t) + sin(2*pi*467.*t);
X=fft(x);
subplot(211);plot(t,x);
xlabel('t/sec');ylabel('x');
.
Answer:
omega=1:pi/100:pi;
z=exp(j .* omega);
H = z ./ (z.^2 + 1.6 * z + 0.9);
subplot(211);
plot(omega, abs(H));
xlabel('\Omega'),ylabel('|H|');
subplot(212);
vi)Connect two oscilloscopes to the circuit, one to the transfer function output and the other to the output of the integrator.
iv)Connect the output of this summer to a squarefunction. This is obtained by usingf(u)in the nonlinear blocks.
v)Connect the output of this squarer to an integrator.
Repeat the experiment but with an initial condition of –2 on the integrator. Again draw the results.
1.2 FIRST ORDER SYSTEM
A single time constant may be simulated using the transfer function block in which you enter the coefficients of the numerator and denominator polynomials. Set up the configuration in a new SIMULINK window to realise the transfer function 1/(s+2) with the input unit step and an oscilloscope connected to the output of the transfer function block.Plotthe block diagram in the space below. Simulate the system for 5 seconds andplotthe response.
i)A unit step input.
ii)Connect the unit step to a transfer function of1/(s2+2ζs+1)withζ= 1.0.
iii)Take a summing block and connect the input step to a +ve input and the transfer function output to a –ve input.
nx=[-2:6];
nh=[0:7];
Plot the functions and convolution results.
Answer:
x=[3, 11, 7, 0, -1, 7, -5, 0, 2];
h=[11, 9, 0, -7, -3, 2, 0 -1];
nx=[-2:6];
nh=[0:7];
MatlabWorksheet3
Part A
ing function conv_m.m to make convolution between the following to functions (x and h):
x=[3, 11, 7, 0, -1, 7, -5, 0, 2];
h=[11, 9, 0, -7, -3, 2, 0 -1];
ylabel('x[n] Hamming win');
X=fft(x1);
df=1;
f=0:df:N-1;
subplot(3,2,4);
plot(f,20*log10(abs(X)));
xlabel('k (Hz)');
ylabel('|X[k]| (db)');
Part B
Simulation Using SIMULINK
.
The hamming window can be codedin Matlabas
for n=1:N
hamming(n)=0.54+0.46*cos((2*n-N+1)*pi/N);
end;
where N is the data length in the FFT.
Answer:
N=1024;
dt=1/N;
1.1 UNDERSTANDING SIMPLE WAVEFORMS AND INTEGRATION
Create a pulse of height 2 units from time 0 to 4 seconds by subtracting two unit steps and adding a gain. Connect this pulse to an integrator with a gain of 0.5 and a zero initial condition. Connect oscilloscopes to show the pulse and the output of the integrator. You may wish to name your simulation (block) diagram; to do so use thesaveasfeature under edit. Your block diagram should be similar as below.
axis([ 0 N/2 0 1.5*max(20*log10(abs(X)))]);
%hamming window
for n=1:N
w=0.54+0.46*cos((2*n-N+1)*pi/N);
end;
x1=x .*w;
subplot(3,2,2);
plot(t,x1);
xlabel('t');
Before simulating you need to pull down the simulation header and double click on parameters. Unless told otherwise always assume that you can use the ode 45 integration algorithm shown in this window and the other default parameters. Typically you will only alter the start and stop times but for this first simulation you can use the default values of 0 and 10 seconds. Double click on the oscilloscopes to get the windows in which the traces will appear, pull down the simulate menu and click onrun.Plotbelow the integrator input and output waveforms.
plot(omega, atan2(imag(H),real(H)));
xlabel('\Omega');ylabel('Phase of H')
efftto analyse following signal by plotting the original signal and its spectrum.
f=0:N-1;
subplot(323);stem(f,abs(X));
xlabel('f/Hz');ylabel('|X|');
axis([ 0 N/2 0 1.5*max(abs(X))]);
subplot(325);stem(f,20*log10(abs(X)));
xlabel('f/Hz');ylabel('20lg|X|');
axis([-6 10 -8 12]);
subplot(3,1,3);
stem(ny,y);
xlabel('n'); ylabel('y[n]');
axis([-6 10 -150 250]);
2.Plot the frequency response over for the following transfer function by letting , where is the frequency (rad/sample)., with appropriate labels and title.
t=0:dt:1-dt;
x=sin(2*pi*32.5.*t) + sin(2*pi*137.4.*t) + sin(2*pi*467.7.*t);
X=fft(x);
subplot(321);plot(t,x);