Matlab错误,矩阵维度不同意

时间:2013-10-16 02:06:59

标签: matlab plot dimensions

我试图用fplot和plot函数绘制2个数字,但对于我的情节(图2),我得到一个错误并且不明白为什么;

使用/时出错 矩阵维度必须一致。

bhpfilter出错(第9行)     H = 3 * g /((fo / f)。^ 2 + 3 *(fo / f)+3);

@(f)bhpfilter(f,fo,g)中的错误

    function [H] = bhpfilter(f, fo, g)
    %freq finds the filter frequency response in V/V
    %fo is the cut off frequency, f is the input frequency and g is the filter
    %gain

    if fo <= 0 || g <=0 %error checking
        error('Inputs invalid');
    else
        H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3);

    end

    fo=1200.;
    g=2.;

    H =@(f) bhpfilter(f,fo,g);
    H_1 = @(f) bhpfilter (f,fo,g)-0.8;

    figure (1);
    fplot(H,[0 2000]);
    title('Plot of H vs f using fplot');
    xlabel('Frequency (Hz)');
    ylabel('Filter frequency response (V/V)');

    fprintf('The value of f that gives a response of 0.8 is %f Hz\n',fzero (H_1, [0 2000])); %placed this line of code here so that it can be visible in command window , showing it works

    figure (2);
    plot([0:2000],H([0:2000])); % code will find individual values of H(1), H(2) etc.. but will not find H([0:200])
    title('Plot of H vs f using plot');
    xlabel('Frequency (Hz)');
    ylabel('Filter frequency response (V/V)');

1 个答案:

答案 0 :(得分:0)

在行H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3);中,g和fo是标量,而f是向量。对于除法,当它是标量/向量时,MATLAB不会将除法运算符识别为元素除法(它用于相反的方式)。你必须把:

H = 3*g ./ ( (fo./f).^2 + 3*(fo./f)+3);

希望有所帮助。