MATLAB第二次作业
题目
解答
1 、代码:
a1=2;a2=4;a3=7;a4=12;
subplot(221)
ezplot('x^2/2^2+y^2/(25-2^2)-1')
title('a=2')
grid on
subplot(222)
ezplot('x^2/4^2+y^2/(25-4^2)-1')
title('a=4')
grid on
subplot(223)
ezplot('x^2/(-3)^2+y^2/(25-(-3)^2)-1')
title('a=-3')
grid on
subplot(224)
ezplot('x^2/(-2.5)^2+y^2/(25-(-2.5)^2)-1')
title('a=-2.5')
grid on
运行结果:
2、代码:
clear;clc;
syms x y
x=0:0.2:5;
y=x.*exp(sin(x));
f=plot(x,y,'r--p');
set(f,'linewidth',4)
grid on
title('y=xe^s^i^n^x')
xlabel('x(0\rightarrow5)')
ylabel('y(0\rightarrow5)')
运行结果:
3、代码:
clear;clc;
[x,y]=meshgrid(-3:0.1:3,-3:0.1:3);
if x+y>1
p=0.54*exp(-0.75*x.^2-3.75*y.^2-1.5*y);
elseif (-1<x+y)&(x+y<1)
p=0.7575*exp(-x.^2-6*y.^2);
else
p=0.5457*exp(-0.75*x.^2-3.75*y.^2+1.5*y);
end
surf(x,y,p)
axis([-3 3 -3 3 0 0.7])
xlabel('x');ylabel('y');zlabel('P(x,y)')
title('分段函数表示的曲面')
运行结果:
4、代码:
clear;clc;
prompt={'请输入圆的半径:'};
title='圆面积周长计算器';
def={'1'};
lineno=1;
answer=inputdlg(prompt,title,[1 45]);
f=str2num(answer{1});
s=pi*f^2;
c=2*pi*f;
string=['圆的面积为',num2str(s),' 圆的周长为',num2str(c)];
[IconData,IconCMap]=imread('handsome.jpg');
h=msgbox(string,'运行结果','custom',IconData,IconCMap);
set(h,'resize','on')
运行结果:
取半径为13,得出结果
5、代码:
clear;clc;
f=@(t,y)[y(1)-0.01*y(1)*y(2);-y(2)+0.015*y(1)*y(2)];
tn=20;
y0=[25;30];
[t,y]=ode45(f,[0,tn],y0);
plot(t,y)
axis([0 20 0 350])
title('Lotka-Voltera模型')
ylabel('数量(y)');
xlabel('时间(t)');
h=legend('捕食者','被捕食者');
set(h,'Location','northeast')
set(gca,'xtick',0:2:20,'ytick',0:50:350)
运行结果: