MATLAB程序设计与应用(第二版)实验答案-095

合集下载

Matlab编程与工程应用(第二版)习题解答(全)

Matlab编程与工程应用(第二版)习题解答(全)

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.。

MATLAB程序设计教程(第二版)课后答案

MATLAB程序设计教程(第二版)课后答案

MATLAB第二版课后答案unit3-8 unit3实验指导1、n=input('请输入一个三位数:');a=fix(n/100);b=fix((n-a*100)/10);c=n-a*100-b*10;d=c*100+b*10+a2(1)n=input('请输入成绩');switch ncase num2cell(90:100)p='A';case num2cell(80:89)p='B';case num2cell(70:79)p='C';case num2cell(60:69)p='D';otherwisep='E';endprice=p(2)n=input('请输入成绩'); if n>=90&n<=100p='A';elseif n>=80&n<=89p='B';elseif n>=70&n<=79p='C';elseif n>=60&n<=69p='D';elsep='E';endprice=p(3)tryn;catchprice='erroe'end3n=[1,5,56,4,3,476,45,6,3,76,45,6,4,3,6,4,23,76,908,6];a=n(1);b=n(1);for m=2:20if n(m)>aa=n(m);elseif n(m)<bb=n(m);endendmax=amin=b法2n=[1,5,56,4,3,476,45,6,3,76,45,6,4,3,6,4,23,76,908,6]; min=min(n)max=max(n)4b=[-3.0:0.1:3.0];for n=1:61a=b(n);y(n)=(exp(0.3*a)-exp(-0.3*a))/2*sin(a+0.3)+log((0.3+a)/2);y5y1=0;y2=1;n=input('请输入n的值:');for i=1:ny1=y1+1/i^2;y2=y2*((4*i*i)/((2*i-1)*(2*i+1)));endy1y26A=[1,1,1,1,1,1;2,2,2,2,2,2;3,3,3,3,3,3;4,4,4,4,4,4;5,5,5,5,5,5;6,6,6,6,6,6]; n=input('请输入n的值:');if n<=5&n>=0disp(A([n],:));elseif n<0disp(lasterr);else disp(A([6],:));disp(lasterr);7(1)f=[];for n=1:40f(n)=n+10*log(n^2+5); endy=f(40)/(f(30)+f(20)) (2)f=[];a=0;for n=1:40f(n)=a+n*(n+1);a=f(n);endy=f(40)/(f(30)+f(20))8y=0;m=input('输入m的值:'); n=input('输入n值:'); for i=1:ny=y+i^m;endy************************************************************ function s=shi8_1(n,m)s=0;for i=1:ns=s+i^m;end************************************************************ shi8_1(100,1)+shi8_1(50,2)+shi8_1(10,1/2)思考练习2N=[1,2,3,4,5];2.*NN./21./N1./N.^23s=fix(100*rand(1,20)*9/10+10)y=sum(s)/20j=0;for i=1:20if s(i)<y&rem(s(i),2)==0j=j+1;A(j)=s(i);else continue;endendA4y1=0;y2=0;n=input('请输入n的值:'); for i=1:ny1=y1+-(-1)^i/(2*i-1);y2=y2+1/4^i;endy1y2unit4实验指导1(1)x=-10:0.05:10;y=x-x.^3./6;plot(x,y)(2)x=-10:0.5:10;ezplot('x^2+2*y^2-64',[-8,8]); grid on;2t=-pi:pi/10:pi;y=1./(1+exp(-t));subplot(2,2,1);bar(t,y);title('条形图(t,y)');axis([-pi,pi,0,1]);subplot(2,2,2);stairs(t,y,'b');title('阶梯图(t,y)');axis([-pi,pi,0,1]);subplot(2,2,3);stem(t,y,'k');title('杆图(t,y)');axis([-pi,pi,0,1]);subplot(2,2,4);loglog(t,y,'y');title('对数坐标图(t,y)');3(1)t=0:pi/50:2*pi;r=5.*cos(t)+4;polar(t,r);title('\rho=5*cos\theta+4'); (2)t=-pi/3:pi/50:pi/3;r=5.*((sin(t)).^2)./cos(t); polar(t,r);4(1)t=0:pi/50:2*pi;x=exp(-t./20).*cos(t);y=exp(-t./20).*sin(t);z=t;plot3(x,y,z);grid on;(2)[x,y]=meshgrid(-5:5);z=zeros(11)+5;mesh(x,y,z);shading interp;5[x,y,z]=sphere(20);surf(x,y,z);axis off;shading interp;m=moviein(20);for i=1:20axis([-i,i,-i,i,-i,i])m(:,i)=getframe;endmovie(m,4);思考练习2(1)x=-5:0.1:5;y=(1./(2*pi)).*exp((-(x.^2))/2); plot(x,y);(2)t=-2*pi:0.1:2*pi;x=t.*sin(t);y=t.*cos(t);plot(x,y);grid on;3t=0:pi/1000:pi;x=sin(3.*t).*cos(t);y1=sin(3.*t).*sin(t);y2=2.*x-0.5;plot(x,y1,'k',x,y2);hold on;k=find(abs(y1-y2)<1e-2);x1=x(k);y3=2.*x1-0.5;plot(x1,y3,'rp');4x=-2:0.01:2;y=sin(1./x);subplot(2,1,1);plot(x,y);subplot(2,1,2);fplot('sin(1./x)',[-2,2],1e-4); 5(1)i=-4*pi:0.1:10;j=12./sqrt(i);polar(i,j);title('{\rho}=12/sqrt(\theta)')(2)a=-pi/6:0.01:pi/6;b=3.*asin(a).*cos(a)./((sin(a)).^3+(cos(a)).^3); polar(a,b);6(1)[u,v]=meshgrid(-4:0.1:4);x=3.*u.*sin(v);y=2.*u.*cos(v);z=4.*u.^2;subplot(2,1,1);mesh(x,y,z);subplot(2,1,2);surf(x,y,z);(2)[x,y]=meshgrid(-3:0.2:3);z=-5./(1+x.^2+y.^2);subplot(1,2,1);mesh(x,y,z);subplot(1,2,2);surf(x,y,z);unit5实验指导1A=randn(10,5)x=mean(A)y=std(A)Max=max(max(A)) Min=min(min(A)) Sumhang=sum(A,2) SumA=sum(Sumhang) B=sort(A);C=sort(B,2,'descend'); C2(1)a=0:15:90;b=a./180.*pi;s=sin(b)c=0:15:75;d=c./180.*pi;t=tan(d)e=input('请输入想计算的值:'); S=sin(e/180*pi)T=tan(e/180*pi)S1=interp1(a,s,e,'spline')T1=interp1(c,t,e,'spline')P1=polyfit(a,s,5);P2=polyfit(c,t,5);S2=polyval(P1,e)T2=polyval(P2,e)(2)n=[1,9,16,25,36,49,64,81,100]; N=sqrt(n);x=input('ji suan zhi : ');interp1(n,N,x,'cubic')3N=64;T=5;t=linspace(0,T,N);h=exp(-t);dt=t(2)-t(1);f=1/dt;X=fft(t);F=X(1:N/2+1);f=f*(0:N/2)/N;plot(f,abs(F),'-*')4P=[2,-3,0,5,13];Q=[1,5,8];p=polyder(P)q=polyder(P,Q)[a,b]=polyder(P,Q)5P1=[1,2,4,0,5];P2=[0,1,2];P3=[1,2,3];P=P1+conv(P2,P3)X=roots(P)A=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5]; p=polyval(P,A)思考练习4A=rand(1,30000);a=mean(A)b=std(A)Max=max(A)Min=min(A)n=0;for i=1:30000if(A(i)>0.5)n=n+1;endendny=n/300005p=[45,74,54,55,14;78,98,45,74,12;87,98,85,52,65] [M,S]=max(p)[N,H]=min(p)junzhi=mean(p,1)fangcha=std(p,1,1)zong=sum(p,2);[Max,wei]=max(zong)[Min,wei]=min(zong)[zcj,xsxh]=sort(zong,'descend')6x=[1:10:101];y=[0,1.0414,1.3222,1.4914,1.6128,1.7076,1.7853,1.8513,1.9085,1.9590,2.0043]; [p,s]=polyfit(x,y,5)a=1:5:101;y1=polyval(p,a);plot(x,y,':o',a,y1,'-*')unit6实验指导1A=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6];p=[0.95,0.67,0.52]';x=A\pA=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6];p=[0.95,0.67,0.53]';x=A\pcond(A)2(1)x1=fzero(@funx1,-1)function fx=funx1(x)fx=x^41+x^3+1;(2)x2=fzero(@funx2,0.5)function fx=funx2(x)fx=x-sin(x)/x;(3)options=optimset('Display','off'); x=fsolve(@fun3,[1,1,1]',options) q=fun3(x)function q=fun3(p)x=p(1);y=p(2);z=p(3);q(1)=sin(x)+y^2+log(z)-7;q(2)=3*x+2^y-z^3+1;q(3)=x+y+z-5;3(1)t0=0;tf=5;y0=1;[t,y]=ode23(@fun4,[t0,tf],y0); t'y'function yp=fun4(t,y)yp=-(1.2+sin(10*t))*y;(2)t0=0;tf=5;y0=1;[t,y]=ode23(@fun5,[t0,tf],y0); t'y'function yp=fun5(t,y)yp=cos(t)-y/(1+t^2);4x=fminbnd(@mymin,0,2);-mymin(x)function fx=mymin(x)fx=-(1+x.^2)/(1+x.^4);5options=optimset('Display','off');[x,fval]=fmincon(@fun6,[0,0,0],[],[],a,b,lb,ub)-fvalfunction f=fun6(x)f=-(sqrt(x(1))+(400-x(1))*1.1+(sqrt(x(2))+(400-x(1))*1.1-x(2))*1.1+sqrt(3)+(((400-x(1))*1.1-x(2))*1.1-x(3))*1.1+sqrt(x(x4)));思考练习1(1)A=[2,3,5;3,7,4;1,-7,1];B=[10,3,5]';C1=inv(A)*BC2=A\B[L,U]=lu(A);x=U\(L\B)(2)A=[6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0,2];B=[-4,13,1,11]';C1=inv(A)*BC2=A\B[L,U]=lu(A);x=U\(L\B)2(1)x1=fzero(@funx1,1.5)function fx=funx1(x)fx=3*x+sin(x)-exp(x);(2)x1=fzero(@funx2,1)function fx=funx2(x)fx=x-1/x+5;(3)options=optimset('Display','off'); x=fsolve(@fun3,[3,0]',options) q=fun3(x)function q=fun3(p)x=p(1);y=p(2);q(1)=x^2+y^2-9;q(2)=x+y-1;3(1)t0=0;tf=5;y0=[0,1];[t,y]=ode45(@vdpol,[t0,tf],y0);[t,y]function ydot=vdpol(t,y);ydot(1)=(2-3*y(2)-2*t*y(1))./(1+t^2);ydot(2)=y(1);ydot=ydot';(2)t0=0;tf=5;y0=[1;0;2];[t,y]=ode45(@vdpoll,[t0,tf],y0);[t,y]function ydot=vdpoll(t,y);ydot(1)=cos(t)-y(3)./(3+sin(t))+5*y(1).*cos(2*t)/((t+1).^2)-y(2); ydot(2)=y(1);ydot(3)=y(2);ydot=ydot';4x=fminbnd(@mymin,0,pi);-mymin(x)function fx=mymin(x)fx=-sin(x)-cos(x.^2);5[x,y1]=fminbnd(@mymax,0,1.5);-y1function fx=mymax(x);fx=-(9*x+4*x.^3-12*x.^2);unit7实验指导1(1)format longfx=inline('sin(x)./x');[I,n]=quadl(fx,0,2,1e-10)(2)format longfx=inline('1./((x-0.3).^2+0.01)-1./((x-0.9).^2+0.04)-6'); [I,n]=quad(fx,0,1,1e-10)2(1)global ki;ki=0;I=dblquad(@fxy,0,1,0,1)ki(2)f=inline('abs(cos(x+y))','x','y');I=dblquad(f,0,pi,0,pi)3X=0.3:0.2:1.5;F=[0.3895,0.6598,0.9147,1.1611,1.3971,1.6212,1.8325];trapz(X,F)4p=0:pi/5:2*pi;for n=1:3nDX=diff(sin(p),n)end5f=inline('sin(x)./(x+cos(2.*x))');g=inline('(cos(x).*(x+cos(2*x))-sin(x).*(1-2.*sin(2*x)))/(x+cos(2.*x)).^2');x=-pi:0.01:pi;p=polyfit(x,f(x),5);dp=polyder(p);dpx=polyval(dp,x); %求dp在假设点的函数值dx=diff(f([x,3.01]))/0.01; %直接对f(x)求数值导数gx=g(x); %求函数f的导函数g在假设点的导数plot(x,dpx,x,dx,'.',x,gx,'-'); %作图思考练习2format longfx=inline('1./(1+x.^2)');[I,n]=quad(fx,-Inf,Inf,1e-10)[I,n]=quadl(fx,-Inf,Inf,1e-10)x=-100000:100000;y=1./(1+x.^2);trapz(x,y)format short3(1)format longfx=inline('log(1+x)./(1+x.^2)');[I,n]=quad(fx,0,1,1e-10)(2)format longfx=inline('sqrt(cos(t.^2)+4*sin((2*t).^2)+1)'); [I,n]=quad(fx,0,2*pi,1e-10)4f=inline('4.*x.*z.*exp(-z.^2.*y-x.^2)');I=triplequad(f,0,pi,0,pi,0,1)5f=inline('sin(x)');g=inline('cos(x)');x=0:0.01:2*pi;p=polyfit(x,f(x),5);dp=polyder(p);dpx=polyval(dp,x);dx=diff(f([x,2*pi+0.01]))/0.01; gx=g(x);plot(x,dpx,x,dx,'.',x,gx,'-') unit8实验指导1syms x y;s=x^4-y^4;factor(s)factor(5135)2syms x;f=(x-2)/(x^2-4);limit(f,x,2)sym x;f=(sqrt(pi)-sqrt(acos(x)))/sqrt(x+1); limit(f,x,-1,'right')3sym x;f=sin(1/x);diff(f,'x')diff(f,'x',2)sym x;f=(1-cos(2*x))/x;diff(f,'x')diff(f,'x',2)4sym x;f=sqrt(exp(x)+1);int(f,'x')syms x y;f=x/(x+y);int(f,'y')sym x;f=exp(x)*(1+exp(x))^2;int(f,'x',0,log(2))sym x;f=x*log(x);int(f,'x',1,exp(1))5sym x;s=symsum((-1)^(x+1)/x,1,Inf)sym y;z=symsum(y^(2*y-1)/(2*y-1),1,Inf)6sym x;f1=(exp(x)+exp(-x))/2;f2=sqrt(x^3-2*x+1);(f1,x,5,0)(f2,x,6,0)7syms x y a;x=solve('x^3+a*x+1=0','x')x=solve('sin(x)+2*cos(x)-sqrt(x)=0','x') [x y]=solve('log(x/y)=9','exp(x+y)=3','x','y')8syms n;[x,y]=dsolve('x*(D2y)+(1-n)*(Dy)+y=0','y(0)=0','Dy(0)=0','x') 思考练习2syms x B1 B2 a bs1=2*((cos(x))^2)-(sin(x))^2;s2=sin(B1)*cos(B2)-cos(B1)*sin(B2);s3=sqrt((a+sqrt(a^2-b))/2)+sqrt((a-sqrt(a^2-b))/2);s4=(4*x^2+8*x+3)/(2*x+1);h1=simplify(s1)h2=simplify(s2)h3=simplify(s3)h4=simplify(s4)3syms x a;f=abs(x)/x;limit(f,x,0,'left')f=(x+a/x)^x;limit(f,x,inf)4syms x y mf=sqrt(x+sqrt(x+sqrt(x)));m=diff(f,'x')diff(m,'x')syms x y;f=x+y-sqrt(x^2+y^2);z1=diff(f,x)z2=diff(f,y)5syms x y;f=x+y-sqrt(x^2+y^2);z1=diff(f,x)z2=diff(f,y)sym x;f=1/(asin(x)^2*(1-x^2)^(1/2)); int(f)6syms xf=1/(1+x);int(f,0,4)sym x;f=x^3*sin(x)^2/(x^6+2*x^4+1);int(f,-1,1)sym x;f=x^3*sin(x)^2/(x^6+2*x^4+1);int(f,-1,1)7syms ns=symsum(1/4^n,1,inf)sym n;s=symsum(((n+1)/n)^(1/2),1,inf) eval(y)8syms xf=tan(x);(f,x,3,0)syms xf=sin(x)^2;(f,x,5,0)9syms xx=solve('log(1+x)-5/(1+sin(x))=2','x')syms x y z[x yz]=solve('4*x^2/(4*x^2+1)=y','4*y^2/(4*y^2+1)=z','4*z^2/(4*z^2+1)=x','x','y','z')10[x ,y]=dsolve('Dx=3*x+4*y','Dy=5*x-7*y','x(0)=0','y(0)=1','t')。

