## RC Filter - Step Response vs. Time

Given R, C and Vin, calculate the step response and error for N time constants.

 R (Ohms) C (F) Vin (V) Input voltage. N Number of Tau to calculate vo. Tau (s) Tau = R*C fc (Hz) fc = 1/(2*pi*R*C)

Step Response: vo = Vin * Exp(-t/Tau)

For a quick tour of some JavaScript / HTML basics, check out Inside the Code.

### JavaScript

Some fairly straightforward RC filter calculations appear in the code. The intersting bit lies in writing to the "textarea". The textbox is only written to once at the end!

First, the code adds a line of text to the variable "TextOut" after each calculation in the for loop. Finally, "TextOut" is written to "myTextArea" as defined by the form.

```//////////////////////////////////////////////
// calc
//////////////////////////////////////////////
function get_StepResponse() {

// get values directly from form
var R=document.myForm.R.value;
var C=document.myForm.C.value;
var vin=document.myForm.vin.value;
var N=document.myForm.N.value;

var Tau, fc, t, vo, vo_err, vo_errp;
var TextOut;

// calc
Tau = R*C;
fc=1/(R*C*2*Math.PI);

// place in text box
document.myForm.fc.value = (fc).toPrecision(4);
document.myForm.Tau.value = (Tau).toPrecision(4);

// create text
TextOut = "t(s)\t\tvo\t\tvo_error (%)\n";
TextOut += "---------------------------------------\n";

// calc step response and place in text box
for(var i = 0; i < N; i++) {
t=i*Tau;
vo=t;
vo = vin * (1-Math.exp(-t/Tau));
vo_err = vin - vo;
vo_errp = vo_err/vin * 100;
TextOut +=  (t).toExponential(3)
+ "\t" + (vo).toExponential(3)+ "\t"
+ (vo_errp).toPrecision(3)+ "\n";
}

// send to textArea
document.myForm.myTextArea.value = TextOut
}
//////////////////////////////////////////
```

### HTML

The text box is easily createrd by the tag named "textarea". You can specify the Rows and Cols inline with the tag.

<textarea name="myTextArea" rows="15" cols="50"></textarea>