题目:
已知y_1=x^2,y_2=cos〖(2x),〗 y_3=y_1*y_2,完成下列操作。
(1)在同一坐标系下用不同的颜色和线型绘制3条曲线
(2) 以子图形式绘制3条曲线。
(3)分别用条形图、阶梯图
代码
% 定义x的取值范围
x = -2:0.1:2;
% 计算y1, y2, y3的值
y1 = x.^2;
y2 = cos(2*x);
y3 = y1 .* y2;
% (1) 在同一坐标系下用不同的颜色和线型绘制3条曲线
figure;
plot(x, y1, 'r-', x, y2, 'g--', x, y3, 'b:');
legend('y1=x^2', 'y2=cos(2x)', 'y3=y1*y2');
xlabel('x');
ylabel('y');
title('Three Curves in the Same Plot');
% (2) 以子图形式绘制3条曲线
figure;
subplot(3,1,1);
plot(x, y1, 'r-');
title('y1=x^2');
subplot(3,1,2);
plot(x, y2, 'g--');
title('y2=cos(2x)');
subplot(3,1,3);
plot(x, y3, 'b:');
title('y3=y1*y2');
% (3) 分别用条形图、阶梯图、杆图和填充图绘制三条曲线
figure;
subplot(2,2,1);
bar(x, y1, 'r');
title('Bar Plot of y1=x^2');
subplot(2,2,2);
stairs(x, y2, 'g');
title('Stem Plot of y2=cos(2x)');
subplot(2,2,3);
stem(x, y3, 'b');
title('Stem Plot of y3=y1*y2');
subplot(2,2,4);
area(x, y1, 'FaceColor', 'r');
hold on;
area(x, y2, 'FaceColor', 'g');
area(x, y3, 'FaceColor', 'b');
title('Area Plot of y1, y2, y3');
legend('y1=x^2', 'y2=cos(2x)', 'y3=y1*y2');