文档库 最新最全的文档下载
当前位置:文档库 › matlab第四章课后答案

matlab第四章课后答案

%Exercise 1(1)
roots([1 1 1])

%Exercise 1(2)
roots([3 0 -4 0 2 -1])

%Exercise 1(3)
p=zeros(1,24);
p([1 17 18 22])=[5 -6 8 -5];
roots(p)

%Exercise 1(4)
p1=[2 3];
p2=conv(p1, p1);
p3=conv(p1, p2);
p3(end)=p3(end)-4; %原p3最后一个分量-4
roots(p3)

%Exercise 2
fun=inline('x*log(sqrt(x^2-1)+x)-sqrt(x^2-1)-0.5*x');
fzero(fun,2)

%Exercise 3
fun=inline('x^4-2^x');
fplot(fun,[-2 2]);grid on;
fzero(fun,-1),fzero(fun,1),fminbnd(fun,0.5,1.5)

%Exercise 4
fun=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]

%Exercise 5
fun=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])

%Exercise 6
fun=@(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])

%Exercise 7
clear; 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])

%Exercise 8(1)
clear;
fun=inline('x.^2.*(x.^2-x-2)');
fplot(fun,[-2 2]);grid on; %作图观察
x(1)=-2;
x(2)=fminbnd(fun,-1,-0.5);
x(4)=fminbnd(fun,1,2);
fun2=inline('-x.^2.*(x.^2-x-2)');
x(3)=fminbnd(fun2,-0.5,0.5);
x(5)=2
feval(fun,x)
%答案: 以上x(2)(4)是局部极小,x(1)(3)(5)是局部极大,从最后一句知道x(1)全局最大, x(4)最小。

%Exercise 8(2)
clear;
fun=inline('3*x.^5-20*x.^3+10');
fplot(fun,[-3 3]);grid on;%作图观察
x(1)=-3;
x(3)=fminsearch(fun,2.5);
fun2=inline('-(3*x.^5-20*x.^3+10)');
x(2)=fminsearch(fun2,-2.5);
x(4)=3;
feval(fun,x)

%Exercise 8(3)
fun=inline('abs(x^3-x^2-x-2)');
fplot(fun,[0 3]);grid on;%作图观察
fminbnd(fun,1.5,2.5)
fun2=inline('-abs(x^3-x^2-x-2)');
fminbnd(fun2,0.5,1.5)

%Exercise 9
close;
x=-2:0.1:1;y=-7:0.1:1;
[x,y]=meshgrid(x,y);
z=y.^3/9+3*x.^2.*y+9*x.^2+y.^2+x.*y+9;
mesh(x,y,z);grid on;%作图观察
fun=inline('x(2)^3/9+3*x(1)^2*x(2)+9*x(1)^2+x(2)^2+x(1)*x(2)+9');
x=fminsearch(fun,[0 0])%求极小值
fun2=inline('-(x(2)^3/9+3*x(1)^2*x(2)+9*x(1)^2+x(2)^2+x(1)*x(2)+9)');
x=fminsearch(fun2,[0 -5])%求极大值

%Exercise 10
clear;t=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];
p2=polyfit(t,c,2)
p3=polyfit(t,c,3)
fun=inline('a(1)*exp(a(2)*(t-14).^2)','a','t');
a=lsqcurvefit(fun,[0 0],t,c)%初值可以试探
f=feval(fun, a,t)
norm(f-c)%拟合效果
plot(t,c,t,f) %作图检验
fun2=inline('b(1)*sin(pi/12*t+b(2))+20','b','t');%原题修改f(x)+20
b=lsqcurvefit(fun2,[0 0],t,c)
fi

gure
f2=feval(fun2, b,t)
norm(f2-c)%拟合效果
plot(t,c,t,f2) %作图检验

%Exercise 11
fun=inline('(1-x)*sqrt(10.52+x)-3.06*x*sqrt(1+x)*sqrt(5)');
x=fzero(fun, 0, 1)

