东华大学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程序设计与应用(第二版)实验答案-095

%在命令窗口调用上述函数文件:
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)
matlab程序设计与应用第二版习题答案

matlab程序设计与应用第二版习题答案Matlab程序设计与应用第二版习题答案Matlab是一种强大的数学软件,广泛应用于科学计算、数据分析和工程设计等领域。
《Matlab程序设计与应用》是一本经典的教材,对于学习和掌握Matlab编程语言具有重要的意义。
本文将为大家提供《Matlab程序设计与应用第二版》中部分习题的答案,帮助读者更好地理解和应用Matlab。
第一章:Matlab基础1.1 基本操作1. a = 3; b = 4; c = sqrt(a^2 + b^2); disp(c);2. x = linspace(-pi, pi, 100); y = sin(x); plot(x, y);3. A = [1 2 3; 4 5 6; 7 8 9]; B = [9 8 7; 6 5 4; 3 2 1]; C = A + B; disp(C);1.2 控制结构1. for i = 1:10disp(i);end2. n = 0; sum = 0; while sum < 100n = n + 1;sum = sum + n;enddisp(n);3. x = 5; if x > 0disp('x is positive');elseif x < 0disp('x is negative');elsedisp('x is zero');end第二章:向量和矩阵运算2.1 向量运算1. A = [1 2 3]; B = [4 5 6]; C = A .* B; disp(C);2. A = [1 2 3]; B = [4 5 6]; C = A ./ B; disp(C);3. A = [1 2 3]; B = [4 5 6]; C = dot(A, B); disp(C);2.2 矩阵运算1. A = [1 2 3; 4 5 6]; B = [7 8; 9 10; 11 12]; C = A * B; disp(C);2. A = [1 2 3; 4 5 6]; B = [7 8; 9 10; 11 12]; C = B * A; disp(C);3. A = [1 2 3; 4 5 6]; B = [7 8; 9 10; 11 12]; C = A .* B; disp(C); 第三章:函数和脚本文件3.1 函数1. function y = myfunc(x)y = x^2 + 3*x + 2;end2. function [y1, y2] = myfunc(x1, x2)y1 = x1^2 + 3*x1 + 2;y2 = x2^2 + 3*x2 + 2;end3. function [y1, y2] = myfunc(x)y1 = x^2 + 3*x + 2;y2 = sin(x);end3.2 脚本文件1. x = linspace(0, 2*pi, 100); y = sin(x); plot(x, y);2. x = linspace(-10, 10, 100); y = x.^2 + 3*x + 2; plot(x, y);3. x = linspace(0, 2*pi, 100); y1 = sin(x); y2 = cos(x); plot(x, y1, x, y2);通过以上习题的答案,读者可以对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 程序设计教程第二版实验答案包括第七章

