0

im trying to plot 2 figures, using fplot and plot functions, but for my plot (fig2) , i get an error and don't understand why;

Error using / Matrix dimensions must agree.

Error in bhpfilter (line 9) H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3);

Error in @(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
  • the code above is by the way in 2 different .m files
    – A Cizzle
    Oct 16, 2013 at 2:09

1 Answer 1

0

In the line H = 3*g / ( (fo/f).^2 + 3*(fo/f)+3); g and fo are scalars while f is a vector. For division MATLAB doesn't recognize the divide operator as element by element division when it is scalar/vector (it does for the other way around). You have to put:

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

Hope that helps.

1
  • "The value of f that gives a response of 0.8 is 1092.820323 Hz" I ran your code fine with that change. Oct 16, 2013 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.