3

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:

f = -50:0.1:50;
x = 10*((sin(pi*f*0.1))/(pi*f*0.1))^2;
plot (f,x);

I can plot the function using a graph tool I found on google but MATLAB is just giving me a blank plot. The axis don't correspond to what I should be getting either.

Does anyone know why this graph doesn't appear as it should?

1 Answer 1

6

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Perfect. Thanks a lot.
I suspect he was the other answerer with the same answer
@Sardar_Usama Hahah I figured. Grumpy people :)
@smith1993 if this answer solves your problem, mark it as accepted by clicking the mark on the left side of this answer.
@smith1993 if this answers your question, please consider to accept this answer. Thanks :) upvote by me as compensation as well :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.