-2

really struggling to get a script for the energy in a capacitor.

Using the formula:

v(t) = (1/c)*int(i(t)) dt

The script need to be able to accept arrays i.e.

t = 0,1,2,3,4
i = 2,3,5,5,8

Where C is a fixed value for example, I'm sure this is very easy for someone experienced in coding. But personally I’m having a nightmare with it!

Any help is greatly appreciated!!

Many Thanks

Lee

NKN
  • 6,482
  • 6
  • 36
  • 55
  • First, int() might not be doing what you expect if you do not have the symbolic math toolbox. Second, if you post some code, it will be easier to make a specific response. Third, the variation of current (i) over time is quite large to get good numerical integration. You might want to break time up into many smaller intervals and make dt smaller. – Nigel Davies Feb 09 '14 at 15:48

1 Answers1

1

You can compute the running integral using cumtrapz:

t = [0,1,2,3,4];
i = [2,3,5,5,8];
C = 1; %// example data

v = 1/C * cumtrapz(t,i);
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147