【问题标题】:Matlab- graphing and subplots [closed]Matlab-绘图和子图[关闭]
【发布时间】:2021-01-19 17:45:08
【问题描述】:

我试着做我的作业,但我不确定我是否做对了。我不完全确定 linspace 是如何工作的,并希望对我的代码有一些输入!我将附上它所基于的作业。 [在此处输入链接描述][1] [在此处输入图片描述][2]

clc
clear all
figure(1);
figure(1);
x = 0:0.1:10
y1 = exp(-0.5*x).*sin(2*x)
y2 = exp(-0.5*x).*cos(2*x)
plot(x,y1,'-b',x,y2,'--r')
legend('y1','y2');
grid on
xlabel('\bfx axis');
ylabel('\bfy axis');
title('Sin and Cos Functions')

figure(2);
subplot(2,2,1)
plot(x,y1,'-b');
legend('y1','Location','northeast');
grid on
xlabel('\bfx')
ylabel('\bfy1')
title('Subplot of x and y1')

figure(3)
subplot(2,2,2)
plot(x,y2,'--r');
legend('y2','Location','northeast');
grid on
xlabel('\bfx')
ylabel('\bfy2')
title('Subplot of x and y2')

figure(4)
x = linspace(-2,8,200);
fun=(x.^2-6*x+5)./(x-3)
plot(x,fun,'b-','LineWidth',2)
axis([-2 8 -10 10]);
hold on;
title('Plot of f(x)')
xlabel('\bfx')
ylabel('\bfy')

(50 分)为 0 到 10 之间的 100 个 x 值绘制函数 y(x) = (e^-0.5x)(Sin2x)。为此函数使用蓝色实线。然后在同一轴上绘制函数 y(x) = (e^-0.5x)(Cos2x)。此功能使用红色虚线。请务必在图上包含图例、标题、轴标签和网格。使用 subplot 重新绘制这两个函数。

(50 Points) 绘制函数 绘制函数 f(x)= ((X^2)-6x-5)/(x-3) 在 -2 ≤ x ≤ 8 范围内使用 200 个点。注意,在 x = 3 处有一条渐近线,因此该函数在该点附近趋于无穷大。为了正确查看绘图的其余部分,您需要将 y 轴限制在 -10 到 10 的范围内。

【问题讨论】:

  • 第一个链接不可达。请将文本作为文本而不是图像发布
  • 您好,谢谢!我输入了代码,因为链接不起作用。这是我第一次使用 matlab,保存文件时遇到了麻烦。
  • 请阅读How to Ask。 Stack Overflow 最适合特定问题。一般反馈肯定不在范围内。很难说你是否做对了,我们不知道你在讲座中学到了什么,或者你应该使用什么功能。另一方面,我敢肯定,考虑到您应该绘制的功能,您创建的绘图是否有意义,不是吗?
  • 你犯了一些错误。 0:0.1:10 有 101 个值,而不是 100 个。您调用了两次 figure(1)。我认为您必须为这两个图表创建两个子图。

标签: string matlab subplot linspace


【解决方案1】:

linspace 接受 3 个参数:一个起始值、一个结束值和 n = 您想要在这两个数字之间生成的点数。它在您的起始值和结束值之间输出一个由 n 个均匀间隔的数字组成的数组。例如linspace(0, 10, 5) 返回一个由 5 个等距数字组成的数组,从 0 开始到 10 结束。

这是图 1 的代码

x = linspace(0, 10, 100);
y1 = exp(-0.5*x).*sin(2*x);
y2 = exp(-0.5*x).*cos(2*x);

figure(1)
plot(x, y1, '-b', x, y2, '--r')
title('This is the title')
legend('This is Y1', 'This is Y2')
xlabel('These are the X values')
ylabel('These are the Y values')
grid on

图 2. 两个子图都可以在一个图形句柄下(即图(2))。

figure(2)
subplot(2, 1, 1)
plot(x, y1, '-b')
title('This is the first subplot')
xlabel('X axis')
ylabel('Y axis')
grid on
subplot(2, 1, 2)
plot(x, y2, '--r')
title('This is the second subplot')
xlabel('Another X axis')
ylabel('Another Y axis')
grid on

图 3. 您的代码看起来正确。这是一个类似的解决方案:

x2 = linspace(-2, 8, 200);
y3 = ((x2.^2)-6.*x2-5)./(x2-3);

figure(3)
plot(x2, y3, '-g')
title('The third figure')
grid on
ylim([-10 10])
xlabel('Another axis')
ylabel('The last axis')

【讨论】:

  • “你必须为每个函数单独调用 plot。” 那不是真的。您可以通过一次调用plot 绘制多个图形。这个简单的任务不需要hold onhold off
  • 我的立场是正确的。已编辑。
猜你喜欢
  • 2012-05-02
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
相关资源
最近更新 更多