第二章:1.(1) w=sqrt(2)*(1+0.34245*10^(-6))w =1.4142(2) a=3.5;b=5;c=-9.8;x=(2*pi*a+(b+c)/(pi+a*b*c)-exp(2))/(tan(b+c)+a)x =0.9829(3) a=3.32;b=-7.9;y=2*pi*a^2*((1-pi/4)*b-(0.8333-pi/4)*a)y =-128.4271(4) t=[2,1-3i;5,-0.65];>> z=0.5*exp(2*t)*log(t+sqrt(1+t*t))z =1.0e+004 *0.0057 - 0.0007i 0.0049 - 0.0027i1.9884 - 0.3696i 1.7706 - 1.0539i2A=[-1,5,-4;0,7,8;3,61,7];B=[8,3,-1;2,5,3;-3,2,0]; >>(1) A+6*Bans =47 23 -1012 37 26-15 73 7>> A^2-B+eye-18 -216 1823 533 11022 868 526>>(2) A*Bans =14 14 16-10 51 21125 328 180>> A.*Bans =-8 15 40 35 24-9 122 0>> B*Aans =-11 0 -157 228 533 -1 28(3) A/Bans =1.2234 -0.92552.9787 -0.9468 2.3511 -0.9574 4.61703.8723 13.8936>> B\Aans =-0.5106 -8.6170 -1.1277 0.7340 17.5745 1.8085 -0.8830 -21.2128 0.4043>>(4) [A,B]ans =-1 5 -4 8 3 -10 7 8 2 5 33 61 7 -3 2 0>> [A([1,3],:);B^2]ans =-1 5 -43 61 773 37 117 37 13-20 1 9>>3A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14];(1) b=find(A>=10&A<=25);>> A(b)ans =2310(2) B=A(1:3,:)B =23.0000 10.0000 -0.7780 041.0000 -45.0000 65.0000 5.000032.0000 5.0000 0 32.0000C=A(:,1:2)C =23.0000 10.000041.0000 -45.000032.0000 5.00006.0000 -9.5400>> D=A(2:4,3:4)D =65.0000 5.0000 0 32.0000 54.0000 3.1400>> E=B*CE =1.0e+003 *0.9141 -0.22391.20802.71231.1330 -0.2103(3) E<Dans =0 10 00 1>> E&Dans =1 10 11 1>> E|Dans =1 11 11 1~E|~Dans =0 01 00 0第三章:2.(1)c=input('请输入成绩');if(c>=90&c<=100)disp 'A' ;elseif(c>=80)disp 'B';elseif(c>=70)disp 'C';elseif(c>60)disp 'D';elsedisp 'E';end请输入成绩55Ec=input('please enter:'); switch fix(c/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');end>> asplease enter:55E>> asplease enter:67D(2)while(1)c=input('请输入成绩');if(c>=90&c<=100)disp 'A' ;elseif(c<90&c>=80)disp 'B';elseif(c<80&c>=70)disp 'C';elseif(c<70&c>=60)disp 'D';elseif(c<60&c>=0)disp 'E';elsedisp 'wrong'endend>> third请输入成绩113wrong请输入成绩-47wrong请输入成绩45E请输入成绩89B请输入成绩78C3.a=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] b=max(a)c=min(a)b=0c=0for i=1:20if a(i)>bb=a(i);endif a(i)<cc=a(i);endendbc6.A=rand(5,6)n=input('请输入矩阵:');if n>5n=5A(n,n)elseA(n)End6. a=rand(5,6);n=input('请输入n');if n==1disp(a(1,:))endif n==2disp(a(2,:))endif n==3disp(a(3,:))endif n==4disp(a(4,:))endif n==5disp(a(5,:))endif n>5disp(a(5,:))end第四章:1.(1)x=-12:0.001:12;y=x-x.^3/6;plot(x,y);xlabel('x轴');ylabel('y轴');title('Y=X-X^3/3!');(2)ezplot('x^2+2*y^2-64',[-8,8,-8,8])xlabel('x轴');ylabel('y轴');axis equaltitle('x^2+2y^2=64');4.(1)t=0:pi/100:2*pi;x=exp(-t/20).*cos(t);y=exp(-t/20).*sin(t);z=t;plot3(x,y,z)(2)[x,y]=meshgrid(-5:5);z=ones(11);surf(x,y,z);shading interp;第五章1.(1)A=randn(10,5);a=mean(A)a =0.0013 0.2310 0.0253 0.3588 -0.4197b=std(A)b =0.9034 0.8829 1.1898 0.7832 1.0821(2)max(max(A))ans =2.1832>> min(min(A))ans =-2.1707(3).sum(sum(A))ans =1.9666sum(A,1)ans =0.0128 2.3095 0.2528 3.5884 -4.1969(4).sort(A)ans =-1.6656 -0.8323 -1.5937 -1.2025 -2.1707-1.1465 -0.5883 -1.4410 -0.3999 -1.6041-0.4326 -0.1867 -1.3362 -0.1567 -1.0565-0.0376 -0.1364 -0.6918 -0.0198 -0.92190.1253 -0.0956 0.2944 0.6686 -0.80510.1746 0.0593 0.5711 0.6900 -0.05920.2877 0.1139 0.7143 0.7119 0.21930.3273 0.7258 0.8580 0.8156 0.25731.1892 1.0668 1.2540 1.1908 0.52871.19092.1832 1.6236 1.2902 1.4151>> sort(ans,2,'descend')ans =-0.8323 -1.2025 -1.5937 -1.6656 -2.1707-0.3999 -0.5883 -1.1465 -1.4410 -1.6041-0.1567 -0.1867 -0.4326 -1.0565 -1.3362-0.0198 -0.0376 -0.1364 -0.6918 -0.92190.6686 0.2944 0.1253 -0.0956 -0.80510.6900 0.5711 0.1746 0.0593 -0.05920.7143 0.7119 0.2877 0.2193 0.11390.8580 0.8156 0.7258 0.3273 0.25731.2540 1.1908 1.1892 1.0668 0.52872.1832 1.6236 1.4151 1.2902 1.19092.(2)n=1:10;a=n.*n;interp1(n,a,2.2,'cubic')ans =4.8133第七章(1)g=inline('sin(x)./x');>> [s,n]=quad(g,0,2)s =1.6054n =18(2)g=inline('1./((x-0.3).*(x-0.3)+0.01)-1./((x-0.9).*(x-0.9)+0.04)-6'); >> [s,n]=quad(g,0,1)s =11.7006n =145。
(完整版)MATLAB数学实验第二版答案(胡良剑)

数学实验答案Chapter 1Page20,ex1(5) 等于[exp(1),exp(2);exp(3),exp(4)](7) 3=1*3, 8=2*4(8) a为各列最小值,b为最小值所在的行号(10) 1>=4,false, 2>=3,false, 3>=2, ture, 4>=1,ture(11) 答案表明:编址第2元素满足不等式(30>=20)和编址第4元素满足不等式(40>=10)(12) 答案表明:编址第2行第1列元素满足不等式(30>=20)和编址第2行第2列元素满足不等式(40>=10)Page20, ex2(1)a, b, c的值尽管都是1,但数据类型分别为数值,字符,逻辑,注意a与c相等,但他们不等于b(2)double(fun)输出的分别是字符a,b,s,(,x,)的ASCII码Page20,ex3>> r=2;p=0.5;n=12;>> T=log(r)/n/log(1+0.01*p)Page20,ex4>> x=-2:0.05:2;f=x.^4-2.^x;>> [fmin,min_index]=min(f)最小值最小值点编址>> x(min_index)ans =0.6500 最小值点>> [f1,x1_index]=min(abs(f)) 求近似根--绝对值最小的点f1 =0.0328x1_index =24>> x(x1_index)ans =-0.8500>> x(x1_index)=[];f=x.^4-2.^x; 删去绝对值最小的点以求函数绝对值次小的点>> [f2,x2_index]=min(abs(f)) 求另一近似根--函数绝对值次小的点f2 =0.0630x2_index =65>> x(x2_index)ans =1.2500>> z=magic(10)z =92 99 1 8 15 67 74 51 58 4098 80 7 14 16 73 55 57 64 414 81 88 20 22 54 56 63 70 4785 87 19 21 3 60 62 69 71 2886 93 25 2 9 61 68 75 52 3417 24 76 83 90 42 49 26 33 6523 5 82 89 91 48 30 32 39 6679 6 13 95 97 29 31 38 45 7210 12 94 96 78 35 37 44 46 5311 18 100 77 84 36 43 50 27 59>> sum(z)>> sum(diag(z))>> z(:,2)/sqrt(3)>> z(8,:)=z(8,:)+z(3,:)Chapter 2Page 45 ex1先在编辑器窗口写下列M函数,保存为eg2_1.m function [xbar,s]=ex2_1(x)n=length(x);xbar=sum(x)/n;s=sqrt((sum(x.^2)-n*xbar^2)/(n-1));例如>>x=[81 70 65 51 76 66 90 87 61 77];>>[xbar,s]=ex2_1(x)Page 45 ex2s=log(1);n=0;while s<=100n=n+1;s=s+log(1+n);endm=nPage 40 ex3clear;F(1)=1;F(2)=1;k=2;x=0;e=1e-8; a=(1+sqrt(5))/2;while abs(x-a)>ek=k+1;F(k)=F(k-1)+F(k-2); x=F(k)/F(k-1);enda,x,k计算至k=21可满足精度clear;tic;s=0;for i=1:1000000s=s+sqrt(3)/2^i;ends,toctic;s=0;i=1;while i<=1000000s=s+sqrt(3)/2^i;i=i+1;ends,toctic;s=0;i=1:1000000;s=sqrt(3)*sum(1./2.^i);s,tocPage 45 ex5t=0:24;c=[15 14 14 14 14 15 16 18 20 22 23 25 28 ...31 32 31 29 27 25 24 22 20 18 17 16];plot(t,c)Page 45 ex6(1)x=-2:0.1:2;y=x.^2.*sin(x.^2-x-2);plot(x,y)y=inline('x^2*sin(x^2-x-2)');fplot(y,[-2 2]) (2)参数方法t=linspace(0,2*pi,100);x=2*cos(t);y=3*sin(t); plot(x,y)(3)x=-3:0.1:3;y=x;[x,y]=meshgrid(x,y);z=x.^2+y.^2;surf(x,y,z)(4)x=-3:0.1:3;y=-3:0.1:13;[x,y]=meshgrid(x,y);z=x.^4+3*x.^2+y.^2-2*x-2*y-2*x.^2.*y+6;surf(x,y,z)(5)t=0:0.01:2*pi;x=sin(t);y=cos(t);z=cos(2*t);plot3(x,y,z)(6)theta=linspace(0,2*pi,50);fai=linspace(0,pi/2,20); [theta,fai]=meshgrid(theta,fai);x=2*sin(fai).*cos(theta);y=2*sin(fai).*sin(theta);z=2*cos(fai);surf(x,y,z)(7)x=linspace(0,pi,100);y1=sin(x);y2=sin(x).*sin(10*x);y3=-sin(x);plot(x,y1,x,y2,x,y3)page45, ex7x=-1.5:0.05:1.5;y=1.1*(x>1.1)+x.*(x<=1.1).*(x>=-1.1)-1.1*(x<-1.1);plot(x,y)page45,ex9clear;close;x=-2:0.1:2;y=x;[x,y]=meshgrid(x,y);a=0.5457;b=0.7575;p=a*exp(-0.75*y.^2-3.75*x.^2-1.5*x).*(x+y>1);p=p+b*exp(-y.^2-6*x.^2).*(x+y>-1).*(x+y<=1);p=p+a*exp(-0.75*y.^2-3.75*x.^2+1.5*x).*(x+y<=-1);mesh(x,y,p)page45, ex10lookfor lyapunovhelp lyap>> A=[1 2 3;4 5 6;7 8 0];C=[2 -5 -22;-5 -24 -56;-22 -56 -16];>> X=lyap(A,C)X =1.0000 -1.0000 -0.0000-1.0000 2.0000 1.0000-0.0000 1.0000 7.0000Chapter 3Page65 Ex1>> a=[1,2,3];b=[2,4,3];a./b,a.\b,a/b,a\bans =0.5000 0.5000 1.0000ans =2 2 1ans =0.6552 一元方程组x[2,4,3]=[1,2,3]的近似解ans =0 0 00 0 00.6667 1.3333 1.0000矩阵方程[1,2,3][x11,x12,x13;x21,x22,x23;x31,x32,x33]=[2,4,3]的特解Page65 Ex 2(1)>> A=[4 1 -1;3 2 -6;1 -5 3];b=[9;-2;1];>> rank(A), rank([A,b]) [A,b]为增广矩阵ans =3ans =3 可见方程组唯一解>> x=A\bx =2.38301.48942.0213(2)>> A=[4 -3 3;3 2 -6;1 -5 3];b=[-1;-2;1];>> rank(A), rank([A,b])ans =3ans =3 可见方程组唯一解>> x=A\bx =-0.4706-0.2941(3)>> A=[4 1;3 2;1 -5];b=[1;1;1];>> rank(A), rank([A,b])ans =2ans =3 可见方程组无解>> x=A\bx =0.3311-0.1219 最小二乘近似解(4)>> a=[2,1,-1,1;1,2,1,-1;1,1,2,1];b=[1 2 3]';%注意b的写法>> rank(a),rank([a,b])ans =3ans =3 rank(a)==rank([a,b])<4说明有无穷多解>> a\bans =110 一个特解Page65 Ex3>> a=[2,1,-1,1;1,2,1,-1;1,1,2,1];b=[1,2,3]';>> x=null(a),x0=a\bx =-0.62550.6255-0.20850.4170x0 =11通解kx+x0Page65 Ex 4>> x0=[0.2 0.8]';a=[0.99 0.05;0.01 0.95];>> x1=a*x, x2=a^2*x, x10=a^10*x>> x=x0;for i=1:1000,x=a*x;end,xx =0.83330.1667>> x0=[0.8 0.2]';>> x=x0;for i=1:1000,x=a*x;end,xx =0.83330.1667>> [v,e]=eig(a)v =0.9806 -0.70710.1961 0.7071e =1.0000 00 0.9400>> v(:,1)./xans =1.17671.1767 成比例,说明x是最大特征值对应的特征向量Page65 Ex5用到公式(3.11)(3.12)>> B=[6,2,1;2.25,1,0.2;3,0.2,1.8];x=[25 5 20]'; >> C=B/diag(x)C =0.2400 0.4000 0.05000.0900 0.2000 0.01000.1200 0.0400 0.0900>> A=eye(3,3)-CA =0.7600 -0.4000 -0.0500-0.0900 0.8000 -0.0100-0.1200 -0.0400 0.9100>> D=[17 17 17]';x=A\Dx =37.569625.786224.7690Page65 Ex 6(1)>> a=[4 1 -1;3 2 -6;1 -5 3];det(a),inv(a),[v,d]=eig(a) ans =-94ans =0.2553 -0.0213 0.04260.1596 -0.1383 -0.22340.1809 -0.2234 -0.0532v =0.0185 -0.9009 -0.3066-0.7693 -0.1240 -0.7248-0.6386 -0.4158 0.6170d =-3.0527 0 00 3.6760 00 0 8.3766(2)>> a=[1 1 -1;0 2 -1;-1 2 0];det(a),inv(a),[v,d]=eig(a) ans =1ans =2.0000 -2.0000 1.00001.0000 -1.0000 1.00002.0000 -3.0000 2.0000v =-0.5773 0.5774 + 0.0000i 0.5774 - 0.0000i-0.5773 0.5774 0.5774-0.5774 0.5773 - 0.0000i 0.5773 + 0.0000id =1.0000 0 00 1.0000 + 0.0000i 00 0 1.0000 - 0.0000i(3)>> A=[5 7 6 5;7 10 8 7;6 8 10 9;5 7 9 10]A =5 76 57 10 8 76 8 10 95 7 9 10>> det(A),inv(A), [v,d]=eig(A)ans =1ans =68.0000 -41.0000 -17.0000 10.0000-41.0000 25.0000 10.0000 -6.0000-17.0000 10.0000 5.0000 -3.000010.0000 -6.0000 -3.0000 2.0000v =0.8304 0.0933 0.3963 0.3803-0.5016 -0.3017 0.6149 0.5286-0.2086 0.7603 -0.2716 0.55200.1237 -0.5676 -0.6254 0.5209d =0.0102 0 0 00 0.8431 0 00 0 3.8581 00 0 0 30.2887(4)(以n=5为例)方法一(三个for)n=5;for i=1:n, a(i,i)=5;endfor i=1:(n-1),a(i,i+1)=6;endfor i=1:(n-1),a(i+1,i)=1;enda方法二(一个for)n=5;a=zeros(n,n);a(1,1:2)=[5 6];for i=2:(n-1),a(i,[i-1,i,i+1])=[1 5 6];enda(n,[n-1 n])=[1 5];a方法三(不用for)n=5;a=diag(5*ones(n,1));b=diag(6*ones(n-1,1));c=diag(ones(n-1,1));a=a+[zeros(n-1,1),b;zeros(1,n)]+[zeros(1,n);c,zeros(n-1,1)] 下列计算>> det(a)ans =665>> inv(a)ans =0.3173 -0.5865 1.0286 -1.6241 1.9489-0.0977 0.4887 -0.8571 1.3534 -1.62410.0286 -0.1429 0.5429 -0.8571 1.0286-0.0075 0.0376 -0.1429 0.4887 -0.58650.0015 -0.0075 0.0286 -0.0977 0.3173>> [v,d]=eig(a)v =-0.7843 -0.7843 -0.9237 0.9860 -0.92370.5546 -0.5546 -0.3771 -0.0000 0.3771-0.2614 -0.2614 0.0000 -0.1643 0.00000.0924 -0.0924 0.0628 -0.0000 -0.0628-0.0218 -0.0218 0.0257 0.0274 0.0257d =0.7574 0 0 0 00 9.2426 0 0 00 0 7.4495 0 00 0 0 5.0000 00 0 0 0 2.5505Page65 Ex 7(1)>> a=[4 1 -1;3 2 -6;1 -5 3];[v,d]=eig(a)v =0.0185 -0.9009 -0.3066-0.7693 -0.1240 -0.7248-0.6386 -0.4158 0.6170d =-3.0527 0 00 3.6760 00 0 8.3766>> det(v)ans =-0.9255 %v行列式正常, 特征向量线性相关,可对角化>> inv(v)*a*v 验算ans =-3.0527 0.0000 -0.00000.0000 3.6760 -0.0000-0.0000 -0.0000 8.3766>> [v2,d2]=jordan(a) 也可用jordanv2 =0.0798 0.0076 0.91270.1886 -0.3141 0.1256-0.1605 -0.2607 0.4213 特征向量不同d2 =8.3766 0 00 -3.0527 - 0.0000i 00 0 3.6760 + 0.0000i>> v2\a*v2ans =8.3766 0 0.00000.0000 -3.0527 0.00000.0000 0.0000 3.6760>> v(:,1)./v2(:,2) 对应相同特征值的特征向量成比例ans =2.44912.44912.4491(2)>> a=[1 1 -1;0 2 -1;-1 2 0];[v,d]=eig(a)v =-0.5773 0.5774 + 0.0000i 0.5774 - 0.0000i-0.5773 0.5774 0.5774-0.5774 0.5773 - 0.0000i 0.5773 + 0.0000id =1.0000 0 00 1.0000 + 0.0000i 00 0 1.0000 - 0.0000i>> det(v)ans =-5.0566e-028 -5.1918e-017i v的行列式接近0, 特征向量线性相关,不可对角化>> [v,d]=jordan(a)v =1 0 11 0 01 -1 0d =1 1 00 1 10 0 1 jordan标准形不是对角的,所以不可对角化(3)>> A=[5 7 6 5;7 10 8 7;6 8 10 9;5 7 9 10]A =5 76 57 10 8 76 8 10 95 7 9 10>> [v,d]=eig(A)v =0.8304 0.0933 0.3963 0.3803-0.5016 -0.3017 0.6149 0.5286-0.2086 0.7603 -0.2716 0.55200.1237 -0.5676 -0.6254 0.5209d =0.0102 0 0 00 0.8431 0 00 0 3.8581 00 0 0 30.2887>> inv(v)*A*vans =0.0102 0.0000 -0.0000 0.00000.0000 0.8431 -0.0000 -0.0000-0.0000 0.0000 3.8581 -0.0000-0.0000 -0.0000 0 30.2887本题用jordan不行, 原因未知(4)参考6(4)和7(1)Page65 Exercise 8只有(3)对称, 且特征值全部大于零, 所以是正定矩阵. Page65 Exercise 9(1)>> a=[4 -3 1 3;2 -1 3 5;1 -1 -1 -1;3 -2 3 4;7 -6 -7 0]>> rank(a)ans =3>> rank(a(1:3,:))ans =2>> rank(a([1 2 4],:)) 1,2,4行为最大无关组ans =3>> b=a([1 2 4],:)';c=a([3 5],:)';>> b\c 线性表示的系数ans =0.5000 5.0000-0.5000 1.00000 -5.0000Page65 Exercise 10>> a=[1 -2 2;-2 -2 4;2 4 -2]>> [v,d]=eig(a)v =0.3333 0.9339 -0.12930.6667 -0.3304 -0.6681-0.6667 0.1365 -0.7327d =-7.0000 0 00 2.0000 00 0 2.0000>> v'*vans =1.0000 0.0000 0.00000.0000 1.0000 00.0000 0 1.0000 v确实是正交矩阵Page65 Exercise 11设经过6个电阻的电流分别为i1, ..., i6. 列方程组如下20-2i1=a; 5-3i2=c; a-3i3=c; a-4i4=b; c-5i5=b; b-3i6=0;i1=i3+i4;i5=i2+i3;i6=i4+i5;计算如下>> A=[1 0 0 2 0 0 0 0 0;0 0 1 0 3 0 0 0 0;1 0 -1 0 0 -3 0 0 0; 1 -1 0 0 0 0 -4 0 0;0 -1 1 0 0 0 0 -5 0;0 1 0 0 0 0 0 0 -3; 0 0 0 1 0 -1 -1 0 0;0 0 0 0 -1 -1 0 1 0;0 0 0 0 0 0 -1 -1 1];>>b=[20 5 0 0 0 0 0 0 0]'; A\bans =13.34536.44018.54203.3274-1.18071.60111.72630.42042.1467Page65 Exercise 12>> A=[1 2 3;4 5 6;7 8 0];>> left=sum(eig(A)), right=sum(trace(A))left =6.0000right =6>> left=prod(eig(A)), right=det(A) 原题有错, (-1)^n应删去left =27.0000right =27>> fA=(A-p(1)*eye(3,3))*(A-p(2)*eye(3,3))*(A-p(3)*eye(3,3)) fA =1.0e-012 *0.0853 0.1421 0.02840.1421 0.1421 0-0.0568 -0.1137 0.1705>> norm(fA) f(A)范数接近0ans =2.9536e-013Chapter 4Page84 Exercise 1(1)roots([1 1 1])(2)roots([3 0 -4 0 2 -1])(3)p=zeros(1,24);p([1 17 18 22])=[5 -6 8 -5];roots(p)(4)p1=[2 3];p2=conv(p1, p1);p3=conv(p1, p2);p3(end)=p3(end)-4; %原p3最后一个分量-4roots(p3)Page84 Exercise 2fun=inline('x*log(sqrt(x^2-1)+x)-sqrt(x^2-1)-0.5*x');fzero(fun,2)Page84 Exercise 3fun=inline('x^4-2^x');fplot(fun,[-2 2]);grid on;fzero(fun,-1),fzero(fun,1),fminbnd(fun,0.5,1.5)Page84 Exercise 4fun=inline('x*sin(1/x)','x');fplot(fun, [-0.1 0.1]);x=zeros(1,10);for i=1:10, x(i)=fzero(fun,(i-0.5)*0.01);end;x=[x,-x]Page84 Exercise 5fun=inline('[9*x(1)^2+36*x(2)^2+4*x(3)^2-36;x(1)^2-2*x(2)^2-20*x(3);16*x(1)-x(1)^3-2*x(2)^ 2-16*x(3)^2]','x');[a,b,c]=fsolve(fun,[0 0 0])Page84 Exercise 6fun=@(x)[x(1)-0.7*sin(x(1))-0.2*cos(x(2)),x(2)-0.7*cos(x(1))+0.2*sin(x(2))];[a,b,c]=fsolve(fun,[0.5 0.5])Page84 Exercise 7clear; close; t=0:pi/100:2*pi;x1=2+sqrt(5)*cos(t); y1=3-2*x1+sqrt(5)*sin(t);x2=3+sqrt(2)*cos(t); y2=6*sin(t);plot(x1,y1,x2,y2); grid on; 作图发现4个解的大致位置,然后分别求解y1=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[1.5,2])y2=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[1.8,-2])y3=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[3.5,-5])y4=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[4,-4])Page84 Exercise 8(1)clear;fun=inline('x.^2.*sin(x.^2-x-2)');fplot(fun,[-2 2]);grid on; 作图观察x(1)=-2;x(3)=fminbnd(fun,-1,-0.5);x(5)=fminbnd(fun,1,2);fun2=inline('-x.^2.*sin(x.^2-x-2)');x(2)=fminbnd(fun2,-2,-1);x(4)=fminbnd(fun2,-0.5,0.5);x(6)=2feval(fun,x)答案: 以上x(1)(3)(5)是局部极小,x(2)(4)(6)是局部极大,从最后一句知道x(1)全局最小,x(2)最大。
matlab第二版习题答案

