|
1、編寫M文件繪制以下函數(shù)在區(qū)間中的圖形。
(1)Matlab程序:
clc,clear;
fprintf('請輸入要顯示區(qū)域的下邊界(x<0):\n');
lim1 = input('');
fprintf('請輸入要顯示區(qū)域的上邊界(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)執(zhí)行結(jié)果為:
2、編寫通用的M函數(shù)求取題1中函數(shù)在任意點(diǎn)的值并繪制函數(shù)在區(qū)間中的圖形。
(1) 程序:
clc,clear;
fprintf('請輸入要顯示區(qū)域的下邊界(x<0):\n ');
lim1 = input('');
fprintf('請輸入要顯示區(qū)域的上邊界(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)結(jié)果:
|
|