integral of a piece wise function

조회 수: 14 (최근 30일)
Giorgio Buoncristiano
Giorgio Buoncristiano 2019년 9월 14일
댓글: Giorgio Buoncristiano 2019년 9월 17일
Hello
I have just started using matlab and I'm blocked in calculating the integral of a simple piece wise fct , need this integral to calculate the variation of the state of charge of a battery . I tried to use cumsum in a for cycle but it doesn't really work. Can someone please help me or give me some ideas ?
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 9월 14일
I recommend that you clean up your code by using elseif
To do an integral use integral() with the waypoints option . But be careful, as that passes vectors of x values and you have to return something the same size. Suggest that you read about "logical indexing"

댓글을 달려면 로그인하십시오.

채택된 답변

Prabhan Purwar
Prabhan Purwar 2019년 9월 17일
Hi,
Integral of a function can be calculated either by making use of integral() function or trapz() function, which is more suited for calculating integral of a piecewise function.
Please have a look at the following code:
clc
close all
C=10;
dt=0.1;
t=0:dt:35;
for j=1:length(t)
if t(j)<=7
i(j)=20;
else
if t(j)<=10
i(j)=(t(j)-7)/3*(-35)+20;
else
if t(j)<=25
i(j)=-15;
else if t(j)<=35
i(j)=-7.5;
end
end
end
end
end
for j=1:length(t)
I(j)=trapz(t(1:j),i(1:j)); %trapz() function
SoC(j)=((C-I(j))./C).*100;
end
Please refer to the following links for further information
Hope this helps.
  댓글 수: 1
Giorgio Buoncristiano
Giorgio Buoncristiano 2019년 9월 17일
I tried using trapz() but I was not able to correctly use it, thank you! Anyway I did it using cumsum() without a for cycle.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by