MATLAB: Trying to plot x,y, but it says error in y = f(x);

plot

function graph
x = 1:100;
y = f(x);
plot(x,y);
end
function [y] = f(n)
y = n * log2(n);
end

Best Answer

You need to use element-wise operations in this line:
y = n .* log2(n);
Note the ‘.*’ replacing the ‘*’. See the documentation for Array vs. Matrix Operations for details.