Problem with simple calculator

I have the following javascript code wherein i calculate f and Xc. When i enter input values the f value gets correctly calculated however Xc output value does not change(Xc depends upon f). Can anybody tell me whats wrong in here?
JS:

 <script>
   function RCCalc(){
		 var R1val = parseFloat(document.getElementById('R1Id').value);
         var R1sel = document.getElementById('R1Select').value;
         var C1val = parseFloat(document.getElementById("C1Id").value);
       	 var C1sel = document.getElementById('C1Select').value;

     	 var R1,C1;
         switch (R1sel) {
               case 'ohm': R1 = 1*R1val; 
                   break;

                   case 'kohm': R1 = 1e3*R1val;
                   break;

                   case 'mohm': R1 = 1e6*R1val;
                   break;

                   default: 
                   return; 
             	}         
         switch (C1sel) {
                case 'uF': C1=C1val*1e-6; 
         break;

         case 'nF': C1=C1val*1e-9;
         break;

               case 'pF': C1=C1val*1e-12;
         break;

         default: return; 
           }     
     
     	var f = 1/(2*3.14*R1*C1);
     var X = 1/(2*3.14*f*C1);
 
     	if(f >= 1000000)
         	document.getElementById("fAns").value = (f/1000000).toFixed(2)+" MHz";
       	else if(f<1000000 && f >= 1000)
         	document.getElementById("fAns").value = (f/1000).toFixed(2)+" KHz";
       	else
         	document.getElementById("fAns").value = (f).toFixed(2)+" Hz";
         
        if(X >= 1000000)
           document.getElementById("XcAns").value = (X/1000000).toFixed(2)+" MΩ";
        else if(X < 1000000 && X  >= 1000)
           document.getElementById("XcAns").value = (X/1000).toFixed(2)+" KΩ";
        else
           document.getElementById("XcAns").value = (X).toFixed(2)+ " Ω"; 
       
       } 
</script>

html:

 <div>
            <form autocomplete="off">
            <label><b>R</b>:</label><input id="R1Id" size="5" type="text" />   
                                          <select id="R1Select">
                                          <option value="ohm">Ω</option>
                                          <option selected="" value="kohm">KΩ</option>
                                          <option value="mohm">MΩ</option>
                                          </select><br />
          <label><b>C</b>:</label><input id="C1Id" size="5" type="text" />   
                                          <select id="C1Select">
                                          <option selected="" value="uF">µF</option>
                                        <option value="nF">nF</option>
                                        <option value="pF">pF</option>
                                          </select><br />
          <input type="reset" value="Reset" /><input onclick="RCCalc()" type="button" value="Calculate" /><br />	
          <b>Results:</b><br />
            <label><b>f<sub>0</sub></b>:</label><input id="fAns" readonly="" size="10" type="text" /><br />
            <label><b>X<sub>c</sub></b>:</label><input id="XcAns" readonly="" size="10" type="text" /><br />
    </form>
</div>
1 Like

So…
Lets do some maths.

if F is 1/(6.28*R1*C1) and X is 1/(6.28*F*C1)… substitute for F…

X = 1/(6.28*C1* ( 1/C1 * 6.28 * R1))

                 1
     -------------------------
X =                         1
        6.28 * C1 *   --------------
                      6.28 * C1 * R1

Which… simplifies… cancel out the 6.28’s, cancel out the C1’s…
X = 1/(1/R1) = R1.
So X = R1. You sure you got your formulae right? (Seems like an awfully convoluted way to write X = R1 if so…)

1 Like

Here it works fine. If the result is the expected I do not know.

but you can clean up your code if you put the needed values in the select directly.

1 Like

The value of capacitive reactance (Xc) will always be the same as the resistance (R) at the cut-off frequency (fo). So there is no point in calculating Xc.

Note the prefix for kilo should be lower case ‘k’ (reference).

1 Like

ha ha…thanks for pointing that out

Are you calculating the impedance for a series RC circuit or a parallel RC circuit.
For a Parallel RC the impedance must be less than the resistor value, as the impedance of the capacitor is in parallel with it.
For a series RC then we need to delve into imaginary numbers as the impedance is the vector sum of the resistor impedance and the capacitor impedance.

Given: A 40 Ω resistor in series with a 88.42 microfarad capacitor. Find the impedance at 60 hertz.

XC = 1/(2πfC)
XC = 1/(2π·60·88.42×10-6)
XC = 30 Ω
Z = R - jXC
Z = 40 - j30
|Z| = sqrt(402 + (-30)2) = 50 Ω
∠Z = arctangent(-30/40) = -36.87°
Z = 40 - j30 = 50∠-36.87°

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.