matlab第二版习题答案Matlab是一种强大的数学软件工具,被广泛应用于科学计算、数据分析和工程设计等领域。
对于学习和掌握Matlab的人来说,习题是不可或缺的一部分。
本文将为大家提供Matlab第二版习题的答案,帮助读者更好地理解和应用Matlab。
第一章:基本操作1.1 Matlab的启动和退出启动Matlab的方法有多种,可以通过桌面图标、命令行或者启动器来打开Matlab。
退出Matlab可以直接关闭窗口或者使用命令"exit"。
1.2 Matlab的基本语法Matlab的基本语法与其他编程语言相似,包括变量的定义、运算符的使用、条件语句和循环语句等。
例如,定义一个变量x并赋值为5可以使用语句"x = 5;"。
1.3 Matlab的数据类型Matlab支持多种数据类型,包括数值型、字符型和逻辑型等。
数值型可以是整数或者浮点数,字符型用单引号或双引号表示,逻辑型只有两个值true和false。
第二章:向量和矩阵操作2.1 向量的定义和运算向量是一维数组,可以通过一对方括号来定义。
Matlab提供了丰富的向量运算函数,如加法、减法、乘法和除法等。
2.2 矩阵的定义和运算矩阵是二维数组,可以通过方括号和分号来定义。
Matlab提供了矩阵的加法、减法、乘法、转置和求逆等运算。
2.3 矩阵的索引和切片可以使用索引和切片来访问矩阵中的元素。
索引从1开始,可以使用冒号表示全部元素。
切片可以用来选择矩阵的一部分。
第三章:函数和脚本文件3.1 函数的定义和调用函数是一段独立的代码块,可以接受输入参数并返回输出结果。
在Matlab中,函数的定义以关键字"function"开头,调用函数使用函数名和参数。
3.2 脚本文件的编写和运行脚本文件是一系列Matlab语句的集合,可以保存为.m文件。
通过运行脚本文件,可以一次性执行多个语句,提高效率。
第四章:图形绘制和数据可视化4.1 图形绘制函数Matlab提供了丰富的图形绘制函数,可以绘制线图、散点图、柱状图等。
matlab第二版课后习题答案