Matlab程序设计与应用第二版刘卫国课后实验答案

Matlab程序设计与应用第二版刘卫国课后实验答案

实验一:T1:%%第一小题z1=2*sin(85*pi/180)/(1+exp(2))%%第二小题x=[2,1+2i;,5];z2=1/2*log(x+sqrt(1+x.^2));z2%%第三小题a=::;z3=1/2*(exp*a)-exp*a)).*sin(a++log(+a )/2)%%第四题t=0::z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t. ^2-1)+(t>=2&t<3).*(t.^2-2*t+1)T2:A=[12,34,-4;34,7,87;3,65,7]B=[1,3,-1;2,0,3;3,-2,7]disp ('A+6*B=');disp(A+6*B);disp('A-B+I=');disp(A-B+eye(3));disp('A*B=');disp(A*B);disp('A.*B=');disp(A.*B);disp('A^3=');disp(A^3);disp('A.^3=');disp(A.^3);disp('A/B=');disp(A/B);disp('B\A=');disp(B\A);disp('[A,B]=');disp([A,B]);disp('[A([1,3],:);B^2]=');disp([A([1,3],:);B^2]);T3:z=1:25;A=reshape(z,5,5)';B=[3,0,16;17,-6,9;0,23,-4;9,7,0;4,13,11];C=A*BD=C(3:5,2:3)T4-1:a=100:999;b=find(rem(a,21)==0);c=length(b)T4-2:a=input('请输入一个字符串:','s');b=find(a>='A'&a<='Z');a(b)=[];disp(a);实验二:T1:E=eye(3),R=rand(3,2),O=zeros(2,3),S=d iag([1,2]);A=[E,R;O,S]disp('A^2=');disp(A^2);disp('[E,R+RS;O,S^2]');B=[E,R+R*S;O,S^2]T2:H=hilb(5)P=pascal(5)Hh=det(H)Hp=det(P)Th=cond(H)Tp=cond(P)a=abs(Th-1);b=abs(Tp-1);if a>bdisp('帕萨卡矩阵P性能更好'); elseif a<bdisp('希尔伯特矩阵H性能更好'); elsedisp('两个矩阵性能相同');endT3:a=1:25;A=reshape(a,5,5)disp('行列式的值:');disp(det(A));disp('矩阵的秩:');disp(rank(A));disp('矩阵的迹:');disp(trace(A));disp('矩阵的范数:');disp(norm(A));T4:A=[-29,6,18;20,5,12;-8,8,5][V,D]=eig(A)T5:A=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/ 6]B=[,,]'X1=A\BB(3)=X2=A\Bdisp('系数矩阵A的条件数:');disp(cond(A));T6:a=1:25;A=reshape(a,5,5)disp('B1=sqrtm(A)');B1=sqrtm(A)disp('B2=sqrt(A)');B2=sqrt(A)disp('B1*B1');B1*B1disp('B2.*B2');B2.*B2实验三:T1:x=:2:for i=1:length(x);if (x(i)<0)&(x(i)~=-3)y(i)=x(i)^2+x(i)-6;elseif(x(i)>=0)&(x(i)<5)&(x(i)~=2)&(x(i)~=3 );y(i)=x(i)^2-5*x(i)+6;elsey(i)=x(i)^2-x(i)-1;endendyT2-if:s=input('please enter the score:');while (s<0||s>100)disp('the score is not reasonable'); s=input('please enter the score:'); endif (s>=90&s<=100);disp('A');elseif(s>=80&s<90);disp('B');elseif(s>=70&s<80);disp('C');elseif(s>=60&s<70);disp('D');elsedisp('E');endT2-switch:s=input('please enter the score:'); while (s<0||s>100)disp('the score is not reasonable'); s=input('please enter the score:'); endswitch fix(s/10)case{9,10}disp('A');case{8}disp('B');case{7}disp('C');case{6}disp('D');case{0,1,2,3,4,5}disp('E');endT3:t=input('请输入工时time=');if t>120w=120*84+(t-120)**84;elseif t<60w=t*84-700;elsew=84*t;enddisp('应发工资为:');disp(w);T4:a=10+floor(rand(1)*89)b=10+floor(rand(1)*89)s=input('请输入+或-或*或/','s');while(s~='+'&&s~='-'&&s~='*'&&s~='/') disp('输入的符号错误,请重新输入'); s=input('请输入+或-或*或/','s'); endswitch scase{'+'}c=a+b;case{'-'}c=a-b;case{'*'}c=a*b;case{'/'}c=a/b;endcT5:A=rand(5,6)n=input('请输入n:');while (n<1)disp('输入的n有误,请重新输入'); n=input('请输入n:');endif n>5B=A(5,:)elseB=A(n,:)End实验四:T1:n=input('请输入n:');m=0;for i=1:n;m=m+1/i^2;endpii=sqrt(6*m)T1-2:n=input('请输入n:');m=1./(1:n).^2;s=sum(m);pii=sqrt(6*s)T2:n=1000;y=0;for i=1:n;y=y+1/(2*i-1);if y>=3disp('最大n值为:');i-1disp('对应的y值为:');y-1/(2*i-1)break;endendT3:format longa=input('请输入a:');b=input('请输入b:');x(1)=a/+b);i=1;x(2)=a/(x(i)+b);while (abs(x(i+1)-x(i))>&i<500)i=i+1;x(i+1)=a/(x(i)+b);endr1=(-b+sqrt(b^2+4*a))/2;r2=(-b-sqrt(b^2+4*a))/2;disp(['x',num2str(i+1),'=',num2str(x( i+1),8)]);disp(['x',num2str(i),'=',num2str(x(i) ,8)]);disp(['r1=',num2str(r1,8),',r2=',num2 str(r2,8)]);format shortT4:f(1)=1;f(2)=0;f(3)=1;i=4;while(i<=100)f(i)=f(i-1)-2*f(i-2)+f(i-3);i=i+1;endfmax=max(f)fmin=min(f)fsum=sum(f)fp=length(find(f>0))fn=length(find(f<0))f0=length(find(f==0))T5:j=0;s=0;for i=3:50m=i*(i-1)-1;if isprime(m)==1ms=s+m;j=j+1;endenddisp(['亲密数的对数:',num2str(j)]); disp(['所有亲密素数之和:',num2str(s)]);实验五:fn1:function f=fn1(n)f=n+10*log(n.^2+5);fn2:function f=fn2(n)f=0;for i=1:nf=f+i*(i+1);endfx:function f=fx(x)f=1./((x-2).^2++1./((x-3).^4+;T1:x=input('请输入一个复数:');fe=exp(x)flg=log(x)fsin=sin(x)fcos=cos(x)T2:m1=input('请输入m1:');m2=input('请输入m2:');t=input('请输入theta:');A=[m1*cos(t),-m1,-sin(t),0;m1*sin(t), 0,...cos(t),0;0,m2,-sin(t),0;0,0,-cos(t),1 ]B=[0,m1*,0,m2*]'disp('[a1,a2,N1,N2]=');disp([A\B]');T3:j=1;n=1;k=1;for i=10:99 % 挑选出10~99中所有的素数if isprime(i)==1m(j)=i;j=j+1;endendfor t=1:length(m);s(n)=10*rem(m(t),10)+fix(m(t)/10); %挑选出的素数进行位置交换if isprime(s(n))==1 %再挑选出交换绝对素数r(k)=m(t);k=k+1;endn=n+1;enddisp(['所有两位绝对素数:',num2str(r)])T5:y1=fn1(40)/(fn1(30)+fn1(20)) %函数调用y2=fn2(40)/(fn2(30)+fn2(20))实验六:T1:x=linspace(0,2*pi,101);y=+3*sin(x)./(1+x.^2)).*cos(x);plot(x,y);T2:x=linspace(0,2*pi,100);y1=x.^2;y2=cos(2*x);y3=y1.*y2;subplot(2,2,1);plot(x,y1,'r:',x,y2,'y-',x,y3,'b-.'); subplot(2,2,2);plot(x,y1,'r:');subplot(2,2,3);plot(x,y2,'y-');subplot(2,2,4);plot(x,y3,'b-.');T2-3:x=linspace(0,2*pi,10);y1=x.^2;y2=cos(2*x);y3=y1.*y2;subplot(3,4,1);bar(x,y1,'r');axis([0,7,0,40]); subplot(3,4,2);stairs(x,y1,'y');axis([0,7,0,40]); subplot(3,4,3);stem(x,y1,'b');axis([0,7,0,40]); subplot(3,4,4);fill(x,y1,'g');axis([0,7,0,40]); subplot(3,4,5);bar(x,y2,'r');axis([-1,7,-1,1]); subplot(3,4,6);stairs(x,y2,'y');axis([-1,7,-1,1]); subplot(3,4,7);stem(x,y2,'b');axis([-1,7,-1,1]); subplot(3,4,8);fill(x,y2,'g');axis([-1,7,-1,1]); subplot(3,4,9);bar(x,y3,'r');axis([0,7,-30,50]); subplot(3,4,10);stairs(x,y3,'y');axis([0,7,-30,50]); subplot(3,4,11);stem(x,y3,'b');axis([0,7,-30,50]); subplot(3,4,12);fill(x,y3,'g');axis([0,7,-30,50]);T3:x=linspace(-5,5,100);y=[];for x0=x;if x0<=0;y=[y,(x0+sqrt(pi))/exp(2)]; elsey=[y,1/2*log(x0+sqrt(1+x0^2))];endendsubplot(1,2,1);plot(x,y);for i=1:length(x)if x(i)<=0y(i)=(x(i)+sqrt(pi))/exp(2); elsey(i)=1/2*log(x(i)+sqrt(1+x(i)^2)); endendsubplot(1,2,2);plot(x,y);T4:a=input('请输入a:');b=input('请输入b:');n=input('请输入n:');theta=0::2*pi;y=a.*sin(b+n.*theta);polar(theta,y);T5:x=linspace(-5,5,21);y=linspace(0,10,31);[x,y]=meshgrid(x,y);z=cos(x).*cos(y).*exp(-sqrt(x.^2+y.^2 )/4);subplot(1,2,1);surf(x,y,z);subplot(1,2,2);contour3(x,y,z);T6:s=0:pi/2;t=0:3*pi/2;[s,t]=meshgrid(s,t);x=cos(s).*cos(t);y=cos(s).*sin(t);z=sin(s);surf(x,y,z);shading interp;实验八:T1:A=rand(1,30000);disp('均值为:');mean(A)disp('标准方差为:');std(A)disp('最大元素为:');max(A)disp('最小元素为:');min(A)k=find(A>;b=length(k);disp('大于的随机数个数占总数的百分比:');sprintf('%f%%', b/300)T2:P=45+50*rand(100,5)disp('最高分及序号:');[Y,U]=max(P,[],1)disp('最低分及序号:');[Y1,U1]=min(P,[],1)disp('每门课平均分:');mean(P,1)disp('每门课的标准方差:');std(P,0,1)s=sum(P,2);disp('总分最高分:');[Y2,U2]=max(s,[],1)disp('总分最低分:');[Y3,U3]=min(s,[],1)[zcj,xsxh]=sort(s,1,'descend') %desc end降序T3:h=6:2:18;t1=[,,,,,,];t2=[,,,,,,];t=:2:;T1=interp1(h,t1,t,'spline')T2=interp1(h,t2,t,'spline')T4:x=1:10:101;y=log10(x);p=polyfit(x,y,5)x1=1::101;y1=log10(x1);y2=polyval(p,x1);plot(x1,y1,'r-',x1,y2,'b-.');T5:P1=[1,2,4,0,5];P2=[0,0,0,1,2];P3=[0,0,1,2,3];P4=conv(P2,P3);sp1=length(P1);sp2=length(P4);P=[zeros(1,sp2-sp1),P1]+P4x=roots(P)A=[-1,,;,2,;0,5,];Ax=polyval(P,A)AA=polyvalm(P,A)实验九:T1:f=inline('det([x x^2 x^3;1 2*x 3*x^2;0 2 6*x])');i=1;x=1;while x<=g(i)=f(x);i=i+1;x=x+;enddx=diff(g)/;dx(1)dx(101)dx(length(g)-1)T2:f1=inline('sqrt(cos(t.^2)+4*sin(2*t). ^2+1)')f2=inline('log(1+x)./(1+x.^2)')I1=quad(f1,0,2*pi)I2=quad(f2,0,2*pi)T3:A=[6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0 ,2];B=[-4,13,1,11]';x1=A\B[L,U]=lu(A);x2=U\(L\B)[Q,R]=qr(A);x3=R\(Q\B)fun:function F=fun(X);x=X(1);y=X(2);z=X(3);F(1)=sin(x)+y^2+log(z)-7;F(2)=3*x+2^y-z^3+1;F(3)=x+y+z-5;T5:f=inline('3*x+sin(x)-exp(x)');fzero(f,X=fsolve('fun',[1 1 1]',optimset('Display','off'))实验十:T1:x=sym('6');y=sym('5');z=(x+1)/(sqrt(3+x)-sqrt(y))T2:syms x y;t=sym('5135');factor(x^4-y^4)factor(t)T3:syms beta1 beta2 x;simplify(sin(beta1)*cos(beta2)-cos(be ta1)*sin(beta2))simplify((4*x^2+8*x+3)/(2*x+1))T4:syms a b c d e f g h k;p1=[0,1,0;1,0,0;0,0,1];p2=[1,0,0;0,1,0;1,0,1];A=[a,b,c;d,e,f;g,h,k];B=p1*p2*AB1=inv(B)B1*B;tril(B)det(B)T5:syms x t a yf1=(x*(exp(sin(x))+1)-2*(exp(tan(x))-1))/sin(x)^3;limit(f1,x,0) %(1)f2=(sqrt(pi)-sqrt(acos(x)))/sqrt(x+1) ;limit(f2,x,-1,'right') %(2)f3=(1-cos(2*x))/x;diff(f3,x,1) %(3)diff(f3,x,2)A=[a^x,t^3;t*cos(x),log(x)];diff(A,x,1) &(4)diff(A,t,2)diff(diff(A,x,1),t,1)f5=(x^2-2*x)*exp(-x^2-y^2-x*y);yx=-diff(f5,x,1)/diff(f5,y,1) %(5) fxy=diff(diff(f5,x,1),y,1)x=sym('0');y=sym('1');eval(fxy)T6:x=sym('x');f1=1/(1+x^4+x^8);int(f1,x)f2=1/(asin(x))^2/sqrt(1-x^2);int(f2,x)f3=(x^2+1)/(x^4+1);int(f3,x,0,inf)f4=exp(x)*(1+exp(x))^2;int(f4,x,0,log(2))实验十一:T1:syms n x ;S1=symsum(1/(2*n-1),n,1,10)S2=symsum(n^2*x^(n-1),n,1,inf)S3=symsum(n^2/5^n,n,1,inf)T2:x=sym('x');f=log(x);taylor(f,x,6,1)T3:x1=solve('log(1+x)-5/(1+sin(x))=2')x2=solve('x^2+9*sqrt(x+1)-1=0')x3=solve('3*x*exp(x)+5*sin(x)=0')[x4,y4]=solve('sqrt(x^2+y^2)-100=0',' 3*x+5*y-8=0')T4:dsolve('D2y+4*Dy+29*y=0','y(0)=0','Dy (0)=15','x')T5:[x,y,z]=dsolve('Dx=2*x-3*y+3*z','Dy=4 *x-5*y+3*z','Dz=4*x-4*y+2*z','t')。

