在matlab中在同一窗口用不同的线型绘制y=sinx,y=cosx在[-2π, 2π]上的图像,并加标注。
时间: 2024-10-23 14:10:45 浏览: 63
在MATLAB中,你可以通过以下步骤在同一窗口用不同的线型(line styles)绘制正弦函数(y = sin(x))和余弦函数(y = cos(x))在区间`[-2π, 2π]`上的图像,并添加相应的标题和坐标轴标签:
1. 首先,创建一个包含从`-2π`到`2π`的等差数组`x`:
```matlab
x = linspace(-2*pi, 2*pi, 400); % 创建400个点均匀分布在区间
```
2. 计算正弦和余弦值:
```matlab
y_sin = sin(x);
y_cos = cos(x);
```
3. 使用`plot`函数绘制两条曲线,指定不同的线型(如实线 `-` 和点线 `:`):
```matlab
plot(x, y_sin, '-o', 'LineWidth', 1.5, 'DisplayName', 'Sine'); % 实心圆点线
hold on; % 继续在同一图上绘图,hold off会在完成所有绘图后再显示
plot(x, y_cos, ':', 'LineWidth', 1.5, 'DisplayName', 'Cosine'); % 空心点线
```
4. 添加标题、坐标轴标签以及图例:
```matlab
title('Trigonometric Functions: Sine and Cosine');
xlabel('X (radians)');
ylabel('Y');
legend('show'); % 显示图例
```
5. 最后,记得关闭`hold`状态:
```matlab
hold off;
```
完整的代码如下:
```matlab
x = linspace(-2*pi, 2*pi, 400);
y_sin = sin(x);
y_cos = cos(x);
plot(x, y_sin, '-o', 'LineWidth', 1.5, 'DisplayName', 'Sine');
hold on;
plot(x, y_cos, ':', 'LineWidth', 1.5, 'DisplayName', 'Cosine');
title('Trigonometric Functions: Sine and Cosine');
xlabel('X (radians)');
ylabel('Y');
legend('show');
hold off;
```
阅读全文
相关推荐


