matlab第二版课后习题答案
《MATLAB第二版课后习题答案》
MATLAB是一种强大的数学软件,被广泛应用于工程、科学和金融等领域。
《MATLAB第二版》是一本经典的教材,为了帮助学生更好地掌握MATLAB的使用,书中提供了大量的课后习题。
下面我们将为大家总结一些MATLAB第二版课后习题的答案,希望能对大家的学习有所帮助。
1. 第一章课后习题答案
第一章主要介绍了MATLAB的基本操作,包括变量的定义、矩阵的运算、函数的使用等。
在课后习题中,有一道题目是要求计算一个矩阵的逆矩阵。
答案是使用MATLAB中的inv函数,将原矩阵作为参数传入即可得到逆矩阵。
2. 第二章课后习题答案
第二章介绍了MATLAB中的绘图功能,包括二维和三维图形的绘制。
有一道课后习题是要求绘制一个正弦曲线和余弦曲线,并在同一张图上显示。
答案是使用MATLAB中的plot函数,分别绘制正弦曲线和余弦曲线,并使用legend函数添加图例。
3. 第三章课后习题答案
第三章介绍了MATLAB中的控制流程,包括if语句、for循环和while循环等。
有一道课后习题是要求编写一个程序,计算1到100之间所有偶数的和。
答案是使用for循环遍历1到100之间的所有数,判断是否为偶数并累加。
通过以上几个例子,我们可以看到MATLAB第二版课后习题的答案涵盖了各种基本和高级的操作,对于学习MATLAB是非常有帮助的。
希望大家在学习MATLAB的过程中能够多加练习,掌握更多的技巧和方法。
MATLAB数学实验第二版课后练习题含答案

MATLAB数学实验第二版课后练习题含答案课后练习题MATLAB数学实验第二版的课后练习题如下:第一章课后练习题1.编写MATLAB程序,计算并输出下列公式的结果:y = \\frac{1}{\\sqrt{2\\pi\\sigma^2}} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}其中,x, $\\mu$, $\\sigma$ 分别由用户输入。
要求输出结果精确至小数点后两位。
答案如下:x=input('请输入 x 的值:');mu=input('请输入 mu 的值:');sigma=input('请输入 sigma 的值:');y=1/sqrt(2*pi*sigma^2) *exp(-(x-mu)^2/ (2*sigma^2));fprintf('y = %.2f\', y);2.编写MATLAB程序,求解下列方程的解:4x + y = 11\\\\x + 2y = 7答案如下:A= [4,1;1,2];B= [11;7];X=inv(A) *B;fprintf('x = %.2f, y = %.2f\', X(1), X(2));第二章课后练习题1.编写MATLAB程序,计算下列多项式的值:P(x) = x^4 - 2x^3 + 3x^2 - x + 1其中,x 由用户输入。
要求输出结果精确至小数点后两位。
答案如下:x=input('请输入 x 的值:');y=x^4-2*x^3+3*x^2-x+1;fprintf('P(%.2f) = %.2f\', x, y);2.编写MATLAB程序,绘制下列函数的图像:f(x) = \\begin{cases} x + 1, & x < 0 \\\\ x^2, & 0 \\leq x < 1 \\\\ 2x - 1, & x \\geq 1 \\end{cases}答案如下:x=-2:0.01:2;y1=x+1;y2=x.^2.* ((x>=0) & (x<1));y3=2*x-1;plot(x,y1,x,y2,x,y3);legend('y1 = x + 1','y2 = x^2','y3 = 2x - 1');总结本文提供了《MATLAB数学实验第二版》的部分课后练习题及其答案。
数学实验(matlab)样题及参考解答

东华大学高等数学实验试题A考试时间:90分钟(附参考解答)班级 学号 姓名 得分 上机考试说明:1. 开考前可将准备程序拷到硬盘, 开考后不允许用移动盘,也不允许上网;2. 领座考生试卷不同,开卷,可利用自己备用的书和其他资料,但不允许讨论,也不允许借用其他考生的书和资料。
3. 解答(指令行,答案等)全部用笔写在考卷上。
一、 计算题(70分)要求:写出M 函数(如果需要的话)、MATLAB 指令和计算结果。
1. 解线性方程组⎪⎪⎩⎪⎪⎨⎧-=+=+--=-+=-+14235231543421431321x x x x x x x x x x x 并求系数矩阵的行列式。
指令行:A=[5 1 –1 0;1 0 3 –1;-1 –1 0 5;0 0 2 4];b=[1;2;3;-1]; x=A\b,d=det(A) 结果:x 1=1.4, x 2= -5.9, x 3=0.1, x 4= -0.3. 行列式=70.2. 设 f(x,y) = 4 sin (x 3y),求 3,22==∂∂∂y x y x f 。
指令行:syms x y; f=diff(4*sin(x^3*y),x); f=diff(f,y); f=subs(f,x,2); f=subs(f,y,3)结果:1063.63. 求方程 3x 4+4x 3-20x+5 = 0 的所有解。
指令行:roots([3 4 0 –20 5])结果:-1.5003 - 1.5470i, -1.5003 + 1.5470i, 1.4134, 0.25394. 使用两种方法求积分dx e x 210221-⎰π的近似值。
方法一:指令行:syms x; s=int(1/sqrt(2*pi)*exp(-x^2/2),0,1); vpa(s,5)结果:0.34135方法二:指令行:x=0:0.01:1; y=1/sqrt(2*pi)*exp(-x.^2/2);trapz(x,y)结果:0.3413方法三:M 函数ex4fun.mfunction f=ex4fun(x)f=1/sqrt(2*pi)*exp(-x.^2/2);指令行:s=quadl(@ex4fun,0,1)结果:0.34135. 求函数 f(x,y) = 3x 2+10y 2+3xy-3x +2y 在原点附近的一个极小值点和极小值。
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];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);endy5y1=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);end7(1)f=[];f(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);taylor(f1,x,5,0)taylor(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')思考练习syms 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);taylor(f,x,3,0)syms xf=sin(x)^2;taylor(f,x,5,0)9syms xx=solve('log(1+x)-5/(1+sin(x))=2','x')syms x y z[x y z]=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程序设计与应用(第二版)课后实验答案

e an dAl l th i ng si nt he i rb ei n ga re go od fo r矩阵和对角阵,试通过数值计算验证。
22E R RS A OS +⎡⎤=⎢⎥⎣⎦解: M 文件如下;由ans,所以22E R RS A O S +⎡⎤=⎢⎥⎣⎦2. 产生5阶希尔伯特矩阵H 和5阶帕斯卡矩阵P ,且求其行列式的值Hh 和Hp 以及它们的条件数Th 和Tp ,判断哪个矩阵性能更好。
为什么?解:M 文件如下:an dAl l t h i ng si nt he i rb ei n ga re go od fo r因为它们的条件数Th>>Tp,所以pascal 矩阵性能更好。
3. 建立一个5×5矩阵,求它的行列式值、迹、秩和范数。
解: M 文件如下:4. 已知2961820512885A -⎡⎤⎢⎥=⎢⎥⎢⎥-⎣⎦求A 的特征值及特征向量,并分析其数学意义。
解:M 文件如图:b ei n ga re go od f数学意义:V 的3个列向量是A 的特征向量,D 的主对角线上3个是A 的特征值,特别的,V 的3个列向量分别是D 的3个特征值的特征向量。
5. 下面是一个线性方程组:111⎡⎤e an dAl l t h i ng si nt he i rb ei n ga re go od fo rs o 输出结果:由结果,X 和X2的值一样,这表示b 的微小变化对方程解也影响较小,而A 的条件数算得较小,所以数值稳定性较好,A 是较好的矩阵。
6. 建立A 矩阵,试比较sqrtm(A)和sqrt(A),分析它们的区别。
解:M 文件如下:分析结果知:sqrtm(A)是类似A 的数值平方根(这可由b1*b1=A 的结果看出),而sqrt(A)则是对A 中的每个元素开根号,两则区别就在于此。
e an dAl l t h i ng si nt he i rb ei n ga re 实验三 选择结构程序设计1. 求分段函数的值。
MATLAB程序设计教程(第二版)课后答案3-8

MA TLAB 第二版课后答案unit2-8第二章1 求下列表达式的值。
(1)w=sqrt(2)*(1+0.34245*10^(-6)) (2) a=3.5; b=5; c=-9.8;x=(2*pi*a+(b+c)/(pi+a*b*c)-exp(2))/tan(b+c)+a (3)a=3.32; b=-7.9;y=2*pi*a^(2)*[(1-pi/4)*b-(0.8333-pi/4)*a](4)t=[2,1-3*i;5,-0.65];z=1/2*exp(2*t)*log(t+sqrt(1+t^(2)))2 求下列表达式A=[-1,5,-4;0,7,8;3,61,7]; B=[8,3,-1;2,5,3;-3,2,0]; (1)A+6*B A^2-B+eye (2)A*B A.*B B.*A (3)A/B B\A(4)[A,B] [A([1,3],:);B^2]3 根据已知,完成下列操作 (1)A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14]; K=find(A>10&A<25); A(K)(2)A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14]; B=A(1:3,:)C=A(:,1:2)D=A(2:4,3:4) E=B*C(3)E<D E&D E|D ~E|~Dunit3 实验指导1、 n=input('请输入一个三位数:'); a=fix(n/100);b=fix((n-a*100)/10); c=n-a*100-b*10; d=c*100+b*10+a 2(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'; otherwise p='E'; endprice=p(2)n=input('请输入成绩'); if n>=90&n<=100 p='A';elseif n>=80&n<=89 p='B';elseif n>=70&n<=79 p='C';elseif n>=60&n<=69 p='D'; elsep='E'; end price=p (3)try n; 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)+l og((0.3+a)/2);endy5y1=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)));endy1y27(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)unit4实验指导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);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)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,l b,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)));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,'-'); %作图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',2sym 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);taylor(f1,x,5,0)taylor(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')[xy]=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')。
东华大学MATLAB数学实验第二版答案(胡良剑)精编版

