Why is my function not graphing in the plot?

40 views (last 30 days)
I am trying to plot a basic function in MATLAB, and the plot is appearing in a figure window, but these is no graph in the plot. My previous question was regarding the same issue, but the error was in my faulty understanding of linspace. I don't believe I am making the same mistake with linspace. I have pasted my code below as well as the plot generated by the code. I verified the shape of the plot with a graphing calculator to make sure I was using the correct x and y limits.
Code:
%Transfer Function for a passive, first-order, high-pass filter: 1/wRC
x = linspace(0.01,1,100);
%For this example, let's set R = 1 kOhm and C = 1 uF
%The variable x represents angular frequency w
R = 1000;
C = 0.000001;
g = (sqrt(R.^2 * x.^2 * C.^2))/(sqrt(1 + (R.^2 * x.^2 * C.^2)));
y = -20*log10(g);
plot(x,y);
grid on
xlim([0 0.05]);
ylim([0 0.05]);
Plot:
I am new to MATLAB and still learning, so thank you again for your time, and I appreciate any guidance you guys can give me.

Accepted Answer

Star Strider
Star Strider on 18 Jun 2022
Two problems:
First, you need to do element-wise division:
g = (sqrt(R.^2 * x.^2 * C.^2))./(sqrt(1 + (R.^2 * x.^2 * C.^2)))
↑ ← HERE
and second, the values of the curve are much greater than the ylim values set for them.
Also, using a logarithmic axis for the independent variable might make the plot look a bit closer to what you want.
%Transfer Function for a passive, first-order, high-pass filter: 1/wRC
x = linspace(0.01,1,100);
%For this example, let's set R = 1 kOhm and C = 1 uF
%The variable x represents angular frequency w
R = 1000;
C = 0.000001;
g = (sqrt(R.^2 * x.^2 * C.^2))./(sqrt(1 + (R.^2 * x.^2 * C.^2)));
y = -20*log10(g);
plot(x,y);
grid on
% % xlim([0 0.05]);
% % ylim([0 0.05]);
% set(gca, 'XScale','log') % Optional
.
  2 Comments
Aarya O
Aarya O on 18 Jun 2022
Huh okay. The plot that you posted looks exactly like the plot I was expecting to see in MATLAB. I probably made a mistake somewhere in the graphing calculator to get those limits. And thank you for the suggestion on the logarithmic scale--you probably picked up on this but I'm trying to make a frequency response bode plot. I knew I was missing something, and thinking about it now I'm pretty sure a logarithmic scale gives me exactly the trend that I'm looking for. Thank you again for the help!

Sign in to comment.

More Answers (1)

Voss
Voss on 18 Jun 2022
Edited: Voss on 18 Jun 2022
Just like you use element-wise operations .* and .^ for multiplication and exponentiation, respectively, you must also use element-wise division ./
x = linspace(0.01,1,100);
%For this example, let's set R = 1 kOhm and C = 1 uF
%The variable x represents angular frequency w
R = 1000;
C = 0.000001;
g = (sqrt(R.^2 * x.^2 * C.^2))/(sqrt(1 + (R.^2 * x.^2 * C.^2))) % matrix division / gives a scalar
g = 5.0500e-04
g = (sqrt(R.^2 * x.^2 * C.^2))./(sqrt(1 + (R.^2 * x.^2 * C.^2))) % element-wise division ./ gives a vector the same size as x
g = 1×100
1.0e-03 * 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900 0.3000
y = -20*log10(g);
plot(x,y);
grid on
(Also, these xlim and ylim don't make sense if you want to see the plotted line.)
% xlim([0 0.05]);
% ylim([0 0.05]);
  2 Comments
Aarya O
Aarya O on 18 Jun 2022
Thank you! I completely missed the element-wise division. I think I'm still not quite used to how MATLAB defaults everything to matrices, so that's an ongoing process to better understanding how to speak MATLAB. Thank you again for your time, and I hope you have a good day

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!