I just solved a problem on TJU online judge. Just an easy problem. So here is the problem
Problem 1560 – Bode Plot
Problem
Consider the AC circuit below. We will assume that the circuit is in steady-state.
Thus, the voltage at nodes 1 and 2 are given by v1 = VScost
and v2 = VRcos(t +
) where VS is the voltage of the source,
is the frequency (in radians per second), and t is time. VR is the magnitude of the voltage drop across the resistor, and
is its phase.
![]()
You are to write a program to determine VR for different values
of. You will need two laws of electricity to solve this problem. The first
is Ohm’s Law, which states v2 = iR where i is the current in the circuit, oriented
clockwise. The second is i = C d/dt(v1 – v2) which relates the current to the
voltage on either side of the capacitor. “d/dt” indicates the derivative with
respect to t.The input will consist of one or more lines. The first line contains three real
numbers and a non-negative integer. The real numbers are VS, R, and C, in that
order. The integer, n, is the number of test cases. The following n lines of
the input will have one real number per line. Each of these numbers is the angular
frequency,.
Output
For each angular frequency in the input you are to output its corresponding
VR on a single line. Each VR value output should be rounded to three digits
after the decimal point.Sample Input
1.0 1.0 1.0 9 0.01 0.031623 0.1 0.31623 1.0 3.1623 10.0 31.623 100.0Sample Output
0.010 0.032 0.100 0.302 0.707 0.953 0.995 1.000 1.000
Well, if you are an Electrical Engineer, you don’t need the second law given in the problem i = C d/dt(v1 – v2) to solve this problem. All you need is phasor, then you can just transform the capacitor value to a “resistor” with value and the problem will be very easy enough to solve with a simple loop to get VR.
After solving a rather simple math equation, I get the value of V2 is this equation:
We are asked to find VR and not V2, in the equation above VR is the amplitude of V2. So we just ignore the inverse tangent part, FYI, that part mean the phase, . So we get:
Just translate it into a C code:
#include
#include
int main() {
double Vs,R,C,omega;
int i,n;
scanf("%lf %lf %lf %d",&Vs,&R,&C,&n);
for (i=0;i<n;i++) {
scanf("%lf",&omega);
printf("%.3lf\n",Vs*R/sqrt(R*R+1/(omega*omega*C*C)));
}
return 0;
}
And I got accepted!
FYI, this problem only solved twice this year, by gxlmoon and me. Computer Scientist don’t know how to use phasor maybe?
beuh, ngeri dah situ..
frik!