0

I am calculating capacitor voltage and current. Now I want to determine the energy also. Energy is just the integral of power, however I cannot integrate my power function:

I_C=exp(-alpha*t).*(x5(1)*cos(omega_d*t)+x5(2)*sin(omega_d*t));
V_C=exp(-alpha*t).*(x6(1)*cos(omega_d*t)+x6(2)*sin(omega_d*t))+V_In; 
Pow_C=V_C.*I_C;
Pow_C_Function=@(t)Pow_C;
Energy_C=quad(Pow_C,0,tf)

I get the error: The integrand function must return an output vector of the same length as the input vector.

Can anyone help?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
user1412994
  • 35
  • 1
  • 2
  • 9

2 Answers2

1

you should define I_C, V_C and Pow_C as functions (as you have done for Pow_C_Function). Currently they are just variables.

Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52
  • Like this? I_C_Function = @(t) I_C; V_C_Function = @(t) V_C; And then use those like Pow_C_Function = @(t) V_C_Function*I_C_Function Doesn't seem to work... could you give me an example of what you mean please? – user1412994 Oct 22 '12 at 03:04
  • I get this error now: Undefined function 'times' for input arguments of type 'function_handle'. For the line: Energy_C=quad(Pow_C,0,tf) – user1412994 Oct 22 '12 at 03:16
1

Well, you've defined I_C and V_C as being two matrices, not two functions. The fix is simple:

I_C   = @(t) exp(-alpha*t).*(x5(1)*cos(omega_d*t)+x5(2)*sin(omega_d*t));
V_C   = @(t) exp(-alpha*t).*(x6(1)*cos(omega_d*t)+x6(2)*sin(omega_d*t))+V_In; 
Energy_C = quad(@(t)V_C(t).*I_C(t), 0,tf);

Also, have a look at quadgk or quadl, or if you're on Matlab R2012a or newer, integral.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96