Matlab中错误的输出图图像

时间:2011-11-24 17:38:36

标签: matlab electronics

我必须将简单的程序编写到Matlab中,但是遇到了问题。任务是:

  

绘制输出电压幅度和相位,如图1所示。   令L为R2C / m,并且m在步骤0.2中取1至5的值。

我尝试使用此代码:

function coherence_task2 (R, omega, tau, m, i)


R=1;

tau=1;

f=50; 

omega=2*pi*f;

ksi=0;

i=1;

for m=1:0.2:5;

    moduo_Z=R*sqrt(((1+omega.^2*tau.^2)/((1-omega.^2*tau^2*m).^2+(omega*tau*m).^2)));

    argument_Z=(atan(omega*tau))-(atan((omega*tau*m)/(1-omega.^2*tau.^2*m)));

end;

Z=moduo_Z*exp((sqrt(-1))*argument_Z);

u=Z*i;

plot(moduo_z , argument_z)

问题是这个程序只绘制了一个点。我是Matlab编程的新手,但想学习..
对我来说最大的问题是我不知道在x轴上绘制什么,在y轴上是什么。有人可以帮我解决这个问题吗? 我不希望你解决我的整个代码,只是为了给我路径去哪里。

编辑: 这是这个任务的图: enter image description here

2 个答案:

答案 0 :(得分:1)

我不确定这有多大帮助,但我有一些快速指示。

plot(moduo_z , argument_z)

moduo_z和argument_z是两个单独的值,而不是要绘制的数据数组。如果要将plot命令添加到for循环中,可以使用

hold on;

循环的前方。然后将所有绘图命令绘制到同一图形中。我稍微修改了你的代码,希望它有所帮助。

function coherence_task2 (R, omega, tau, m, i)

R=1;
tau=1;
f=50; 
omega=2*pi*f;
ksi=0;
i=1;


moduo = zeros(21,1); % length(1:0.2:5) = 21.

argu = zeros(21,1);

cnt = 1;

for m=1:0.2:5

    moduo(cnt) = R*sqrt(((1+omega.^2*tau.^2)/((1-omega.^2*tau^2*m).^2+(omega*tau*m).^2)));

    argu(cnt) = (atan(omega*tau))-(atan((omega*tau*m)/(1-omega.^2*tau.^2*m))); 

    cnt = cnt + 1;

end

% This was never used

% Z=moduo_Z*exp((sqrt(-1))*argument_Z); 


% This was never used

% u=Z*i; 

plot(moduo , argu)

答案 1 :(得分:1)

matlab的绘图函数将数据连音符(x,y)绘制成图形的方式。这意味着您必须首先生成这些连音符或x-y值对。例如:

% generate a vector of values from -5 to +5
x = [-5:0.1:5]; 

% put every element of x in the expression -> y has same length
y = 1 ./ (1 + exp(x));

% plot the two vectors. note that the tuplets are defined by the array index
plot(x,y)

另请注意,有两种方法可以使用'.m'文件(matlab脚本):

功能:如果您的文件以关键字function开头,则文件名必须与该函数的名称相对应。然后,这会使您的工作区中的函数可用,如C ++或Java中的静态方法 - 因此在您的情况下,您可以从控制台调用函数coherence_task2 (R, omega, tau, m, i),如下所示:coherence_task2(123,44,55,66)

此外,您的函数现在没有返回参数。您可以在.m文件中定义返回参数(第一行):function y = SomeFunctionName(x)。在函数体中,只需在'endfunction'之前为y赋值 - 然后在执行后自动返回该值。这允许您从控制台调用这样的函数并获取结果:anotherY = SomeFunctionName(x)

批处理文件:如果文件不以function开头,则会将其解释为您工作区中执行的一系列命令 - 就像您在控制台中键入它们一样。例如,您可以将我上面写的内容(绘图示例)放入.m文件中。然后,您可以通过将文件名写入控制台(不带任何参数)来执行脚本/序列。

这是我几天前写的一个批处理文件的例子,它也说明你没有绘制函数,你绘制了x-y值对:

n = [1:6000];

theta = 2*pi/(137.51/180) * n;
r = 0.1*sqrt(n);

x = cos(theta).*r;
y = sin(theta).*r;
plot(x, y, '.');