MATLAB程序设计及应用(第二版)课后实验答案

MATLAB程序设计及应用(第二版)课后实验答案

Matlab课后实验题答案实验一 MATLAB运算基础1. 先求下列表达式的值,然后显示MATLAB工作空间的使用情况并保存全部变量。

(1)0 122sin851ze =+(2)21ln( 2z x=+,其中2120.455i x+⎡⎤=⎢⎥-⎣⎦(3)0.30.330.3sin(0.3)ln, 3.0, 2.9,,2.9,3.0 22a ae e az a a--+=++=--(4)2242011122123t tz t tt t t⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t=0:0.5:2.52. 已知:1234413134787,2033657327A B --⎡⎤⎡⎤⎢⎥⎢⎥==⎢⎥⎢⎥⎢⎥⎢⎥-⎣⎦⎣⎦求下列表达式的值:(1) A+6*B 和A-B+I (其中I 为单位矩阵) (2) A*B 和A.*B (3) A^3和A.^3 (4) A/B 及B\A(5) [A,B]和[A([1,3],:);B^2] 解:3. 设有矩阵A 和B123453166789101769,111213141502341617181920970212223242541311A B ⎡⎤⎡⎤⎢⎥⎢⎥-⎢⎥⎢⎥⎢⎥⎢⎥==-⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦(1) 求它们的乘积C 。

(2) 将矩阵C 的右下角3×2子矩阵赋给D 。

(3) 查看MATLAB 工作空间的使用情况。

4. 完成下列操作:(1) 求[100,999]之间能被21整除的数的个数。

(2) 建立一个字符串向量,删除其中的大写字母。

解:(1) 结果:(2). 建立一个字符串向量 例如:ch='ABC123d4e56Fg9';则要求结果是:实验二 MATLAB 矩阵分析与处理1. 设有分块矩阵33322322E R A O S ⨯⨯⨯⨯⎡⎤=⎢⎥⎣⎦,其中E 、R 、O 、S 分别为单位矩阵、随机矩阵、零矩阵和对角阵,试通过数值计算验证22E R RS A OS +⎡⎤=⎢⎥⎣⎦。

Matlab程序设计与应用第二版刘卫国课后实验答案

Matlab程序设计与应用第二版刘卫国课后实验答案

实验一:T1:%%第一小题z1=2*sin(85*pi/180)/(1+exp(2))%%第二小题x=[2,1+2i;—0.45,5];z2=1/2*log(x+sqrt(1+x。

^2));z2%%第三小题a=-3。

0:0。

1:3.0;z3=1/2*(exp(0.3*a)-exp(—0.3*a))。

*sin(a+0.3)+log((0。

3+a)/2)%%第四题t=0:0。

5:2.5z4=(t〉=0&t<1)。

*(t.^2)+(t〉=1&t〈2)。

*(t。

^2—1)+(t〉=2&t〈3).*(t。

^2—2*t+1)T2:A=[12,34,-4;34,7,87;3,65,7]B=[1,3,-1;2,0,3;3,—2,7]disp ('A+6*B=’);disp(A+6*B);disp('A-B+I=’);disp(A—B+eye(3));disp('A*B=’);disp(A*B);disp('A.*B=’);disp(A。

*B);disp('A^3=');disp(A^3);disp('A。

^3=’);disp(A.^3);disp(’A/B=’);disp(A/B);disp(’B\A=');disp(B\A);disp(’[A,B]=');disp([A,B]);disp(’[A([1,3],:);B^2]=');disp([A([1,3],:);B^2]);T3:z=1:25;A=reshape(z,5,5)’;B=[3,0,16;17,-6,9;0,23,-4;9,7,0;4,13,11];C=A*BD=C(3:5,2:3)T4—1:a=100:999;b=find(rem(a,21)==0);c=length(b)T4-2:a=input('请输入一个字符串:’,’s’);b=find(a〉='A’&a〈=’Z’);a(b)=[];disp(a);实验二:T1:E=eye(3),R=rand(3,2),O=zeros (2,3),S=diag([1,2]);A=[E,R;O,S]disp(’A^2=');disp(A^2);disp('[E,R+RS;O,S^2]’);B=[E,R+R*S;O,S^2]T2:H=hilb(5)P=pascal(5)Hh=det(H)Hp=det(P)Th=cond(H)Tp=cond(P)a=abs(Th—1);b=abs(Tp-1);if a>bdisp('帕萨卡矩阵P性能更好');elseif a<bdisp(’希尔伯特矩阵H性能更好’); elsedisp(’两个矩阵性能相同’);endT3:a=1:25;A=reshape(a,5,5)disp(’行列式的值:’);disp(det(A));disp('矩阵的秩:’);disp(rank(A));disp('矩阵的迹:');disp(trace(A));disp(’矩阵的范数:');disp(norm(A));T4:A=[—29,6,18;20,5,12;—8,8,5][V,D]=eig(A)T5:A=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6] B=[0.95,0。

matlab程序设计(第二版)课后作业答案

matlab程序设计(第二版)课后作业答案

一、实验结果:1、利用函数eye,ones,zeros,rand,randn 函数分别产生首先编写脚本函数如下:a1=eye(3)a2=eye(15,8)b1=ones(3)b2=ones(15,8)c1=zeros(3)c2=zeros(15,8)d1=2*rand(3,3)-1d2=2*randn(3,3)+1r1=2*rand(15,8)-1r2=2*randn(15,8)+12、使用函数diag和reshape函数:编写脚本:a1=[8 -7 2];a2=diag(a1);a3=fliplr(a2);a4=[5 3];a5=diag(a4,-1);a6=fliplr(a5);a=a3+a6a1=reshape(a,1,9)b1=[4 5 7];b2=diag(b1);b3=fliplr(b2);b4=[2 0 8];b5=diag(b4);b=b3+b5b6=reshape(b,1,9)3、直接在工作空间里输入:a=5-round(100*rand(50,2))/104、脚本文件:a=[-pi:2*pi/360:pi];y1=sin(a)y2=cos(a)5.、编写脚本:A=rand(10)b=A>0.6[i,j,v]=find(b)c=sum(v)6、编写脚本:A=randn(10)b=-0.5<A<0.5[i,j,v]=find(b);c=sum(v)7、编写实现表中功能的语句:if and(a<1,b<=0.5)语句1elseif and(a<1,b>0.5)语句2elseif and(a>=1,b<=0.5)语句3elseif and(a>=1,b>0.5)语句4elsedisp('error')end8、编写脚本如下:A=2*rand(3)b=A>1c=A.*b;[i,j,v]=find(c);v9、编写脚本函数:A=101*rand(100,3)b=A>100;c=A.*b;[i,j,d]=find(c);d10、为节约空间,用5*5的矩阵来模拟100*100的矩阵:编写脚本函数如下:A=[0 2 0 3 0;0 0 0 0 0;0 8 0 5 0;0 4 0 5 0;0 4 0 2 0]b=any(A);[i,j,v]=find(1-b);A(:,j)=[];B=A';c=any(B);[i,j,v]=find(1-c);B(:,j)=[];A=B'一、实验结果:1、输入脚本函数:x=0:pi/50:2*pi;k=[1 26 51 76 101];x(k)=[];figure(1)subplot(2,2,1)plot(x,sin(x)),grid onsubplot(2,2,2)plot(x,cos(x)),grid onsubplot(2,2,3)plot(x,tan(x)),grid onsubplot(2,2,4)plot(x,cot(x)),grid on2、分别利用title函数给图形加上标题,用text函数给坐标系中的建立文本对象,可将字符串放在图形的指定位置,用xlabel、ylabel函数添加x、y轴的标记,用legend 函数给图形加上插图说明。

Matlab编程与工程应用(第二版)习题解答(全)

Matlab编程与工程应用(第二版)习题解答(全)

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.。

MATLAB程序设计教程(第二版)课后答案

MATLAB程序设计教程(第二版)课后答案
t=linspace(0,T,N);
h=exp(-t);
dt=t(2)-t(1);
f=1/dt;
X=fft(t);
F=X(1:N/2+1);
f=f*(0:N/2)/N;
plot(f,abs(F),'-*')
4
P=[2,-3,0,5,13];
Q=[1,5,8];
p=polyder(P)
q=polyder(P,Q)
end
y1
y2
6
A=[1,1,1,1,1,1;2,2,2,2,2,2;3,3,3,3,3,3;4,4,4,4,4,4;5,5,5,5,5,5;6,6,6,6,6,6];
n=input('请输入n的值:');
if n<=5&n>=0
disp(A([n],:));
elseif n<0
disp(lasterr);
3
t=0:pi/1000:pi;
x=sin(3.*t).*cos(t);
y1=sin(3.*t).*sin(t);
y2=2.*x-0.5;
plot(x,y1,'k',x,y2);
hold on;
k=find(abs(y1-y2)<1e-2);
x1=x(k);
y3=2.*x1-0.5;
plot(x1,y3,'rp');
4
b=[-3.0:0.1:3.0];
for n=1:61
a=b(n);
y(n)=(exp(0.3*a)-exp(-0.3*a))/2*sin(a+0.3)+log((0.3+a)/2);
end
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
y=[exp(x),log(x),sin(x),cos(x)];
%在命令窗口调用上述函数文件:
y=mat1(1+i)
%
function [a,b,N,M]=shiyanwu2(m,n,t)
A=[m*cos(t*pi/180),-m,-sin(t*pi/180),0;m*sin(t*pi/180),0,cos(t*pi/180),0;0,n,-sin(t*pi/180),0;0,0,-cos(t*pi/180),1];
%
for i=1:100
if i==1
f(i)=1;
elseif i==2
f(i)=0;
elseif i==3
f(i)=1;
else
f(i)=f(i-1)-2*f(i-2)+f(i-3);
end
end
max(f)
min(f)
sum(f)
length(find(f>0))
length(find(f==0))
j=10*rem(i,10)+fix(i/10);
if mat3(i)&mat3(j)
disp(i)
end
end
%
function y=fx(x)
y=1./((x-2).^2+0.1)+1./((x-3).^4+0.01);
%在命令窗口调用该函数文件:
y=fx(2)
a=[1,2;3,4];
y=fx(a)
%(4)
t=0:0.5:2.5;
z4=t.^2.*(t>=0&t<1)+(t.^2-1).*(t>=1&t<2)+(t.^2-2*t+1).*(t>=2&t<3)
%
A=[12 34 -4;34 7 87;3 65 7];
B=[1 3 -1;2 0 3;3 -2 7];
A+6*B
A-B+eye(size(A))
y=mat6(40)/(mat6(30)+mat6(20))
%(2)方法二
function f2=mat7(n)
i=1:n;
m=i.*(i+1);
f2=sum(m);
end
%在命令窗口中调用该函数文件如:
y=mat7(40)/(mat7(30)+mat7(20))
%
%
x=linspace(0,2*pi,101);
x=inv(A)*b
%(2):
B=[0.95,0.67,0.53]';
x=inv(A)*B
%(3):
cond(A)
%
A=hilb(4)
A(:,1)=[]
A(4,:)=[]
B=[0.95,0.67,0.52]';
X=inv(A)*B
B1=[0.95,0.67,0.53]';
X1=inv(A)*B1
N=cond(B)
N1=cond(B1)
Na=cond(A) %矩阵A为病态矩阵
%
A=[1,4,9;16,25,36;49,64,81]
B=sqrtm(A)
C=sqrt(A) %sqrtm函数是以矩阵为单位进行计算,sqrt函数是以矩阵中的元素进行计算
%
%
x=[-5.0,-3.0,1.0,2.0,2.5,3.0,5.0];
h=input('该员工工作时数是:');
if h>120
x=(h-120)*84*(1+0.15)+120*84;
elseif h<60
x=h*84-700;
else
x=h*84;
end
disp([num2str(n),'号员工','的应发工资为',num2str(x)]);
%
a=fix(10+(99-10)*rand(1,2)) %产生两个随机整数
MATLAB
%
%
%(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)
y=(0.5+3*sin(x)./(1+x.^2)).*cos(x);
plot(x,y)
%
%(1)
x=linspace(-2*pi,2*pi,100);
y1=x.^2;
y2=cos(2*x);
y3=y1.*y2;
plot(x,y1,'b-',x,y2,'r:',x,y3,'y--');
text(4,16,'\leftarrow y1=x^2');
if x>100|x<0
disp('您输入的成绩不是百分制成绩,请重新输入。');
else
if x<=100&x>=90
disp('A');
elseif x<=89&x>=80
disp('B');
elseif x<=79&x>=70
disp('C');
elseif x<=69&x>60
disp('D');
for i=1:n
s=s+1/i/i;
end
PI=sqrt(6*s)
pi
%
n=input('n=?');
a=1:n;
b=1./a.^2;
PI=sqrt(6*sum(b))
pi
%
y=0;
n=1;
while(y<3)
y=y+1/(2*n-1);
n=n+1;
end
y=y-1/(2*(n-1)-1)
n=n-2
y=[]; %建立存放所有y值的矩阵
for x0=x
if x0<0&x0~=-3
y=[y,x0*x0+x0-6];
elseif x0>=0&x0<5&x0~=2&x0~=3
y=[y,x0*x0-5*x0+6];
else
y=[y,x0*x0-x0-1];
end
end
x %输出所有x
y %输出所有y
%
x=[-5,-3,1,2,2.5,3,5];
subplot(2,2,4);
y1=x.^2;
subplot(2,2,1);%分区
bar(x,y1);
title('y1=x^2的条形图');%设置标题
subplot(2,2,2);
stairs(x,y1);
title('y1=x^2的阶梯图');
subplot(2,2,3);
stem(x,y1);
title('y1=x^2的杆图');
E=eye(3);
R=rand(3,2);
O=zeros(2,3);
S=diag([2,3]);
A=[E,R;O,S];
A^2
B=[E,(R+R*S);O,S^2]
%
H=hilb(5)
P=pascal(5)
Hh=det(H)
Hp=det(P)
Th=cond(H)
Tp=cond(P)
%
A=fix(10*rand(5))
x=a(1);
y=a(2);
t=input('请输入运算符号:','s');
if t=='+'
z=x+y;
elseif t=='-'
z=x-y;
elseif t=='*'
z=x*y;
elseif t=='/'
z=x/y;
end
disp([num2str(x),t,num2str(y),'=',num2str(z)]) %输出运算结果
plot(x,y1);
title('y1=x^2');%设置标题
subplot(1,3,2);
plot(x,y2);
title('y2=cos(2*x)');
subplot(1,3,3);
plot(x,y3);
title('y3=x^2*cos(2*x)');
%(3)
x=linspace(-2*pi,2*pi,20);
length(find(f<0))
%
s=0;n=0;
for i=2:49
b=i*(i+1)-1;
m=fix(sqrt(b));
for j=2:m
if rem(b,j)==0
break
end
end
if j==m
n=n+1;
s=s+b;
end
end
n
s
%
%
function y=mat1(x) %建立函数文件mat1.m
%
a=rand(5,6) %产生5x6的随机矩阵
n=input('请输入您要输出矩阵的第几行:');
相关文档
最新文档