数学实验答案Chapter 1Page20,ex1(5) 等于[exp(1),exp(2);exp(3),exp(4)](7) 3=1*3, 8=2*4(8) a为各列最小值,b为最小值所在的行号(10) 1>=4,false, 2>=3,false, 3>=2, ture, 4>=1,ture(11) 答案表明:编址第2元素满足不等式(30>=20)和编址第4元素满足不等式(40>=10)(12) 答案表明:编址第2行第1列元素满足不等式(30>=20)和编址第2行第2列元素满足不等式(40>=10)Page20, ex2(1)a, b, c的值尽管都是1,但数据类型分别为数值,字符,逻辑,注意a与c相等,但他们不等于b(2)double(fun)输出的分别是字符a,b,s,(,x,)的ASCII码Page20,ex3>> r=2;p=0.5;n=12;>> T=log(r)/n/log(1+0.01*p)Page20,ex4>> x=-2:0.05:2;f=x.^4-2.^x;>> [fmin,min_index]=min(f)最小值最小值点编址>> x(min_index)ans =0.6500 最小值点>> [f1,x1_index]=min(abs(f)) 求近似根--绝对值最小的点f1 =0.0328x1_index =24>> x(x1_index)ans =-0.8500>> x(x1_index)=[];f=x.^4-2.^x; 删去绝对值最小的点以求函数绝对值次小的点>> [f2,x2_index]=min(abs(f)) 求另一近似根--函数绝对值次小的点f2 =0.0630x2_index =65>> x(x2_index)ans =1.2500>> z=magic(10)z =92 99 1 8 15 67 74 51 58 4098 80 7 14 16 73 55 57 64 414 81 88 20 22 54 56 63 70 4785 87 19 21 3 60 62 69 71 2886 93 25 2 9 61 68 75 52 3417 24 76 83 90 42 49 26 33 6523 5 82 89 91 48 30 32 39 6679 6 13 95 97 29 31 38 45 7210 12 94 96 78 35 37 44 46 5311 18 100 77 84 36 43 50 27 59>> sum(z)>> sum(diag(z))>> z(:,2)/sqrt(3)>> z(8,:)=z(8,:)+z(3,:)Chapter 2Page 45 ex1先在编辑器窗口写下列M函数,保存为eg2_1.m function [xbar,s]=ex2_1(x)n=length(x);xbar=sum(x)/n;s=sqrt((sum(x.^2)-n*xbar^2)/(n-1));例如>>x=[81 70 65 51 76 66 90 87 61 77];>>[xbar,s]=ex2_1(x)Page 45 ex2s=log(1);n=0;while s<=100n=n+1;s=s+log(1+n);endm=nPage 40 ex3clear;F(1)=1;F(2)=1;k=2;x=0;e=1e-8; a=(1+sqrt(5))/2;while abs(x-a)>ek=k+1;F(k)=F(k-1)+F(k-2); x=F(k)/F(k-1);enda,x,k计算至k=21可满足精度clear;tic;s=0;for i=1:1000000s=s+sqrt(3)/2^i;ends,toctic;s=0;i=1;while i<=1000000s=s+sqrt(3)/2^i;i=i+1;ends,toctic;s=0;i=1:1000000;s=sqrt(3)*sum(1./2.^i);s,tocPage 45 ex5t=0:24;c=[15 14 14 14 14 15 16 18 20 22 23 25 28 ...31 32 31 29 27 25 24 22 20 18 17 16];plot(t,c)Page 45 ex6(1)x=-2:0.1:2;y=x.^2.*sin(x.^2-x-2);plot(x,y)y=inline('x^2*sin(x^2-x-2)');fplot(y,[-2 2]) (2)参数方法t=linspace(0,2*pi,100);x=2*cos(t);y=3*sin(t); plot(x,y)(3)x=-3:0.1:3;y=x;[x,y]=meshgrid(x,y);z=x.^2+y.^2;surf(x,y,z)(4)x=-3:0.1:3;y=-3:0.1:13;[x,y]=meshgrid(x,y);z=x.^4+3*x.^2+y.^2-2*x-2*y-2*x.^2.*y+6;surf(x,y,z)(5)t=0:0.01:2*pi;x=sin(t);y=cos(t);z=cos(2*t);plot3(x,y,z)(6)theta=linspace(0,2*pi,50);fai=linspace(0,pi/2,20); [theta,fai]=meshgrid(theta,fai);x=2*sin(fai).*cos(theta);y=2*sin(fai).*sin(theta);z=2*cos(fai);surf(x,y,z)(7)x=linspace(0,pi,100);y1=sin(x);y2=sin(x).*sin(10*x);y3=-sin(x);plot(x,y1,x,y2,x,y3)page45, ex7x=-1.5:0.05:1.5;y=1.1*(x>1.1)+x.*(x<=1.1).*(x>=-1.1)-1.1*(x<-1.1);plot(x,y)page45,ex9clear;close;x=-2:0.1:2;y=x;[x,y]=meshgrid(x,y);a=0.5457;b=0.7575;p=a*exp(-0.75*y.^2-3.75*x.^2-1.5*x).*(x+y>1);p=p+b*exp(-y.^2-6*x.^2).*(x+y>-1).*(x+y<=1);p=p+a*exp(-0.75*y.^2-3.75*x.^2+1.5*x).*(x+y<=-1);mesh(x,y,p)page45, ex10lookfor lyapunovhelp lyap>> A=[1 2 3;4 5 6;7 8 0];C=[2 -5 -22;-5 -24 -56;-22 -56 -16];>> X=lyap(A,C)X =1.0000 -1.0000 -0.0000-1.0000 2.0000 1.0000-0.0000 1.0000 7.0000Chapter 3Page65 Ex1>> a=[1,2,3];b=[2,4,3];a./b,a.\b,a/b,a\bans =0.5000 0.5000 1.0000ans =2 2 1ans =0.6552 一元方程组x[2,4,3]=[1,2,3]的近似解ans =0 0 00 0 00.6667 1.3333 1.0000矩阵方程[1,2,3][x11,x12,x13;x21,x22,x23;x31,x32,x33]=[2,4,3]的特解Page65 Ex 2(1)>> A=[4 1 -1;3 2 -6;1 -5 3];b=[9;-2;1];>> rank(A), rank([A,b]) [A,b]为增广矩阵ans =3ans =3 可见方程组唯一解>> x=A\bx =2.38301.48942.0213(2)>> A=[4 -3 3;3 2 -6;1 -5 3];b=[-1;-2;1];>> rank(A), rank([A,b])ans =3ans =3 可见方程组唯一解>> x=A\bx =-0.4706-0.2941(3)>> A=[4 1;3 2;1 -5];b=[1;1;1];>> rank(A), rank([A,b])ans =2ans =3 可见方程组无解>> x=A\bx =0.3311-0.1219 最小二乘近似解(4)>> a=[2,1,-1,1;1,2,1,-1;1,1,2,1];b=[1 2 3]';%注意b的写法>> rank(a),rank([a,b])ans =3ans =3 rank(a)==rank([a,b])<4说明有无穷多解>> a\bans =110 一个特解Page65 Ex3>> a=[2,1,-1,1;1,2,1,-1;1,1,2,1];b=[1,2,3]';>> x=null(a),x0=a\bx =-0.62550.6255-0.20850.4170x0 =11通解kx+x0Page65 Ex 4>> x0=[0.2 0.8]';a=[0.99 0.05;0.01 0.95];>> x1=a*x, x2=a^2*x, x10=a^10*x>> x=x0;for i=1:1000,x=a*x;end,xx =0.83330.1667>> x0=[0.8 0.2]';>> x=x0;for i=1:1000,x=a*x;end,xx =0.83330.1667>> [v,e]=eig(a)v =0.9806 -0.70710.1961 0.7071e =1.0000 00 0.9400>> v(:,1)./xans =1.17671.1767 成比例,说明x是最大特征值对应的特征向量Page65 Ex5用到公式(3.11)(3.12)>> B=[6,2,1;2.25,1,0.2;3,0.2,1.8];x=[25 5 20]'; >> C=B/diag(x)C =0.2400 0.4000 0.05000.0900 0.2000 0.01000.1200 0.0400 0.0900>> A=eye(3,3)-CA =0.7600 -0.4000 -0.0500-0.0900 0.8000 -0.0100-0.1200 -0.0400 0.9100>> D=[17 17 17]';x=A\Dx =37.569625.786224.7690Page65 Ex 6(1)>> a=[4 1 -1;3 2 -6;1 -5 3];det(a),inv(a),[v,d]=eig(a) ans =-94ans =0.2553 -0.0213 0.04260.1596 -0.1383 -0.22340.1809 -0.2234 -0.0532v =0.0185 -0.9009 -0.3066-0.7693 -0.1240 -0.7248-0.6386 -0.4158 0.6170d =-3.0527 0 00 3.6760 00 0 8.3766(2)>> a=[1 1 -1;0 2 -1;-1 2 0];det(a),inv(a),[v,d]=eig(a) ans =1ans =2.0000 -2.0000 1.00001.0000 -1.0000 1.00002.0000 -3.0000 2.0000v =-0.5773 0.5774 + 0.0000i 0.5774 - 0.0000i-0.5773 0.5774 0.5774-0.5774 0.5773 - 0.0000i 0.5773 + 0.0000id =1.0000 0 00 1.0000 + 0.0000i 00 0 1.0000 - 0.0000i(3)>> A=[5 7 6 5;7 10 8 7;6 8 10 9;5 7 9 10]A =5 76 57 10 8 76 8 10 95 7 9 10>> det(A),inv(A), [v,d]=eig(A)ans =1ans =68.0000 -41.0000 -17.0000 10.0000-41.0000 25.0000 10.0000 -6.0000-17.0000 10.0000 5.0000 -3.000010.0000 -6.0000 -3.0000 2.0000v =0.8304 0.0933 0.3963 0.3803-0.5016 -0.3017 0.6149 0.5286-0.2086 0.7603 -0.2716 0.55200.1237 -0.5676 -0.6254 0.5209d =0.0102 0 0 00 0.8431 0 00 0 3.8581 00 0 0 30.2887(4)(以n=5为例)方法一(三个for)n=5;for i=1:n, a(i,i)=5;endfor i=1:(n-1),a(i,i+1)=6;endfor i=1:(n-1),a(i+1,i)=1;enda方法二(一个for)n=5;a=zeros(n,n);a(1,1:2)=[5 6];for i=2:(n-1),a(i,[i-1,i,i+1])=[1 5 6];enda(n,[n-1 n])=[1 5];a方法三(不用for)n=5;a=diag(5*ones(n,1));b=diag(6*ones(n-1,1));c=diag(ones(n-1,1));a=a+[zeros(n-1,1),b;zeros(1,n)]+[zeros(1,n);c,zeros(n-1,1)] 下列计算>> det(a)ans =665>> inv(a)ans =0.3173 -0.5865 1.0286 -1.6241 1.9489-0.0977 0.4887 -0.8571 1.3534 -1.62410.0286 -0.1429 0.5429 -0.8571 1.0286-0.0075 0.0376 -0.1429 0.4887 -0.58650.0015 -0.0075 0.0286 -0.0977 0.3173>> [v,d]=eig(a)v =-0.7843 -0.7843 -0.9237 0.9860 -0.92370.5546 -0.5546 -0.3771 -0.0000 0.3771-0.2614 -0.2614 0.0000 -0.1643 0.00000.0924 -0.0924 0.0628 -0.0000 -0.0628-0.0218 -0.0218 0.0257 0.0274 0.0257d =0.7574 0 0 0 00 9.2426 0 0 00 0 7.4495 0 00 0 0 5.0000 00 0 0 0 2.5505Page65 Ex 7(1)>> a=[4 1 -1;3 2 -6;1 -5 3];[v,d]=eig(a)v =0.0185 -0.9009 -0.3066-0.7693 -0.1240 -0.7248-0.6386 -0.4158 0.6170d =-3.0527 0 00 3.6760 00 0 8.3766>> det(v)ans =-0.9255 %v行列式正常, 特征向量线性相关,可对角化>> inv(v)*a*v 验算ans =-3.0527 0.0000 -0.00000.0000 3.6760 -0.0000-0.0000 -0.0000 8.3766>> [v2,d2]=jordan(a) 也可用jordanv2 =0.0798 0.0076 0.91270.1886 -0.3141 0.1256-0.1605 -0.2607 0.4213 特征向量不同d2 =8.3766 0 00 -3.0527 - 0.0000i 00 0 3.6760 + 0.0000i>> v2\a*v2ans =8.3766 0 0.00000.0000 -3.0527 0.00000.0000 0.0000 3.6760>> v(:,1)./v2(:,2) 对应相同特征值的特征向量成比例ans =2.44912.44912.4491(2)>> a=[1 1 -1;0 2 -1;-1 2 0];[v,d]=eig(a)v =-0.5773 0.5774 + 0.0000i 0.5774 - 0.0000i-0.5773 0.5774 0.5774-0.5774 0.5773 - 0.0000i 0.5773 + 0.0000id =1.0000 0 00 1.0000 + 0.0000i 00 0 1.0000 - 0.0000i>> det(v)ans =-5.0566e-028 -5.1918e-017i v的行列式接近0, 特征向量线性相关,不可对角化>> [v,d]=jordan(a)v =1 0 11 0 01 -1 0d =1 1 00 1 10 0 1 jordan标准形不是对角的,所以不可对角化(3)>> A=[5 7 6 5;7 10 8 7;6 8 10 9;5 7 9 10]A =5 76 57 10 8 76 8 10 95 7 9 10>> [v,d]=eig(A)v =0.8304 0.0933 0.3963 0.3803-0.5016 -0.3017 0.6149 0.5286-0.2086 0.7603 -0.2716 0.55200.1237 -0.5676 -0.6254 0.5209d =0.0102 0 0 00 0.8431 0 00 0 3.8581 00 0 0 30.2887>> inv(v)*A*vans =0.0102 0.0000 -0.0000 0.00000.0000 0.8431 -0.0000 -0.0000-0.0000 0.0000 3.8581 -0.0000-0.0000 -0.0000 0 30.2887本题用jordan不行, 原因未知(4)参考6(4)和7(1)Page65 Exercise 8只有(3)对称, 且特征值全部大于零, 所以是正定矩阵. Page65 Exercise 9(1)>> a=[4 -3 1 3;2 -1 3 5;1 -1 -1 -1;3 -2 3 4;7 -6 -7 0]>> rank(a)ans =3>> rank(a(1:3,:))ans =2>> rank(a([1 2 4],:)) 1,2,4行为最大无关组ans =3>> b=a([1 2 4],:)';c=a([3 5],:)';>> b\c 线性表示的系数ans =0.5000 5.0000-0.5000 1.00000 -5.0000Page65 Exercise 10>> a=[1 -2 2;-2 -2 4;2 4 -2]>> [v,d]=eig(a)v =0.3333 0.9339 -0.12930.6667 -0.3304 -0.6681-0.6667 0.1365 -0.7327d =-7.0000 0 00 2.0000 00 0 2.0000>> v'*vans =1.0000 0.0000 0.00000.0000 1.0000 00.0000 0 1.0000 v确实是正交矩阵Page65 Exercise 11设经过6个电阻的电流分别为i1, ..., i6. 列方程组如下20-2i1=a; 5-3i2=c; a-3i3=c; a-4i4=b; c-5i5=b; b-3i6=0;i1=i3+i4;i5=i2+i3;i6=i4+i5;计算如下>> A=[1 0 0 2 0 0 0 0 0;0 0 1 0 3 0 0 0 0;1 0 -1 0 0 -3 0 0 0; 1 -1 0 0 0 0 -4 0 0;0 -1 1 0 0 0 0 -5 0;0 1 0 0 0 0 0 0 -3; 0 0 0 1 0 -1 -1 0 0;0 0 0 0 -1 -1 0 1 0;0 0 0 0 0 0 -1 -1 1];>>b=[20 5 0 0 0 0 0 0 0]'; A\bans =13.34536.44018.54203.3274-1.18071.60111.72630.42042.1467Page65 Exercise 12>> A=[1 2 3;4 5 6;7 8 0];>> left=sum(eig(A)), right=sum(trace(A))left =6.0000right =6>> left=prod(eig(A)), right=det(A) 原题有错, (-1)^n应删去left =27.0000right =27>> fA=(A-p(1)*eye(3,3))*(A-p(2)*eye(3,3))*(A-p(3)*eye(3,3)) fA =1.0e-012 *0.0853 0.1421 0.02840.1421 0.1421 0-0.0568 -0.1137 0.1705>> norm(fA) f(A)范数接近0ans =2.9536e-013Chapter 4Page84 Exercise 1(1)roots([1 1 1])(2)roots([3 0 -4 0 2 -1])(3)p=zeros(1,24);p([1 17 18 22])=[5 -6 8 -5];roots(p)(4)p1=[2 3];p2=conv(p1, p1);p3=conv(p1, p2);p3(end)=p3(end)-4; %原p3最后一个分量-4roots(p3)Page84 Exercise 2fun=inline('x*log(sqrt(x^2-1)+x)-sqrt(x^2-1)-0.5*x');fzero(fun,2)Page84 Exercise 3fun=inline('x^4-2^x');fplot(fun,[-2 2]);grid on;fzero(fun,-1),fzero(fun,1),fminbnd(fun,0.5,1.5)Page84 Exercise 4fun=inline('x*sin(1/x)','x');fplot(fun, [-0.1 0.1]);x=zeros(1,10);for i=1:10, x(i)=fzero(fun,(i-0.5)*0.01);end;x=[x,-x]Page84 Exercise 5fun=inline('[9*x(1)^2+36*x(2)^2+4*x(3)^2-36;x(1)^2-2*x(2)^2-20*x(3);16*x(1)-x(1)^3-2*x(2)^ 2-16*x(3)^2]','x');[a,b,c]=fsolve(fun,[0 0 0])Page84 Exercise 6fun=@(x)[x(1)-0.7*sin(x(1))-0.2*cos(x(2)),x(2)-0.7*cos(x(1))+0.2*sin(x(2))];[a,b,c]=fsolve(fun,[0.5 0.5])Page84 Exercise 7clear; close; t=0:pi/100:2*pi;x1=2+sqrt(5)*cos(t); y1=3-2*x1+sqrt(5)*sin(t);x2=3+sqrt(2)*cos(t); y2=6*sin(t);plot(x1,y1,x2,y2); grid on; 作图发现4个解的大致位置,然后分别求解y1=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[1.5,2])y2=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[1.8,-2])y3=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[3.5,-5])y4=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[4,-4])Page84 Exercise 8(1)clear;fun=inline('x.^2.*sin(x.^2-x-2)');fplot(fun,[-2 2]);grid on; 作图观察x(1)=-2;x(3)=fminbnd(fun,-1,-0.5);x(5)=fminbnd(fun,1,2);fun2=inline('-x.^2.*sin(x.^2-x-2)');x(2)=fminbnd(fun2,-2,-1);x(4)=fminbnd(fun2,-0.5,0.5);x(6)=2feval(fun,x)答案: 以上x(1)(3)(5)是局部极小,x(2)(4)(6)是局部极大,从最后一句知道x(1)全局最小,x(2)最大。
MATLAB程序设计与应用(第二版)课后实验答案

