How can I vectorize my code?
이전 댓글 표시
I know vectorizing code in Matlab makes it more efficent? How do I vectorize (or approach vectorizing) my code below?
% Ask for user input
L = input('Enter the inductance (mH): ');
C = input('Enter the capacitance (nF): ');
R = input('Enter the resistance (Ohms): ');
V = input('Enter the voltage (mV): ');
% Convert unit
L = L * 1e-3; %mH to H
C = C * 1e-9; %nF to F
V = V * 1e-3; %V to V
% Calculate resonant frequency
f0 = 1 / (2 * pi * sqrt(L * C)); % in Hz
fprintf('Resonant Frequency: %.2f Hz\n', f0);
%Define the frequency scale from 0 to 10,00,000 Hz (1 Mhz)
f = 0:1:2000000;
%Vrms equation calculation to be graphed
Vrms = zeros(1,length(f));
for i=1:length(f)
w = 2 * pi * f(i);
%display;
Vrms(i) = V * R / (sqrt(R^2 + ( w*L - 1 / (w * C))^2));
end
%display graph
plot(f,Vrms);
%label the axis and title for graph
title('Frequency Response');
xlabel('Frequency (Hz)');
ylabel('Volts (V)');
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Simscape Electrical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
