MATLAB语言基础与应用(第二版)第5章 习题答案
- 格式:docx
- 大小:180.99 KB
- 文档页数:12
Matlab编程与工程应用〔第二版习题解答第一章:MATLAB简介1.>> myage = 25;>> myage = myage - 1;>> myage = myage + 2;2.>> myage = 25;>> myage = myage - 1;>> myage = myage + 2;3.>> namelengthmaxans =634.>> format bank>> 1/3ans =0.335.>> format rat>> 5/6 + 2/7ans =47/426.25 19 4.3333 9 97.>> pounds = 100;>> kilos = 2.2 * poundskilos =220.00008.>> R1 = 20; R2 = 30; R3 = 40;>> RT = <R1\1 + R2\1 + R3\1>\1RT =9.23089.>> ftemp = 90;>> ctemp = <ftemp - 32> * 5 / 9ctemp =32.222210. 略11.>> sind<90>ans =112.>> sita = 2 * pi / 3;>> r = 2;>> x = r * cos<sita>x =-1.0000>> y = r * sin<sita>y =1.732113.>> t = 100;V = 50;>> wcf = 35.7 + 0.6 * t - 35.7 + 0.16 * V + 0.43 * t * 0.16 * Vwcf =41214.Fix<3.5> = 3; Floor<3.5> = 3;Fix<3.4> = 3; Fix<-3.4> = -3;Fix<3.2> = 3; Floor<3.2> = 3;Fix<-3.2> = -3; Floor<-3.2> = -4;Fix<-3.2> = -3; Ceil<-3.2> = -3;15.Sqrt<19>3 ^ 1.2Tan<pi>16.Intmin<‘int32’>; intmax<‘int32’>; intmin<‘int64’>;intmax<‘int64’>;17.Realmin<‘double’>; Realmax<‘double’>;18.>> DblNum = 33.8;>> int32Num = int32<DblNum>int32Num =3419.>> A1 = rand;A2 = 20*rand;A3 = 30 * rand + 20;A4 = round<rand * 10>;A5 = round<rand * 11>; /A6 = round<rand * 50> + 50;>> A4 = randi<10>;>> A5 = randi<11>;>> A6 = randi<50> + 50;20. 略21.Double〔‘A’ < Double<‘a’>; 所以大写在前,小写在后;22.>> CharNum = 'xyz';>> CharNum = char<CharNum - 2>CharNum =vwx23.>> vec = [3 : 1 : 6]vec =3 4 5 6>> vec2 = [1.0000:0.5000:3.0000]vec2 =1.0000 1.50002.0000 2.50003.0000>> vec3 = [5 : -1 :2]vec3 =5 4 3 224.>> vec1 = linspace<4,8,3>vec1 =4 6 8>> vec2 = linspace<-3,-15,5>vec2 =-3 -6 -9 -12 -15>> vec3 = linspace<9,5,3>vec3 =9 7 525.>> vec = [1 : 1 : 10]vec =1 2 3 4 5 6 7 8 9 10 >> vec = linspace<1,10,10>vec =1 2 3 4 5 6 7 8 9 10 >> vec2 = [2:5:12]vec2 =2 7 12>> vec = linspace<2,12,3>vec =2 7 12>> myend = randi<4> + 8;>> vec1 = [1 : 3 : myend]vec1 =1 4 7 1027.>> vec = [-1:0.2:1]'vec =-1.0000-0.8000-0.6000-0.4000-0.20000.20000.40000.60000.80001.000028.>> vec = [0 : 1 : 15];>> n = [1 : 2 : numel<vec> - 1];>> vec2 = vec<n>vec2 =0 2 4 6 8 10 12 14 29.function [a b] = PI_CONT<x>%UNTITLED Summary of this function goes here% Detailed explanation goes heren =1 : 1 : fix<numel<x> / 2>;a = x<n>;n = 1+fix<numel<x>/2> : 1 : numel<x>;b = x<n>;endrem<numel<vec>,2>30.rand<2,3>10*rand<2,3>5 + round<15 * rand<2,3>>randint<2,3,[5 15]>31.rows = randi<5>;cols = randi<5>;y = zeros<rows,cols>;>> mat = [ 7 8 9 1012 10 8 6];>> A1 = mat<1,3>>> A2 = mat<2,:>>> A3 = mat<:,1:2>33.>> mymat = [2 3 4;5 6 7]>> mymat1 = fliplr<mymat>>> mymat2 = flipud<mymat>>> mymat3 = rot90<mymat>34.>> mymatzero = zeros<4,2>>> mymatzero<2,:> = [3,6]35.>> x = linspace<-pi,pi,20>;>> y = sin<x>;36.>> randmat = randint<3,5,[-5 5]> >> sign<randmat>37.>> randmat = randint<4,6,[-5 5]>;>> randmat2 = abs<randmat> 38.>> randmat = rand<3,5>>> randmat<3,:> = []39.>> vec = 1 : 1:1000;>> vec<end>>> vec<numel<vec>>>> [a b ] = size<vec>;>> vec<a * b>40.同上41.>> mat = zeros<3,5>;>> mat<:,:,1> = zeros<3,5>;>> mat<:,:,2> = zeros<3,5>; 42.>> myc = clock>> today = myc<1:3>>> now = myc<4:6>>> now = fix<now>第二章 MATLAB程序设计概述1.ri = 2; % Radius internalro = 4; % Radius outerV = 4 * pi / 3 * <ro ^ 3 - ri ^ 3> % calculate volume2.% Calculate Atomic Weight H2O2Weight_O = 15.9994;Weight_H = 1.0079;AtomicWeight = Weight_O * 2 + Weight_H * 2 % Calculate H2O2 Atomic Weight 3.fprintf<'Length of The character string is : %d\n',…length<input<'Please input character string :\n','s'>>>;4.NewNumber = input<'please input a Number :\n'>;fprintf<'Number with 2 decimal is :%0.2f\n',NewNumber>;5.>> vec = input<'Enter a matrix: '>Enter a matrix: [1:1:10;1:1:10]6.>> fprintf<'OUTPUT : %f\n',12345.6789>OUTPUT : 12345.678900>> fprintf<'OUTPUT : %10.4f\n',12345.6789>OUTPUT : 12345.6789>> fprintf<'OUTPUT : %10.2f\n',12345.6789>OUTPUT : 12345.68>> fprintf<'OUTPUT : %6.4f\n',12345.6789>OUTPUT : 12345.6789>> fprintf<'OUTPUT : %2.4f\n',12345.6789>OUTPUT : 12345.67897.>> fprintf<'OUTPUT : %d\n',12345>OUTPUT : 12345>> fprintf<'OUTPUT : %5d\n',12345>OUTPUT : 12345>> fprintf<'OUTPUT : %8d\n',12345>OUTPUT : 12345>> fprintf<'OUTPUT : %3d\n',12345>OUTPUT : 123458.>> x = 12.34;>> y = 4.56;>> fprintf<'x is %.3f\n',x>x is 12.340>> fprintf<'x is %0.0f\n',x>x is 12>> fprintf<'y is %0.1f\n',y>y is 4.6>> fprintf<'y is %0.1f !\n',y>y is 4.6 !9.% Calculate Area of the squarenessfprintf<'The Area of The Squareness is %.2f',…<input<'Please input the Length of the Squareness \n'>> …* <input<'Please input the Width of the Squareness \n'>>>;10.NewName = input<'What is your name? ','s'>;fprintf<'Wow,your name is %s!\n',NewName>;11.NewString = input<'Enter your string : ','s'>;fprintf<'Your String was : ''%s''\n',NewString>;12.WaterSpeed = input<'Enter the folw in m^3/s : '>;WaterSpeedft = 0.028 \ WaterSpeed;fprintf<'A flow rate of %.3f meters per sec \n',WaterSpeed>;fprintf<'is equivalent to %.3f feet per sec\n',WaterSpeedft>;13.IncomeY = input<'Input your income every year?'>;FoodCostY= [IncomeY * 8 / 100 IncomeY*10 / 100];FoodCostM = 12\FoodCostY;fprintf<'Food Cost in every year is %.2f - %.2f\n',FoodCostY<1>,FoodCostY<2>>; fprintf<'Food Cost in every Month is %.2f - %.2f\n',FoodCostM<1>,FoodCostM<2>>;14.Weight = input<'Please input the Weight of the plan \n'>Area = input<'Please input the area of the plan wing \n'>fprintf<'Charge of Plan Win is %.2f \n',Weight / Area>15.x = 10;y = 10;plot<x,y,'g+'>;16.clfx = -2 : 0.1 : 2;y = exp<x>;plot<x,y,'g'>;xlabel<'X'>;ylabel<'Y'>;legend<'Exp<x>'>;Title<'Exp Example'>;17.clfx = 1 : 5 : 100;y = sqrt<x>;plot<x,y,'go'>;hold on;bar<x,y>;18.clfy = randint<1,100,[0 100]>;x = 1 : 1 : length<y>;plot<x,y,'ro'>;hold onplot<x,y,'g+'>;figure<2>;plot<x,y,'k'>;figure<3>;bar<x,y>19.clfx = linspace<0,pi,10>;y = sin<x>;plot<x,y,'r'>;figure<2>;x = linspace<0,pi,100>;y = sin<x>;plot<x,y,'b'>;20.load TimeTemp.datx = TimeTemp<:,1>';y = TimeTemp<:,2>';plot<x,y>;Xlabel<'Height'>;Ylabel<'Temperature'>;Title<'Height - Temperature'>;legend<'TEMP'>21.>> randint<3,6,[50 100]>;>> save RandInt.bat ans -ascii;>> randint<2,6,[50 100]>>> save RandInt.bat randint -ascii –append >> load RandInt.bat>> RandInt22.>> load testtan.bat>> tan<testtan>23.>> load hightemp.dat>> hightemp<:,1> = hightemp<:,1> .+ 1900>> save y2ktemp.dat hightemp –ascii24.function [ fn ] = fn< x >%Calculate y as a function of xfn = x^3 - 4 * x^2 + sin<x>;end25.function [ mwh] = mwh_to_gj<x>%convers from MWh to GJmwh = 3.6 * x;end26.function [ output_args ] = miletometer< x >%Conver mile to meterMile = input<'mile per hours number :'>;meter = Mile * 5280 * 0.3048 / 3600;output_args = meter;fprintf<'Meter of the mile is : % .2f',meter>; end27.function [ Tn ] = Interest< p,i,n >%UNTITLED4 Summary of this function goes hereTn = p * <1 + i> * n;end28.略29.function [ V ] = SpeedConv<pi,ps>%UNTITLED4 Summary of this function goes hereV = 1.016 * sqrt<pi - ps>end30. function [ THR ] =Cal_rate<A>%UNTITLED4 Summary of this function goes hereTHR = <220 - A> * 0.6;end31.function [ nJ ] =STL_calc<n>%UNTITLED4 Summary of this function goes herenJ = sqrt<2 * pi * n> * <n / exp<1>>^n;end32.function [ output_args ] = costn< n >%UNTITLED5 Summary of this function goes hereoutput_args = 5 * n ^ 2 - 44 * n + 11;endn = input<'Enter the number of units :'>;Cn = costn<n>;fprintf<'The cost for 100 units will be $%.2f\n',Cn>;33.略〔仿造32题34.function [ V ] = oblong< length >%UNTITLED8 Summary of this function goes hereV = 1/12*sqrt<2>*length^3;endfprintf<'The volume of oblong is : %.2f\n ',oblong<input<'Enter the length of the oblong:\n'>>>35.function [ y ] = pickone< x >%pickone<x> returns a random element from vector xn = length<x>;y = x<randi<n>>;end36.function [ y ] = vecout< x >%UNTITLED11 Summary of this function goes herey = x : 1 : x+5;end37.b = input<'Enter the first side :'>;c = input<'Enter the second side :'>afa = input<'Enter the angle between them :'>y = sqrt<b^2 + c^2 - 2*b*c*cos<afa *<pi/180>>>;fprintf<'The third side is %.3f\n',y>;38.略39.load floatnums.datfloatnums = round<floatnums>;floatnums = floatnums';save intnums.datfloatnums-ascii;40.load costssales.datn = length<costssales<:,1>>;fprintf<'There were %d quarters in the file ',n>;n = 1 : 1 : n;costs = costssales<:,1>;sales = costssales<:,1>;plot<n,costs,'ko'>;hold onplot<n,sales,'b*'>;legend<'COSTS','SALES'>;xlabel<'COST'>;ylabel<'SALE'>;title<'COST-SALE'>;costssales = costssales'costssales = flipud<costssales>;save newfile.datcostssales-ascii;第三章选择语句2. if <input<'please Enter a x :\n','s'> ~= 'x'>disp<'sorry the char not x'>;end3. function y = Test3<x>if <x == 12>y = 1;elsey = x + 1;end;4. disp<'The program will calculate the volume of a pyramid'>;a = input<'Enter the length of the base :'>;c = input<'Is that i or c?','s'>;if< c == 'c'>a = a/2.54;end;b = input<'Enter the width of the base :','s'>;c = input<'Is that i or c?'>;if< c == 'c'>b = b/2.54;end;d = input<'Enter the length of the base :','s'>;c = input<'Is that i or c?'>;if< c == 'c'>d = d/2.54;end;fprintf<'The Volume of the Pyramid is %.3f cubic inches.',a * b * d / 3>;5.if<input<'Are you an engineer?<Y/N>','s'> == 'Y'>disp<'You are the best!'>;elsedisp<'oh~ soga'>;end;6.Fenzi = input<'Please input enter the FENZI: '>;Fenmu = input<'Please input enter the FENMU: '>;if<Fenmu == 0>disp<'Sorry The Fenmu can not be zero!'>;elsefprintf<'The result is %.2f.\n',Fenzi/Fenmu>;end;7.function Result = Test3_Func< a,b >%TEST3_FUNC Summary of this function goes here% Detailed explanation goes hereResult = sqrt<1 - b^2/a>;EndFunction:a = input<'Please Enter the a : '>;b = input<'Please Enter the b : '>;if <a == 0>disp<'Error 0001'>;elsefprintf<'The Result is %.2f \n',Test3_Func<a,b>>;end;8.a = input<'Please Enter the Shousuo : '>;b = input<'Please Enter the Shuzhang : '>;if <a<120 && b<80>disp<'you are the best choice !'>;elsedisp<'Sorry you can not do this!'>;end;9.A1 = input<'Enter the First Area: '>;A2 = input<'Enter the Second Area: '>;if<A1 > A2>disp<'UP!'>;elseif <A1 < A2>disp<'Down!'>;elsedisp<'Keep!'>;en10.a = input<'Enter the PH number : '>;if <a < 7>disp<'Suan'>;elseif <a > 7>disp<'Jian'>;elsedisp<'Zhong'>;end;11.function Result = Test3_Func< a,b >%TEST3_FUNC Summary of this function goes here % Detailed explanation goes hereif <a < b>Result = a : 1 : b;elseResult = a : -1 : b;end12.function Result = Test3_Func< x >%TEST3_FUNC Summary of this function goes here % Detailed explanation goes here[a b] = size<x>;if <<a ~= 1 && b ~= 1> || <a == 1 && b == 1>> Result = x;elseif<a == 1>Result = fliplr<x>;elseResult = flipud<x>;end13.letter = input<'Enter your answer :','s'>;if <letter == 'Y' || letter == 'y'>disp<'OK,continuing'>;elseif<letter == 'N' || letter == 'n'>disp<'OK,halting'>;elsedisp<'Error'>;end14.switch lettercase {'Y''y'}disp<'OK,contining'>case {'N''n'}disp<'OK,halting'>otherwisedisp<'Error'>end15.Mahe = input<'Enter the Speed of Plan : '>/input<'Enter the Speed of Voice: '>; if<Mahe > 1>disp<'supersonic speed'>;elseif<Mahe < 1>disp<'subsonic speed'>;elsedisp<'transonic speed'>;end;16.X = input<'Enter the temp in degrees C : \n'>;switch <input<'Do you want F or K?','s'>>case'F'fprintf<'The temp in degrees F is %.1f',<9/5>*X + 32>;case'K'fprintf<'The temp in degrees K is %.1f',X + 273.15>;otherwisedisp<'Enter Error!'>;end;17.%function y = Test3<x>X = randi<100>;if rem<X,2> == 0fprintf<'%d ÊÇżÊý\n',X>;elsefprintf<'%d ÊÇÆæÊý\n',X>;end18.function y = Test3<x>switch rem<x,4>case 0y = true;otherwisey =false;end;19.function y = Test3<innum>if innum == int32<innum>y = true;elsey =false;end;20.function y = Test3<a,b,c>if a^2 == b^2 + c^2y = true;elsey =false;end;21.Re = input<'Enter the Re Value :\n'>;if Re <= 2300 disp<'Laminar region'>;elseif Re > 2300 && Re <= 4000 disp<'Transition region'>; else disp<'Turbulent region'>;end;22.d1 = input<'Enter d1 : \n'>;d2 = input<'Enter d2 : \n'>;if d1 <= 0 || d2 <= 0disp<'Error!'>;elsefprintf<'%.2f',Test3_Func<d1,d2>>;end;function y = Test3_Func<a,b>y = a * b / 2;23.V = input<'Enter the speed of the wing: '>;if V <= 38 disp<'tropical depression '>;elseif V >= 73 disp<'typhoon'>;else disp<'revolving storm'>;end;24.V = input<'Enter the speed of the wing :'>;if V >74 && V <= 95disp<'1 : 4-5'>;elseif V > 95 && V <= 110disp<'2 : 6-8'>;elseif V > 110 && V <= 130disp<'3 : 9-12'>;elseif V > 130 && V <= 155disp<'4 : 13-18'>;elseif V > 155disp<'5 : 18'>;elsedisp<'Error'>;end25.略26.H = input<'Enter the hight of the cloude :'>;if H < 6500 && H > 0disp<'Low'>;elseif H >= 6500 && H < 20000disp<'Middle'>;elsedisp<'High'>;27.if letter == 'x'disp<'Hello'>;elseif letter == 'y'||letter == 'Y'disp<'Yes'>;elseif letter == 'Q'disp<'Quit'>;elsedisp<'Error'>;end;28.switch numcase {0 1 2}f2<num>;case {-2 -1}f3<num>;case 3f4<num>;otherwisef1<num>;end;29.switch menu<'menu','1.Cylinder','2.Circle','3.Rectangle'>case 1R = input<'Please enter the radius of buttom circle:'>;H = input<'Enter the height of the Cylinder:'>;A = 2 * pi * R * H + 2 * pi * <R ^ 2>;fprintf<'The area is %.2f',A>;case 2R = input<'Please enter the radius of circle:'>;A = pi * <R ^ 2>;fprintf<'The area is %.2f',A>;case 3a = input<'Please enter the long of Rectangle:'>;b = input<'Please enter the with of Rectangle :'>;A = a * b;fprintf<'The area is %.2f',A>;end30.同2931.同2932.x = input<'Enter the Value X :'>;Y = menu<'Menue Choose','Sin','Cos','Tan'>;switch Ycase 1output = sin<x>;case 2output = cos<x>;case 3output = tan<x>;end;33.略34.function output = Test3_Func<x>Y = menu<'Menue Choose','ceil','round','sign'>;switch Ycase 1output = ceil<x>;case 2output = round<x>;case 3output = sign<x>;end;35.略36.略37.略第四章循环1.>> for i = 1.5:0.2:3.1i = iend2.function output = Test3_Func<x>x = x - <rem<x,2> == 0>;output = 0;for i = 1 : 2 : xoutput = output + i;end;3.function output = Test3_Func<x>x = x - <rem<x,2> == 0>;output = 1;for i = 1 : 2 : xoutput = output * i;end;4.5.>> for i = 32 : 1 : 255char<i>end;6.>> vec = [5.5 11 3.45];>> for i = 1 : length<vec>fprintf<'Element %d is %.2f .\n',i,vec<i>>;end;7.n = randi<4> + 1;sum = 0;for i = 1 : nsum = sum + input<'Enter the value'>;end;fprintf<'Sum = %.1f\n',sum>;8disp<'Please enter the threshold below which samples will be considered to be invalid :'>;Value1 = input<'Considered to be invalid :\n'>;Value2 = input<'Please enter the number of data samples to be entered :\n'>; Sum = 0;num = 0;for i = 1:Value2data = input<'Please enter a data sample :'>;if data > Value1Sum = Sum + data;num = num + 1;endendif num == 0disp<'Data Error !'>;elsefprintf<'The average of the %d valid data samples is %.2f volts.',num,Sum/num>; end9.load MaxTemp.mat[a b] = size<MaxTemp>;for i = 1 : a;Matix1 = zeros<size<MaxTemp>>;Matix1<i,:> = MaxTemp<i,:>end10.sum = 0;for i = 1 : length<pipe>sum = sum + <pipe<i,1> < 2.1 || pipe<i,1> > 2.3>;sum = sum + <pipe<i,2> < 10.3 || pipe<i,2> > 10.4>;endfprintf<'There were %d rejects.\n',sum>;11.略12.略13.x = randint<1,5,[-10,10]>;x2 = 0;for i = 1 : length<x>x1<1,i> = x<1,i> - 3;if x1<1,i> > 0x2 = x2 + 1;end;x3<1,i> = abs<x1<1,i>>;if i > 1x4 = max<x1<1,i>,x1<1,i-1>>;end;end16.17.略18.19.20.略22.略23.略24.10略略第五章向量化代码1.Mat.*2;2.Result = vec;3.cumprod<vec>;4.>> vec = randint<1,6,[0 20]>;>> maxvec = max<vec>;>> minvec = min<vec>;>> cumsumvec = cumsum<vec>5.Sum<vec>6.>> clear>> vec = randint<1,5,[-10 10]>;>> vec_3 = vec - 3;>> ZhengshuNum = length<find<vec > 0>>;>> vec_abs = abs<vec>;>> vec_max = max<vec>;7.max<vec,[],1>max<vec,[],2>max<max<vec>>;8.function output = Test3_Func<r,n>vec = [];for i = 0 : nvec = [vec r.^i];end;output = sum<vec>;>> vec1 = Vec<[1 3 5]>;>> vec2 = Vec<[2 4 6]>;>> CheckSum = vec1.*vec2;11.>> vec = 1 : 1 : 1000;>> Result = sum<1./<vec.^2>>;>> Check = pi.^2/6;>> diff = check – result13.略14.略15.clear;clf;vec_temp = 0 : 0.5 : 10;x1 = input<'Enter the number of level :'>;while<all<vec_temp - x1>>x1 = input<'Enter the number of level :'>;end;disp<x1>16.略17.ticif x==21disp<x>end;tocticswitch xcase 21disp <x>;end;toc18.Ones<3>19.If find<r > 0> == [] && fin<h > 0> == 0V = pi .*<r.^2>*hEnd;第六章 MATLAB 程序1~20 略21.function S = Test3_Func<n>P = 10000;i = 0.05;vec = 1 : 1 : n;S = P * <<1 + i>.^vec>;plot<vec,S>;n = input<'Enter the Year:'>;while ~<n >0 && int32<n> == n>n = input<'Enter the Year:'>;end;Test3_Func<n>;22.Function lenf = lenprompt<>;Lenf = input<‘Enter the length By inches: ’>; Function Leni = covert_f_to_i<lenf>Leni = …<英尺转英寸>Function printlens<lenf,leni>Disp<lenf>;Disp<leni>;23.略24.。
第5章习题与答案5.1用矩阵三角分解方法解方程组123123123214453186920x x x x x x x x x +-=⎧⎪-+=⎨⎪+-=⎩ 解答:>>A=[2 1 -1;4 -1 3;6 9 -1] A =2 1 -1 4 -13 6 9 -1 >>b=[14 18 20]; b =14 18 20 >> [L, U, P]=lu(A) L =1.0000 0 0 0.6667 1.0000 0 0.3333 0.2857 1.0000 U =6.0000 9.0000 -1.0000 0 -7.0000 3.6667 0 0 -1.7143 P =0 0 1 0 1 0 1 0 0 >> y=backsub(L,P*b’) y =20.0000 4.6667 6.0000 >> x=backsub(U,y) x =6.5000 -2.5000 -3.5000 5.2 Cholesky 分解方法解方程组123121332352233127x x x x x x x ++=⎧⎪+=⎨⎪+=⎩ 解答:>> A=[3 2 3;2 2 0;3 0 12] A =3 2 32 2 03 0 12>> b=[5;3;7]b =537>> L=chol(A)L =1.7321 1.1547 1.73210 0.8165 -2.44950 0 1.7321>> y=backsub(L,b)y =-11.6871 15.7986 4.0415>> x=backsub(L',y)x =-6.7475 28.8917 49.93995.3解答:观察数据点图形>> x=0:0.5:2.5x =0 0.5000 1.0000 1.5000 2.0000 2.5000 >> y=[2.0 1.1 0.9 0.6 0.4 0.3]y =2.0000 1.1000 0.9000 0.6000 0.4000 0.3000 >> plot(x,y)图5.1 离散点分布示意图从图5.1观察数据点分布,用二次曲线拟合。
matlab程序设计与应用第二版习题答案matlab程序设计与应用第二版习题答案【篇一:matlab程序设计与应用(第二版)实验答案】%实验一 matlab运算基础%第1题%(1)z1=2*sin(85*pi/180)/(1+exp(2))%(2)x=[2,1+2i;-0.45,5];z2=0.5*log(x+sqrt(1+x.^2))%(3)a=-3.0:0.1:3.0;z3=(exp(0.3*a)-exp(-0.3*a))/2.*sin(a+0.3)+log((0.3+a)/2)%(4)t=0:0.5:2.5;z4=t.^2.*(t=0t1)+(t.^2-1).*(t=1t2)+(t.^2-2*t+1).*(t=2t3)%第2题a=[12 34 -4;34 7 87;3 65 7];b=[1 3 -1;2 0 3;3 -2 7];a+6*ba-b+eye(size(a))a*ba.*ba^3a.^3a/bb\a[a,b][a([1,3],:);b^2]%第3题a=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16 17 18 19 20;21 22 23 24 25] b=[3 0 16;17 -6 9;0 23 -4;9 7 0;4 13 11]c=a*bf=size(c)d=c(f(1)-2:f(1),f(2)-1:f(2))whos%第4题%(1):a=100:999;b=rem(a,21);c=length(find(b==0))%(2):a=lsdhksdlkklsdkl;k=find(a=aa=z);a(k)=[]%实验二 matlab矩阵分析与处理 %第1题e=eye(3);r=rand(3,2);o=zeros(2,3);s=diag([2,3]);a=[e,r;o,s];a^2b=[e,(r+r*s);o,s^2]%第2题h=hilb(5)p=pascal(5)hh=det(h)hp=det(p)th=cond(h)tp=cond(p)%第3题a=fix(10*rand(5))h=det(a)trace=trace(a)rank=rank(a)norm=norm(a)%第4题a=[-29,6,18;20,5,12;-8,8,5][v,d]=eig(a)%数学意义略%第5题方法一%(1):a=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6]; b=[0.95,0.67,0.52]; x=inv(a)*b%(2):b=[0.95,0.67,0.53];x=inv(a)*b%(3):cond(a)%第5题方法二a=hilb(4)a(:,1)=[]a(4,:)=[]b=[0.95,0.67,0.52];x=inv(a)*bb1=[0.95,0.67,0.53];x1=inv(a)*b1n=cond(b)n1=cond(b1)na=cond(a) %矩阵a为病态矩阵%第6题a=[1,4,9;16,25,36;49,64,81]b=sqrtm(a)c=sqrt(a) %sqrtm函数是以矩阵为单位进行计算,sqrt函数是以矩阵中的元素进行计算%实验三选择程序结构设计%第1题程序一x=[-5.0,-3.0,1.0,2.0,2.5,3.0,5.0];y=[]; %建立存放所有y值的矩阵for x0=xif x00x0~=-3y=[y,x0*x0+x0-6];elseif x0=0x05x0~=2x0~=3y=[y,x0*x0-5*x0+6];elsey=[y,x0*x0-x0-1];endendx%输出所有xy%输出所有y%第1题程序二x=[-5,-3,1,2,2.5,3,5];y=[];for a=1:7if x(a)0x(a)~=-3y=[y,(x(a))^2+x(a)-6];elseif x(a)=0x(a)5x(a)~=2x(a)~=3y=[y,(x(a))^2-5*x(a)+6];elsey=[y,x(a)*x(a)-x(a)-1];endend%第2题程序一x=input(请输入一个百分制成绩:);if x100|x0disp(您输入的成绩不是百分制成绩,请重新输入。
第一章测试1.MATLAB、FORTRAN、MAPLE并称为三大数学软件,以研究和解决各种具体的数学问题和工程问题。
A:对B:错答案:B2.下列属于MATLAB独特优势的是()。
A:强大的科学计算及数据处理能力B:简单易用的程序语言C:友好的工作平台和编程环境D:出色的图形处理功能答案:ABCD3.MATLAB界面有()个窗口。
A:4B:3C:5D:6答案:D4.MATLAB、FORTRAN、Mathematica并称为三大数学软件,以研究和解决各种具体的数学问题和工程问题。
A:错B:对答案:A5.在MATLAB中,可以在命令行窗口中通过帮助命令来查询帮助信息,最常用的帮助命令是()。
A:helpwinB:helpC:whichD:what答案:B第二章测试1.下列可作为MATLAB合法变量名的是()。
A:@hB:xyz_2aC:合计D:123答案:B2.设A=[1,2;3,4],B=[5,6;7,8],则A*B=()。
A:[19,22; 21,32]B:[19,22;43,50]C:[5,12;43,50]D:[5,12;21,32]答案:B3.输⼊字符串时,要⼊()将字符括起来。
A:{ }B:C:[ ]D:” ”答案:B4.使⼊语句t=0:7⼊成的是7个元素的向量。
()A:对B:错答案:B5.下列语句中正确的是()。
A:x==y==3B:y=3,x=yC:x=y=3D:x=y==3答案:ABD第三章测试1.sym函数可以建立一个符号量。
()A:错B:对答案:B2.下列指令可用于对符号运算表达式进行化简的是()。
A:expand(S)B:factor(S)C:collect(S)D:horner(S)答案:ABCD3.MATLAB用于符号常微分方程求解的函数是()。
A:solverB:solveC:dsolverD:dsolve答案:D4.ezplot函数可用于绘制符号函数的三维曲线。
()A:错B:对答案:A5.MATLAB将函数展开为幂级数,所使用的函数是()。
作业一4、写出完成下列操作的命令.(1)将矩阵A第2~5行中第1,3,5列元素赋给矩阵B。
>> M=[0:1:48];〉〉A=reshape(M,7,7)A =0 7 14 21 28 35 421 8 15 22 29 36 432 9 16 23 30 37 443 10 17 24 31 38 454 11 18 25 32 39 465 12 19 26 33 40 476 13 20 27 34 41 48>> B=A(2:5,1:2:5)B =1 15 292 16 303 17 314 18 32(2)删除矩阵A的第七行元素。
〉〉A(7,:)=[]A =0 7 14 21 28 35 421 8 15 22 29 36 432 9 16 23 30 37 443 10 17 24 31 38 454 11 18 25 32 39 465 12 19 26 33 40 47 (3)将矩阵A的每个元素值加30.>〉A=A+30A =30 37 44 51 58 65 7231 38 45 52 59 66 7332 39 46 53 60 67 7433 40 47 54 61 68 7534 41 48 55 62 69 7635 42 49 56 63 70 77(4求矩阵A的大小和维素。
sizeA = size(A)dA = ndims(A)sizeA =6 7dA =2(5)将向量t的0元素用机器零来代替。
〉> t=[1 2 3 4 0 5];t =1 2 3 4 0 5〉〉find(t==0)ans =5〉> t(5)=epst =1.0000 2。
0000 3。
0000 4。
0000 0.0000 5。
0000(6)将含有12个元素的向量x转换成3*4矩阵.〉> x=[0:11]x =0 1 2 3 4 5 6 7 8 9 10 11 〉〉y=reshape(x,3,4)y =0 3 6 91 4 7 102 5 8 11(7)求一个字符串的ASCII。
matlab基础与应用教程课后答案matlab基础与应用教程课后答案【篇一:matlab教程基本应用练习题及解答】txt>要求:将每题的答案(命令行和运行结果、图片或m文件的文件名及具体内容)直接拷贝插入到各题的下方:(1)若为命令行,要求将提示符“”一起拷入,并在右侧用“%”注明命令行的每条命令的作用;(2)若为多个运行结果,拷入后要求解释每个结果具体对应题目中的哪个要求,也在右侧用“%”注明;(3)如果为m 文件,除了将文件名和此文件的具体内容全部拷入外,再将所有原始m文件和本练习题电子版放在一个以“专业班级+本人姓名+学号”命名的文件夹内一起上传。
ans =1 0 00 1 00 0 1ans =0 0 00 0 0ans =1 1 11 1 11 1 11 1 120+40*rand(1,10) %生成10个在区间[20,60]上均匀分布的随机数。
ans =44.617351.677556.872549.528327.050636.228257.418856.6 76236 .410855.7460二.1)计算向量(2,4,6,8)的最大值,最小值,平均值,中值,排序,总和值; max([2,4,6,8])%最大值ans =8min([2,4,6,8])% 最小值ans =2mean([2,4,6,8])% 平均值ans =5median([2,4,6,8])% 中值ans =5sort([2,4,6,8])% 排序ans =2 4 6 8sum([2,4,6,8])% 总和值ans =202)在行向量(2,4,6,8)和(1,3,5,7)之间实施加减乘除及幂运算; a=[2 4 6 8];b=[1 3 5 7];a+b%加ans =3 7 11 15a-b%减ans =a.*b%乘ans =2 12 30 56a./b%除ans =2.0000 1.3333 1.2000 1.1429a.^b%幂ans =2 64 7776 20971523)在向量(1,3,5)与标量2之间实施加减乘除及幂运算。
通信电路仿真上机试题班级:姓名:学号:第一章4.gcd函数用于求两个数的最大公约数。
现拥有help命令查看该函数的用法,然后利用该函数15和35的最大约数。
程序: >> help gcdGCD Greatest common divisor.G = GCD(A,B) is the greatest common divisor of correspondingelements of A and B. The arrays A and B must contain non-negativeintegers and must be the same size (or either can be scalar).GCD(0,0) is 0 by convention; all other GCDs are positive integers.[G,C,D] = GCD(A,B) also returns C and D so that G = A.*C + B.*D.These are useful for solving Diophantine equations and computingHermite transformations.See also LCM.结果:>> g=gcd(15,35)g =56.以致三角形的三边a=9.6,b=13.7,c=19.4,求三角形的面积。
提示:利用海伦公式area=sqrt(s*(s-a)*(s-b)*(s-c))计算,其中s=(a+b+c)/2。
程序:clearclca=9.6,b=13.7,c=19.4s=(a+b+c)/2area=sqrt(s*(s-a)*(s-b)*(s-c))结果:a = 9.6000 b =13.7000 c = 19.4000 s = 21.3500 area = 61.1739第二章5.计算下列分段函数0 x<=5F(x)= (x-3)/4 5<x<=102*x x>10程序:clearclcx=input('请输入一个数,x=');if x<=5y=0;elseif x>5&x<=10y=(x-3)/4;else x>10y=2*x;endy结果:请输入一个数,x=11y =228.已知S=1+2+2^2+2^3+…+2^63,求S 的值。
如对您有帮助,欢迎下载支持,谢谢!第二章3.设矩阵A为A=[24 23 9 21 6;65 74 24 11 21;34 5 98 75 21;8 42 42 53 121;43 21 45 64 21];(1)B=A(2:5,1:2:5)B =65 24 2134 98 218 42 12143 45 21(2)A(7)=[]A =24 65 34 8 43 23 5 42 21 9 24 98 42 45 21 11 75 53 64 6 21 21 121 21(3)A+30(4)size(A);ndims(A)(5)题目有误(6)reshape(x,3,4)(7)abs(x)(8)char(x)4. L1 =0 0 0 0 1 0 0 0 0L2 =1 1 1 1 1 0 0 0 0L3 =0 0 0 1 1 1 0 0 0L4 =4 5 65.(1)B=A(1:3,:)C=A(:,1:2)D=A(2:4,3:4)E=B*CB =23.0000 10.0000 -0.7780 041.0000 -45.0000 65.0000 5.000032.0000 5.0000 0 32.0000C =23.0000 10.000041.0000 -45.000032.0000 5.00006.0000 -9.5400D =65.0000 5.0000如对您有帮助,欢迎下载支持,谢谢!0 32.000054.0000 3.1400E =1.0e+003 *0.9141 -0.22391.20802.71231.1330 -0.2103(2)E<Dans =0 10 00 1E&Dans =1 10 11 1E|Dans =1 11 11 1~D|~Eans =0 01 00 0find(A>=10&A<25)ans =156.all(A)ans =any(A)ans =1isnan(A)ans =0 1 0 0 0 0 0isinf(A)ans =如对您有帮助,欢迎下载支持,谢谢!0 0 1 1 0 0 0isfinite(A)ans =1 0 0 0 1 1 17.A(1).x1=’学号’;A(1).x2=’姓名’;A(1).x3=’专业’;A(1).x4.x41=’成绩1’;……….A(2).x1=’学号’;A(2).x2=’姓名’;A(2).x3=’专业’;A(2).x4.x41=’成绩1’;……….A(3).x1=’学号’;A(3).x2=’姓名’;A(3).x3=’专业’;A(3).x4.x41=’成绩1’;……….A(4).x1=’学号’;A(4).x2=’姓名’;A(4).x3=’专业’;A(4).x4.x41=’成绩1’;……….A(5).x1=’学号’;A(5).x2=’姓名’;A(5).x3=’专业’;A(5).x4.x41=’成绩1’;……….8.(1)size(B)ans =2 2ndims(B)ans =2(2)B(2)ans =[3x3 doubleB(4)ans ={3x3 cell}(3)B(3)=[]B =[1] [3x3 double] {3x3 cell}B{3}=[]B =[1] [3x3 double] []第三章1.(1)A=eye(3)(2) A=100+100*rand(5,6)(3)A=1+sqrt(0.2)*randn(10,50)(4)B=ones(size(A))(5)A+30*eye(size(A))(6)B=diag(diag(A))2.B=rot90(A)C=rot90(A,-1)如对您有帮助,欢迎下载支持,谢谢!3.B=inv(A) ;A的逆矩阵C=det(A) ;A的行列式的值D=A*BE=B*AD=E 因此A与A-1是互逆的。
第5章习题与答案5.1用矩阵三角分解方法解方程组123123123214453186920x x x x x x x x x +-=⎧⎪-+=⎨⎪+-=⎩ 解答:>>A=[2 1 -1;4 -1 3;6 9 -1] A =2 1 -1 4 -13 6 9 -1 >>b=[14 18 20]; b =14 18 20 >> [L, U, P]=lu(A) L =1.0000 0 0 0.6667 1.0000 0 0.3333 0.2857 1.0000 U =6.0000 9.0000 -1.0000 0 -7.0000 3.6667 0 0 -1.7143 P =0 0 1 0 1 0 1 0 0 >> y=backsub(L,P*b’) y =20.0000 4.6667 6.0000 >> x=backsub(U,y) x =6.5000 -2.5000 -3.5000 5.2 Cholesky 分解方法解方程组123121332352233127x x x x x x x ++=⎧⎪+=⎨⎪+=⎩ 解答:>> A=[3 2 3;2 2 0;3 0 12] A =3 2 32 2 03 0 12>> b=[5;3;7]b =537>> L=chol(A)L =1.7321 1.1547 1.73210 0.8165 -2.44950 0 1.7321>> y=backsub(L,b)y =-11.6871 15.7986 4.0415>> x=backsub(L',y)x =-6.7475 28.8917 49.93995.3解答:观察数据点图形>> x=0:0.5:2.5x =0 0.5000 1.0000 1.5000 2.0000 2.5000 >> y=[2.0 1.1 0.9 0.6 0.4 0.3]y =2.0000 1.1000 0.9000 0.6000 0.4000 0.3000 >> plot(x,y)图5.1 离散点分布示意图从图5.1观察数据点分布,用二次曲线拟合。
>> x=0:0.5:2.5 x =0 0.5000 1.0000 1.5000 2.0000 2.5000 >> y=[2.0 1.1 0.9 0.6 0.4 0.3] y =2.0000 1.1000 0.9000 0.6000 0.4000 0.3000 >> plot(x,y)>> orthpolyfit(x,y,2) ans =0.2857 -1.3371 1.9000于是得到的二次拟合曲线方程为20.2857 1.3371 1.9y x x =-+ >> xx=-1:0.01:3;>> pp=orthpolyfit(x,y,2); >> yy=polyval(pp,xx); >> plot(xx,yy) >> hold on>> scatter(x,y,'k*')00.51 1.52 2.5图5.2 二次曲线拟合示意图5.4已知函数()y f x =的观测数据为试构造不超过三次的插值多项式,并计算(1)f -的近似值。
解答:使用MATLAB 工具箱-1-0.500.51 1.522.5300.511.522.533.54图5.3 插值工具箱示意图Linear model Poly3:f(x) = p1*x^3 + p2*x^2 + p3*x + p4 Coefficients:p1 = 0.119 p2 = -0.07143 p3 = -2.619 p4 = 1 >> p=[p1 p2 p3 p4]; >> polyval(p,-1) ans = 3.4286即(1)f -=3.4286.5.5用最小二乘方法对下表中的数据用经验公式bxy ae =进行拟合解答:经验公式bxy ae =首先线性化,令ln ,ln y y y a bx ==+则。
图5.4(a )数据点分布图对数据点{},i i x y 进行分析,图5.4(b )线性化数据点分布图对图5.4(b )数据点进行线性拟合 >> c=linearfit(x,yp,1) c =12345678910-0.3011 8.1694于是ln 8.1694,0.3011a b ==-,求得3531.39,0.3011a b ==-。
于是所求的函数为0.30113531.39bxxy ae e-==,拟合示意图如下。
图5.4(c )数据点拟合示意图5.6使用数值方法求函数221()(1)f x x =+在 1.1,1.2,1.3x =处的导数值。
()f x 的函数值如下表中:在MA TLAB 命令行窗口中输入 >> x=[1.0 1.1 1.2 1.3 1.4];>> y=[0.2500 0.2268 0.2066 0.1890 0.1736];>>[A,df1]=npointdiff(x,y,1.1);[A,df2]=npointdiff(x,y,1.2);[A,df3]=npointdiff(x,y,1.3);[df1;df2;df3] ans =-0.2320 -0.2170 -0.20335.7用Romberg 方法计算积分21sin()x dx x⎰,要求结果保留5位小数。
解答:编写被积函数M 文件myfun07.m function f=myfun07(x)024681012f=sin(x)./x;参考EXAMP5008,修改romberg函数如下function[s,n]=romberg(f,a,b,Eps)if nargin<4Eps=1e-6;ends=1;s0=0;k=2;t=[];t(1,1)=(b-a)*(feval(f,a)+feval(f,b))/2;while abs(s-s0)>Epsh=(b-a)/2^(k-1);w=0;if h~=0for i=1:2^(k-1)-1w=w+feval(f,a+i*h);endt(k,1)=h*(feval(f,a)/2+w+feval(f,b)/2);for j=2:kfor i=1:(k-j+1)t(i,j)=(4^(j-1)*t(i+1,j-1)-t(i,j-1))/(4^(j-1)-1);endends=t(1,k);s0=t(1,k-1);k=k+1;n=k;elses=s0n=-kendend在命令行中调用romberg函数:>> y=romberg('myfun07',1,2);vpa(y,5)ans = 0.659335.8求矩阵1.0 1.00.51.0 1.00.250.50.25 2.0⎡⎤⎢⎥⎢⎥⎢⎥⎣⎦的全部特征值及其对应的特征向量。
解答:在MA TLAB命令行窗口中输入>> A=[1.0 1.0 0.5;1.0 1.0 0.25;0.5 0.25 2.0]; >> [V D] = eig(A)V =0.7212 0.4443 0.5315 -0.6863 0.5621 0.4615 -0.0937 -0.6976 0.7103 D =-0.0166 0 0 0 1.4801 00 0 2.53655.9使用Euler 方法解初值问题2(01)(0)1y y xy x y '⎧=--≤≤⎨=⎩,计算结果保留6位小数。
解答:编写右端向量函数M 文件myfun11.m function f=myfun10(x,y) f=-y-x.*y.*y;利用EXAMP5014,在命令行中调用euler 函数[x y]=euler('myfun10',0,1,1,0.1);format long ;y=vpa(y,6) y =[ 1.0, 0.9, 0.8019, 0.708849, 0.62289, 0.545081, 0.475718, 0.414567, 0.36108, 0.314542, 0.274183]5.10求解二阶微分方程的初值问题,(01)(0)0,(0)1y y x x y y ''-=≤≤⎧⎨'==⎩解答:令y z '=,原问题化为一阶方程组,(01)(0)0,(0)1y z z x y x y z '=⎧⎪'=+≤≤⎨⎪==⎩编写右端向量函数M 文件myfun11.m function y=myfun11(x,w) y=zeros(2,1); %y=w(1) %z=w(2) y(1)=w(2); y(2)=x+w(1);在命令行中调用ode45函数求原函数及导函数:[x,y]=ode45(@dy,[0 1],[0 1]);x=x',z=y';y=z(1,:),yy=z(2,:) x =Columns 1 through 150 0.0001 0.0001 0.0002 0.0002 0.0005 0.0007 0.0010 0.0012 0.0025 0.0037 0.0050 0.0062 0.0125 0.0188 Columns 16 through 300.0251 0.0313 0.0563 0.0813 0.1063 0.1313 0.1563 0.18130.2063 0.2313 0.2563 0.2813 0.3063 0.3313 0.3563Columns 31 through 450.3813 0.4063 0.4313 0.4563 0.4813 0.5063 0.5313 0.5563 0.5813 0.6063 0.6313 0.6563 0.6813 0.7063 0.7313Columns 46 through 570.7563 0.7813 0.8063 0.8313 0.8563 0.8813 0.9063 0.9313 0.9485 0.9657 0.9828 1.0000y =Columns 1 through 150 0.0001 0.0001 0.0002 0.0002 0.0005 0.0007 0.0010 0.0012 0.0025 0.0037 0.0050 0.0062 0.0125 0.0188Columns 16 through 300.0251 0.0314 0.0564 0.0815 0.1067 0.1321 0.1576 0.1833 0.2093 0.2355 0.2620 0.2888 0.3160 0.3435 0.3715Columns 31 through 450.4000 0.4289 0.4584 0.4884 0.5190 0.5502 0.5821 0.6146 0.6480 0.6820 0.7169 0.7526 0.7893 0.8268 0.8653Columns 46 through 570.9048 0.9453 0.9869 1.0296 1.0735 1.1186 1.1649 1.21261.2460 1.2802 1.3149 1.3504yy =Columns 1 through 151.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0002 1.0004Columns 16 through 301.0006 1.0010 1.0032 1.0066 1.0113 1.0173 1.0245 1.0330 1.0427 1.0538 1.0661 1.0797 1.0946 1.1108 1.1283Columns 31 through 451.1472 1.1674 1.1890 1.2119 1.2362 1.2619 1.2890 1.3176 1.3476 1.3791 1.4120 1.4465 1.4825 1.5200 1.5591Columns 46 through 571.5999 1.6422 1.6862 1.7319 1.7793 1.8284 1.8793 1.9320 1.96922.0073 2.0463 2.0862作图plot(x,y,'k-',x,yy,'k*');title('Solution of ODEs with ode45')legend('primitive function','derived function')图形结果如下:图5.5 二阶微分方程求解示意图5.11取步长0.1h =,用经典公式求解初值问题2,(01)(0)1y xy x y '=≤≤⎧⎨=⎩解答:参考EXAMP5016,编写右端函数M 文件myfun12.mfunction f=myfun12(x,y)f=2*x*y;修改函数rungekutta 如下function [xx,yy]=rungekutta(f,a,b,y0,h)% f is the function entered as a string 'f'% a,b are the left and right end points% ya is the initial condition% h is the stepx=a:h:b;n=length(x);y=zeros(1,n);y(1)=y0;for i=1:n-1k1=feval(f,x(i),y(i));k2=feval(f,x(i)+h/2,y(i)+h/2*k1);k3=feval(f,x(i)+h/2,y(i)+h/2*k2);k4=feval(f,x(i+1),y(i)+h*k3);y(i+1)=(k1+2*k2+2*k3+k4)*h/6+y(i); endxx=x;yy=y;ey=exp(x.*x)plot(xx,yy,'kp',xx,ey,'k*');xlabel('Independent Variable x'),ylabel('Dependent Variable y')title('Solution of ODE with Runge-Kutta method')legend('numal solution','exact solution')在命令行中调用rungekutta函数:>> [x y]=rungekutta('myfun12',0,1,1,0.1)ey =1.0000 1.0101 1.0408 1.0942 1.1735 1.2840 1.4333 1.6323 1.89652.2479 2.7183x =0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000y =1.0000 1.0101 1.0408 1.0942 1.1735 1.2840 1.4333 1.6323 1.89652.2479 2.7183图形结果如下:图5.6 经典公式求解示意图。