2013-10-16 259 views
0

即時嘗試繪製2個數字,使用fplot和繪圖功能,但對於我的情節(圖2),我得到一個錯誤,不明白爲什麼;Matlab錯誤,矩陣尺寸不同意

錯誤使用/ 矩陣尺寸必須一致。 (行9) H = 3 * g /((fo/f)。^ 2 + 3 *(fo/f)+3);

錯誤@(F)bhpfilter(F,FO,克)

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 [email protected](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)'); 
+0

上面的代碼是順便在2個不同的.m文件中 –

回答

0

在線路H = 3*g/((fo/f).^2 + 3*(fo/f)+3);克和FO是標量,而f是一個矢量。對於除法,當MATLAB是標量/矢量(它反其道而行之時)時,MATLAB不會將其識別爲單元劃分。你必須把:

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

希望有所幫助。

+0

是啊我試過它不起作用... –

+0

我得到了同樣的錯誤 –

+0

「給出響應0.8的f的值是1092.820323Hz」 我用這個改變很好地運行了你的代碼。 – user2816823