0

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

错误使用/矩阵尺寸必须一致。

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)');
4

1 回答 1

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);

希望有帮助。

于 2013-10-16T03:09:58.940 回答