%Exercise 12
r=5.04/12/100;N=20*12;
x=7500*180 %房屋总价格
y=x*0.3 %首付款额
x0=x-y%贷款总额
a=(1+r)^N*r*x0/((1+r)^N-1)%月付还款额
r1=4.05/12/100;x1=10*10000;%公积金贷款
a1=(1+r1)^N*r1*x1/((1+r1)^N-1)
x2=x0-x1%商业贷款
a2=(1+r)^N*r*x2/((1+r)^N-1)
a=a1+a2

%Exercise 13
%列方程th*R^2+(pi-2*th)*r^2-R*r*sin(th)=pi*r^2/2
%化简得sin(2*th)-2*th*cos(2*th)=pi/2
%以下Matlab计算
clear;fun= inline('sin(2*th)-2*th*cos(2*th)-pi/2','th')
th=fsolve(fun,pi/4)
R=20*cos(th)

%Exercise 14
%先在Editor窗口写M函数保存
function x=secant(fname,x0,x1,e)
while abs(x0-x1)>e,
x=x1-(x1-x0)*feval(fname,x1)/(feval(fname,x1)-feval(fname,x0));
x0=x1;x1=x;
end
%再在指令窗口
fun=inline('x*log(sqrt(x^2-1)+x)-sqrt(x^2-1)-0.5*x');
secant(fun,1,2,1e-8)

%Exercise 15
%作系数为a,初值为xo,从第m步到第n步迭代过程的M函数:
function f=ex4_15fun(a,x0,m,n)
x(1)=x0; y(1)=a*x(1)+1;x(2)=y(1);
if m<2, plot([x(1),x(1),x(2)],[0,y(1),y(1)]);hold on; end
for i=2:n
y(i)=a*x(i)+1; x(i+1)=y(i);
if i>m, plot([x(i),x(i),x(i+1)],[y(i-1),y(i),y(i)]); end
end
hold off;
%M脚本文件
subplot(2,2,1);ex4_15fun(0.9,1,1,20);
subplot(2,2,2);ex4_15fun(-0.9,1,1,20);
subplot(2,2,3);ex4_15fun(1.1,1,1,20);
subplot(2,2,4);ex4_15fun(-1.1,1,1,20);

%Exercise 16
%设夹角t, 问题转化为 min f=5/sin(t)+10/cos(t)
%取初始值pi/4, 计算如下
fun=@(t)5/sin(t)+10/cos(t);
[t,f]=fminsearch(fun, pi/4)
t =
0.6709
f =
20.8097

%Exercise 17
%提示:x(k+2)=f(x(k))=a^2*x(k)*(1-x(k))*(1-a*x(k)*(1-x(k)))
%计算平衡点x
%|f'(x)|<1则稳定

%Exercise 18
%先写M文件
function f=ex4_18(a,x0,n)
x=zeros(1,n);y=x;
x(1)=x0;
y(1)=a*x(1)+1;
x(2)=y(1);
plot([x(1),x(1),x(2)],[0,y(1),y(1)],'r');
hold on;
for i=2:n
y(i)=a*x(i)+1;
x(i+1)=y(i);
plot([x(i),x(i),x(i+1)],[y(i-1),y(i),y(i)])
end
hold off;
%再执行指令
>> ex4_18(0.9,1,20)
>> ex4_18(-0.9,1,20)
>> ex4_18(1.1,1,20)
>> ex4_18(-1.1,1,20)

%Exercise 19
clear; close; x(1)=0; y(1)=0;
for k=1:3000
x(k+1)=1+y(k)-1.4*x(k)^2; y(k+1)=0.3*x(k);
end
plot(x(1000:1500),y(1000:1500),'+g');hold on
plot(x(1501:2000),y(1501:2000),'.b');
plot(x(2001:2500),y(2001:2500),'*y');
plot(x(2501:3001),y(2501:3001),'.r');

相关文档