Why is my function not graphing in the plot?
조회 수: 34 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2022년 6월 18일
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
.
추가 답변 (1개)
Voss
2022년 6월 18일
편집: Voss
2022년 6월 18일
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]);
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!