Plotting function in MATLAB

前端 未结 1 1466
醉酒成梦
醉酒成梦 2021-01-20 17:27

I am having a problem plotting the following function in Matlab:

x = 10*((sin(pi*f*0.1))/(pi*f*0.1))^2;

I am using this code:



        
相关标签:
1条回答
  • 2021-01-20 18:13

    You need to use element-wise division (./) rather than mrdivide (/) which attempts to solve a linear system. Similarly, you need to use the element-wise power (.^) rather than the matrix power, mpower (^).

    x = 10 * ((sin(pi * f * 0.1)) ./ (pi * f * 0.1)).^2;
    

    The . in the operator is subtle and not necessary when working with scalars; however you must use it if you want element-wise behavior when working with multi-dimensional arrays.

    Also to be consistent with common conventions, I'd recommend switching f and x so you have a function f(x)

    x = -50:0.1:50;
    f = 10 * ((sin(pi * x * 0.1)) ./ (pi * x * 0.1)).^2;
    plot(x, f)
    
    0 讨论(0)
提交回复
热议问题