Why is my function not graphing in the plot?
32 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
More Answers (1)
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 = (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
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]);
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!