# Finding values with square wave input

Discussion in 'Programmer's Corner' started by brighton53, Jan 16, 2013.

1. ### brighton53 Thread Starter New Member

Jan 16, 2013
7
0

but I thought I would post another thread in there because It's more relevent and has more infomation.

So I'm generate 2 graphs with one being a current in the circuit and another a voltage across a capacitor.

Because I'm generating the square wave myself - I've been attempting to calculate the values with each of the sine waves that make up the square wave and add the results together. But Apparently my answer is incorrect - (my input is correct).

Can somebody take a quick look please !

Code ( (Unknown Language)):
1. %Constants
2. %%%%%%%%%%%%%%
3. T=1e-9;
4. omega = (2*pi)/T;
5. t = 0:0.1e-11:3e-9;
6.
7. c1 = 250e-12;
8. c2 = 250e-12;
9. c3 = 250e-12;
10. r1 = 0.5;
11. r2 = 1;
12.
13. zc1 = 1/(1i*omega*c1);
14. zc2 = 1/(1i*omega*c2);
15. zc3 = 1/(1i*omega*c3);
16. zr1 = r1;
17. zr2 = r2;
18.
19. zmatrix=[zc1,   -zc1,               0
20.         -zc1,   zc1+zr1+zc2+zr2,    -zr2
21.         0,      -zr2,               zr2+zc3];
22.
23. zinv = inv(zmatrix);
24.
25.
26. for n=0:200
27.     Vin = zeros(3, length(t));
28.     Vin(1,:) =  (1/(2*n+1))*sin((2*n + 1)*omega*t);
29.     Itemp = zinv*Vin;
30.     if n == 0
31.         I= Itemp;
32.         Vtotal = Vin(1,:);
33.         Vc3 = I(3,:)*zc3;
34.     else
35.         I = I + Itemp;
36.         Vtotal = Vtotal + Vin(1,:);
37.         Vc3 = Vc3 + I(3,:)*zc3;
38.     end
39. end
40.
41. figure(1)
42. subplot(311)
43. plot(t, Vtotal)
44. title('Input v(t)')
45. xlabel('time (s)')
46. ylabel('Voltage (V)')
47. subplot(312)
48. plot(t, I(2,:))
49. title('I(t)')
50. xlabel('time / s')
51. ylabel('Current (A)')
52. subplot(313)
53. plot(t, Vc3)
54. title('Voltage Cap 3')
55. xlabel('time (s)')
56. ylabel('Voltage (V)')
my output

and the outputs for I(t) and the voltage of capacitor 3 apparently should look like

2. ### MrChips Moderator

Oct 2, 2009
17,067
5,281
What are your units for r1 and r2? They seem rather low in value.

3. ### brighton53 Thread Starter New Member

Jan 16, 2013
7
0
Ohms - rather low but those are the values we where given.

4. ### brighton53 Thread Starter New Member

Jan 16, 2013
7
0
Any Ideas anybody?