Matlab 课后实验题答案实验一 MATLAB 运算基础1. 先求下列表达式的值,然后显示MATLAB 工作空间的使用情况并保存全部变量。
(1) 0122sin851z e=+ (2) 221ln(1)2z x x =++,其中2120.455i x +⎡⎤=⎢⎥-⎣⎦ (3) 0.30.330.3sin(0.3)ln , 3.0, 2.9,,2.9,3.022a a e e az a a --+=++=--(4) 2242011122123t t z t t t t t ⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t =0:0.5:2.5 解:M 文件:z1=2*sin(85*pi/180)/(1+exp(2))x=[2 1+2*i;-.45 5];z2=1/2*log(x+sqrt(1+x^2)) 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) t=0:0.5:2.5;z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3) .*(t.^2-2*t+1)2. 已知: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] 解:M 文件: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(3) A*B A.*B A^3 A.^3 A/B B\A [A,B][A([1,3],:);B^2]3. 设有矩阵A 和B123453166789101769,111213141502341617181920970212223242541311A B ⎡⎤⎡⎤⎢⎥⎢⎥-⎢⎥⎢⎥⎢⎥⎢⎥==-⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦(1) 求它们的乘积C 。
Matlab程序设计教程(第二版)课后参考答案

Matlab程序设计教程(第二版)课后参考答案第一章实验1.实验一第1题2.自己验证总结Matlab的主要优点3.实验一第2题4.实验一第3题5.网站思考练习1.启动见书P5 退出见书P62.Matlab主要功能见书P2 4种功能3.分行输入行末尾加续行符,即三个点“…”4.见书P115.直接在命令窗口输入fac第二章实验1.(1) w=sqrt(2)*(1+0.34245*10^(-6))w = 1.4142(2) a=3.5;b=5;c=-9.8;x=(2*pi*a+(b+c)/(pi+a*b*c)-exp(2))/(tan(b+c)+a)x =0.9829(3) a=3.32;b=-7.9;y=2*pi*a^2*((1-pi/4)*b-(0.8333-pi/4)*a)y = -128.4271(4) t=[2,1-3i;5,-0.65];z=0.5*exp(2*t)*log(t+sqrt(1+t.*t))z =1.0e+004 *0.0048 + 0.0002i 0.0048 - 0.0034i1.58992.0090 - 1.3580i2.实验二第1题3.实验二第2题4. H=hilb(5);P=pascal(5);Hh=det(H)Hh = 3.7493e-012Hp=det(P)Hp = 1Th=cond(H)Th = 4.7661e+005Tp=cond(P)Tp = 8.5175e+003条件数越趋近于1,矩阵的性能越好,所以帕斯卡矩阵性能更好。
5. A=[-29,6,18;20,5,12;-8,8,5]A =-29 6 1820 5 12-8 8 5[V,D]=eig(A)V =0.7130 0.2803 0.2733-0.6084 -0.7867 0.87250.3487 0.5501 0.4050D =-25.3169 0 00 -10.5182 00 0 16.8351V为A的特征向量,D为A的特征值。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
数学实验答案Chapter 1Page20,ex1(5) 等于[exp(1),exp(2);exp(3),exp(4)](7) 3=1*3, 8=2*4(8) a为各列最小值,b为最小值所在的行号(10) 1>=4,false, 2>=3,false, 3>=2, ture, 4>=1,ture(11) 答案表明:编址第2元素满足不等式(30>=20)和编址第4元素满足不等式(40>=10)(12) 答案表明:编址第2行第1列元素满足不等式(30>=20)和编址第2行第2列元素满足不等式(40>=10)Page20, ex2(1)a, b, c的值尽管都是1,但数据类型分别为数值,字符,逻辑,注意a与c相等,但他们不等于b(2)double(fun)输出的分别是字符a,b,s,(,x,)的ASCII码Page20,ex3>> r=2;p=0.5;n=12;>> T=log(r)/n/log(1+0.01*p)Page20,ex4>> x=-2:0.05:2;f=x.^4-2.^x;>> [fmin,min_index]=min(f)最小值最小值点编址>> x(min_index)ans =0.6500 最小值点>> [f1,x1_index]=min(abs(f)) 求近似根--绝对值最小的点f1 =0.0328x1_index =24>> x(x1_index)ans =-0.8500>> x(x1_index)=[];f=x.^4-2.^x; 删去绝对值最小的点以求函数绝对值次小的点>> [f2,x2_index]=min(abs(f)) 求另一近似根--函数绝对值次小的点f2 =0.0630x2_index =65>> x(x2_index)ans =1.2500>> z=magic(10)z =92 99 1 8 15 67 74 51 58 4098 80 7 14 16 73 55 57 64 414 81 88 20 22 54 56 63 70 4785 87 19 21 3 60 62 69 71 2886 93 25 2 9 61 68 75 52 3417 24 76 83 90 42 49 26 33 6523 5 82 89 91 48 30 32 39 6679 6 13 95 97 29 31 38 45 7210 12 94 96 78 35 37 44 46 5311 18 100 77 84 36 43 50 27 59>> sum(z)>> sum(diag(z))>> z(:,2)/sqrt(3)>> z(8,:)=z(8,:)+z(3,:)Chapter 2Page 45 ex1先在编辑器窗口写下列M函数,保存为eg2_1.m function [xbar,s]=ex2_1(x)n=length(x);xbar=sum(x)/n;s=sqrt((sum(x.^2)-n*xbar^2)/(n-1));例如>>x=[81 70 65 51 76 66 90 87 61 77];>>[xbar,s]=ex2_1(x)Page 45 ex2s=log(1);n=0;while s<=100n=n+1;s=s+log(1+n);endm=nPage 40 ex3clear;F(1)=1;F(2)=1;k=2;x=0;e=1e-8; a=(1+sqrt(5))/2;while abs(x-a)>ek=k+1;F(k)=F(k-1)+F(k-2); x=F(k)/F(k-1);enda,x,k计算至k=21可满足精度clear;tic;s=0;for i=1:1000000s=s+sqrt(3)/2^i;ends,toctic;s=0;i=1;while i<=1000000s=s+sqrt(3)/2^i;i=i+1;ends,toctic;s=0;i=1:1000000;s=sqrt(3)*sum(1./2.^i);s,tocPage 45 ex5t=0:24;c=[15 14 14 14 14 15 16 18 20 22 23 25 28 ...31 32 31 29 27 25 24 22 20 18 17 16];plot(t,c)Page 45 ex6(1)x=-2:0.1:2;y=x.^2.*sin(x.^2-x-2);plot(x,y)y=inline('x^2*sin(x^2-x-2)');fplot(y,[-2 2]) (2)参数方法t=linspace(0,2*pi,100);x=2*cos(t);y=3*sin(t); plot(x,y)(3)x=-3:0.1:3;y=x;[x,y]=meshgrid(x,y);z=x.^2+y.^2;surf(x,y,z)(4)x=-3:0.1:3;y=-3:0.1:13;[x,y]=meshgrid(x,y);z=x.^4+3*x.^2+y.^2-2*x-2*y-2*x.^2.*y+6;surf(x,y,z)(5)t=0:0.01:2*pi;x=sin(t);y=cos(t);z=cos(2*t);plot3(x,y,z)(6)theta=linspace(0,2*pi,50);fai=linspace(0,pi/2,20); [theta,fai]=meshgrid(theta,fai);x=2*sin(fai).*cos(theta);y=2*sin(fai).*sin(theta);z=2*cos(fai);surf(x,y,z)(7)x=linspace(0,pi,100);y1=sin(x);y2=sin(x).*sin(10*x);y3=-sin(x);plot(x,y1,x,y2,x,y3)page45, ex7x=-1.5:0.05:1.5;y=1.1*(x>1.1)+x.*(x<=1.1).*(x>=-1.1)-1.1*(x<-1.1);plot(x,y)page45,ex9clear;close;x=-2:0.1:2;y=x;[x,y]=meshgrid(x,y);a=0.5457;b=0.7575;p=a*exp(-0.75*y.^2-3.75*x.^2-1.5*x).*(x+y>1);p=p+b*exp(-y.^2-6*x.^2).*(x+y>-1).*(x+y<=1);p=p+a*exp(-0.75*y.^2-3.75*x.^2+1.5*x).*(x+y<=-1);mesh(x,y,p)page45, ex10lookfor lyapunovhelp lyap>> A=[1 2 3;4 5 6;7 8 0];C=[2 -5 -22;-5 -24 -56;-22 -56 -16];>> X=lyap(A,C)X =1.0000 -1.0000 -0.0000-1.0000 2.0000 1.0000-0.0000 1.0000 7.0000Chapter 3Page65 Ex1>> a=[1,2,3];b=[2,4,3];a./b,a.\b,a/b,a\bans =0.5000 0.5000 1.0000ans =2 2 1ans =0.6552 一元方程组x[2,4,3]=[1,2,3]的近似解ans =0 0 00 0 00.6667 1.3333 1.0000矩阵方程[1,2,3][x11,x12,x13;x21,x22,x23;x31,x32,x33]=[2,4,3]的特解Page65 Ex 2(1)>> A=[4 1 -1;3 2 -6;1 -5 3];b=[9;-2;1];>> rank(A), rank([A,b]) [A,b]为增广矩阵ans =3ans =3 可见方程组唯一解>> x=A\bx =2.38301.48942.0213(2)>> A=[4 -3 3;3 2 -6;1 -5 3];b=[-1;-2;1];>> rank(A), rank([A,b])ans =3ans =3 可见方程组唯一解>> x=A\bx =-0.4706-0.2941(3)>> A=[4 1;3 2;1 -5];b=[1;1;1];>> rank(A), rank([A,b])ans =2ans =3 可见方程组无解>> x=A\bx =0.3311-0.1219 最小二乘近似解(4)>> a=[2,1,-1,1;1,2,1,-1;1,1,2,1];b=[1 2 3]';%注意b的写法>> rank(a),rank([a,b])ans =3ans =3 rank(a)==rank([a,b])<4说明有无穷多解>> a\bans =110 一个特解Page65 Ex3>> a=[2,1,-1,1;1,2,1,-1;1,1,2,1];b=[1,2,3]';>> x=null(a),x0=a\bx =-0.62550.6255-0.20850.4170x0 =11通解kx+x0Page65 Ex 4>> x0=[0.2 0.8]';a=[0.99 0.05;0.01 0.95];>> x1=a*x, x2=a^2*x, x10=a^10*x>> x=x0;for i=1:1000,x=a*x;end,xx =0.83330.1667>> x0=[0.8 0.2]';>> x=x0;for i=1:1000,x=a*x;end,xx =0.83330.1667>> [v,e]=eig(a)v =0.9806 -0.70710.1961 0.7071e =1.0000 00 0.9400>> v(:,1)./xans =1.17671.1767 成比例,说明x是最大特征值对应的特征向量Page65 Ex5用到公式(3.11)(3.12)>> B=[6,2,1;2.25,1,0.2;3,0.2,1.8];x=[25 5 20]'; >> C=B/diag(x)C =0.2400 0.4000 0.05000.0900 0.2000 0.01000.1200 0.0400 0.0900>> A=eye(3,3)-CA =0.7600 -0.4000 -0.0500-0.0900 0.8000 -0.0100-0.1200 -0.0400 0.9100>> D=[17 17 17]';x=A\Dx =37.569625.786224.7690Page65 Ex 6(1)>> a=[4 1 -1;3 2 -6;1 -5 3];det(a),inv(a),[v,d]=eig(a) ans =-94ans =0.2553 -0.0213 0.04260.1596 -0.1383 -0.22340.1809 -0.2234 -0.0532v =0.0185 -0.9009 -0.3066-0.7693 -0.1240 -0.7248-0.6386 -0.4158 0.6170d =-3.0527 0 00 3.6760 00 0 8.3766(2)>> a=[1 1 -1;0 2 -1;-1 2 0];det(a),inv(a),[v,d]=eig(a) ans =1ans =2.0000 -2.0000 1.00001.0000 -1.0000 1.00002.0000 -3.0000 2.0000v =-0.5773 0.5774 + 0.0000i 0.5774 - 0.0000i-0.5773 0.5774 0.5774-0.5774 0.5773 - 0.0000i 0.5773 + 0.0000id =1.0000 0 00 1.0000 + 0.0000i 00 0 1.0000 - 0.0000i(3)>> A=[5 7 6 5;7 10 8 7;6 8 10 9;5 7 9 10]A =5 76 57 10 8 76 8 10 95 7 9 10>> det(A),inv(A), [v,d]=eig(A)ans =1ans =68.0000 -41.0000 -17.0000 10.0000-41.0000 25.0000 10.0000 -6.0000-17.0000 10.0000 5.0000 -3.000010.0000 -6.0000 -3.0000 2.0000v =0.8304 0.0933 0.3963 0.3803-0.5016 -0.3017 0.6149 0.5286-0.2086 0.7603 -0.2716 0.55200.1237 -0.5676 -0.6254 0.5209d =0.0102 0 0 00 0.8431 0 00 0 3.8581 00 0 0 30.2887(4)(以n=5为例)方法一(三个for)n=5;for i=1:n, a(i,i)=5;endfor i=1:(n-1),a(i,i+1)=6;endfor i=1:(n-1),a(i+1,i)=1;enda方法二(一个for)n=5;a=zeros(n,n);a(1,1:2)=[5 6];for i=2:(n-1),a(i,[i-1,i,i+1])=[1 5 6];enda(n,[n-1 n])=[1 5];a方法三(不用for)n=5;a=diag(5*ones(n,1));b=diag(6*ones(n-1,1));c=diag(ones(n-1,1));a=a+[zeros(n-1,1),b;zeros(1,n)]+[zeros(1,n);c,zeros(n-1,1)] 下列计算>> det(a)ans =665>> inv(a)ans =0.3173 -0.5865 1.0286 -1.6241 1.9489-0.0977 0.4887 -0.8571 1.3534 -1.62410.0286 -0.1429 0.5429 -0.8571 1.0286-0.0075 0.0376 -0.1429 0.4887 -0.58650.0015 -0.0075 0.0286 -0.0977 0.3173>> [v,d]=eig(a)v =-0.7843 -0.7843 -0.9237 0.9860 -0.92370.5546 -0.5546 -0.3771 -0.0000 0.3771-0.2614 -0.2614 0.0000 -0.1643 0.00000.0924 -0.0924 0.0628 -0.0000 -0.0628-0.0218 -0.0218 0.0257 0.0274 0.0257d =0.7574 0 0 0 00 9.2426 0 0 00 0 7.4495 0 00 0 0 5.0000 00 0 0 0 2.5505Page65 Ex 7(1)>> a=[4 1 -1;3 2 -6;1 -5 3];[v,d]=eig(a)v =0.0185 -0.9009 -0.3066-0.7693 -0.1240 -0.7248-0.6386 -0.4158 0.6170d =-3.0527 0 00 3.6760 00 0 8.3766>> det(v)ans =-0.9255 %v行列式正常, 特征向量线性相关,可对角化>> inv(v)*a*v 验算ans =-3.0527 0.0000 -0.00000.0000 3.6760 -0.0000-0.0000 -0.0000 8.3766>> [v2,d2]=jordan(a) 也可用jordanv2 =0.0798 0.0076 0.91270.1886 -0.3141 0.1256-0.1605 -0.2607 0.4213 特征向量不同d2 =8.3766 0 00 -3.0527 - 0.0000i 00 0 3.6760 + 0.0000i>> v2\a*v2ans =8.3766 0 0.00000.0000 -3.0527 0.00000.0000 0.0000 3.6760>> v(:,1)./v2(:,2) 对应相同特征值的特征向量成比例ans =2.44912.44912.4491(2)>> a=[1 1 -1;0 2 -1;-1 2 0];[v,d]=eig(a)v =-0.5773 0.5774 + 0.0000i 0.5774 - 0.0000i-0.5773 0.5774 0.5774-0.5774 0.5773 - 0.0000i 0.5773 + 0.0000id =1.0000 0 00 1.0000 + 0.0000i 00 0 1.0000 - 0.0000i>> det(v)ans =-5.0566e-028 -5.1918e-017i v的行列式接近0, 特征向量线性相关,不可对角化>> [v,d]=jordan(a)v =1 0 11 0 01 -1 0d =1 1 00 1 10 0 1 jordan标准形不是对角的,所以不可对角化(3)>> A=[5 7 6 5;7 10 8 7;6 8 10 9;5 7 9 10]A =5 76 57 10 8 76 8 10 95 7 9 10>> [v,d]=eig(A)v =0.8304 0.0933 0.3963 0.3803-0.5016 -0.3017 0.6149 0.5286-0.2086 0.7603 -0.2716 0.55200.1237 -0.5676 -0.6254 0.5209d =0.0102 0 0 00 0.8431 0 00 0 3.8581 00 0 0 30.2887>> inv(v)*A*vans =0.0102 0.0000 -0.0000 0.00000.0000 0.8431 -0.0000 -0.0000-0.0000 0.0000 3.8581 -0.0000-0.0000 -0.0000 0 30.2887本题用jordan不行, 原因未知(4)参考6(4)和7(1)Page65 Exercise 8只有(3)对称, 且特征值全部大于零, 所以是正定矩阵. Page65 Exercise 9(1)>> a=[4 -3 1 3;2 -1 3 5;1 -1 -1 -1;3 -2 3 4;7 -6 -7 0]>> rank(a)ans =3>> rank(a(1:3,:))ans =2>> rank(a([1 2 4],:)) 1,2,4行为最大无关组ans =3>> b=a([1 2 4],:)';c=a([3 5],:)';>> b\c 线性表示的系数ans =0.5000 5.0000-0.5000 1.00000 -5.0000Page65 Exercise 10>> a=[1 -2 2;-2 -2 4;2 4 -2]>> [v,d]=eig(a)v =0.3333 0.9339 -0.12930.6667 -0.3304 -0.6681-0.6667 0.1365 -0.7327d =-7.0000 0 00 2.0000 00 0 2.0000>> v'*vans =1.0000 0.0000 0.00000.0000 1.0000 00.0000 0 1.0000 v确实是正交矩阵Page65 Exercise 11设经过6个电阻的电流分别为i1, ..., i6. 列方程组如下20-2i1=a; 5-3i2=c; a-3i3=c; a-4i4=b; c-5i5=b; b-3i6=0;i1=i3+i4;i5=i2+i3;i6=i4+i5;计算如下>> A=[1 0 0 2 0 0 0 0 0;0 0 1 0 3 0 0 0 0;1 0 -1 0 0 -3 0 0 0; 1 -1 0 0 0 0 -4 0 0;0 -1 1 0 0 0 0 -5 0;0 1 0 0 0 0 0 0 -3; 0 0 0 1 0 -1 -1 0 0;0 0 0 0 -1 -1 0 1 0;0 0 0 0 0 0 -1 -1 1];>>b=[20 5 0 0 0 0 0 0 0]'; A\bans =13.34536.44018.54203.3274-1.18071.60111.72630.42042.1467Page65 Exercise 12>> A=[1 2 3;4 5 6;7 8 0];>> left=sum(eig(A)), right=sum(trace(A))left =6.0000right =6>> left=prod(eig(A)), right=det(A) 原题有错, (-1)^n应删去left =27.0000right =27>> fA=(A-p(1)*eye(3,3))*(A-p(2)*eye(3,3))*(A-p(3)*eye(3,3)) fA =1.0e-012 *0.0853 0.1421 0.02840.1421 0.1421 0-0.0568 -0.1137 0.1705>> norm(fA) f(A)范数接近0ans =2.9536e-013Chapter 4Page84 Exercise 1(1)roots([1 1 1])(2)roots([3 0 -4 0 2 -1])(3)p=zeros(1,24);p([1 17 18 22])=[5 -6 8 -5];roots(p)(4)p1=[2 3];p2=conv(p1, p1);p3=conv(p1, p2);p3(end)=p3(end)-4; %原p3最后一个分量-4roots(p3)Page84 Exercise 2fun=inline('x*log(sqrt(x^2-1)+x)-sqrt(x^2-1)-0.5*x');fzero(fun,2)Page84 Exercise 3fun=inline('x^4-2^x');fplot(fun,[-2 2]);grid on;fzero(fun,-1),fzero(fun,1),fminbnd(fun,0.5,1.5)Page84 Exercise 4fun=inline('x*sin(1/x)','x');fplot(fun, [-0.1 0.1]);x=zeros(1,10);for i=1:10, x(i)=fzero(fun,(i-0.5)*0.01);end;x=[x,-x]Page84 Exercise 5fun=inline('[9*x(1)^2+36*x(2)^2+4*x(3)^2-36;x(1)^2-2*x(2)^2-20*x(3);16*x(1)-x(1)^3-2*x(2)^ 2-16*x(3)^2]','x');[a,b,c]=fsolve(fun,[0 0 0])Page84 Exercise 6fun=@(x)[x(1)-0.7*sin(x(1))-0.2*cos(x(2)),x(2)-0.7*cos(x(1))+0.2*sin(x(2))];[a,b,c]=fsolve(fun,[0.5 0.5])Page84 Exercise 7clear; close; t=0:pi/100:2*pi;x1=2+sqrt(5)*cos(t); y1=3-2*x1+sqrt(5)*sin(t);x2=3+sqrt(2)*cos(t); y2=6*sin(t);plot(x1,y1,x2,y2); grid on; 作图发现4个解的大致位置,然后分别求解y1=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[1.5,2])y2=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[1.8,-2])y3=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[3.5,-5])y4=fsolve('[(x(1)-2)^2+(x(2)-3+2*x(1))^2-5,2*(x(1)-3)^2+(x(2)/3)^2-4]',[4,-4])Page84 Exercise 8(1)clear;fun=inline('x.^2.*sin(x.^2-x-2)');fplot(fun,[-2 2]);grid on; 作图观察x(1)=-2;x(3)=fminbnd(fun,-1,-0.5);x(5)=fminbnd(fun,1,2);fun2=inline('-x.^2.*sin(x.^2-x-2)');x(2)=fminbnd(fun2,-2,-1);x(4)=fminbnd(fun2,-0.5,0.5);x(6)=2feval(fun,x)答案: 以上x(1)(3)(5)是局部极小,x(2)(4)(6)是局部极大,从最后一句知道x(1)全局最小,x(2)最大。