Why is my function not graphing in the plot?
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (1)
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]);
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

