1、编写M文件绘制以下函数在区间中的图形。
(1)Matlab程序:
clc,clear;
fprintf('请输入要显示区域的下边界(x<0):\n');
lim1 = input('');
fprintf('请输入要显示区域的上边界(x>3):\n');
lim2 = input('');
x1 = lim1:0.01:0;
x2 = 0:0.01:3;
x3 = 3:0.01:lim2;
y1 = sin(x1);
y2 = x2;
y3 = 6 - x3;
plot(x1,y1,'-b');
hold on
plot(x2,y2,'-r');
hold on
plot(x3,y3,'-g');
(2)输入:
(3)执行结果为:
2、编写通用的M函数求取题1中函数在任意点的值并绘制函数在区间中的图形。
(1) 程序:
clc,clear;
fprintf('请输入要显示区域的下边界(x<0):\n ');
lim1 = input('');
fprintf('请输入要显示区域的上边界(x>3):\n ');
lim2 = input('');
x1 = lim1:0.01:0;
x2 = 0:0.01:3;
x3 = 3:0.01:lim2;
y1 = sin(x1);
y2 = x2;
y3 = 6 - x3;
plot(x1,y1,'-b');
hold on
plot(x2,y2,'-r');
hold on
plot(x3,y3,'-g');
hold on
fprintf('请输入x的值:');
x = input('');
if x <= 0
y = sin(x);
elseif x <= 3
y = x;
else
y = 6 - x;
end
plot(x,y,'*k');
(2) 输入:
(3)结果:
|