I'm new to Matlab and wanted to integrate a section-wise integrated vector.
The code I use to create a vector looks like this:
dt=1/1000;
t=0:dt:6;
g(t<=0)=0;
g(t>=0 & t<=1)=1*t(t>=0 & t<=1);
g(t>=1 & t<=3)=1;
g(t>=3 & t<=4)=-1*(t(t>=3 & t<=4)-4);
g(t>=4)=0;
I managed to get it work with a diff()
operation:
function [h] = diff_plot(g,t)
dt = 1/1000;
h = diff(g)*1/dt;
h(end) = h(end-1);
subplot(2,1,1);
plot(t,g);
grid on;
xlabel('Zeit in T');
title('g(t)');
subplot(2,1,2);
plot(t,h);
grid on;
xlabel('Zeit in T');
title('h(t)=dg(t)/dt');
end
But now i don't know how to do it with the int()
- function. I always get the error "Undefined function 'int' for input arguments of type 'double'."
You could use cumsum in an oposite way of diff function.
Below is description of cumsum function.
B = cumsum(A) returns the cumulative sum of A starting at the beginning of the first array dimension in A whose size does not equal 1.
By multiplying dt, you can calculate integral of discrete function.
int_g=cumsum(g)*dt;
plot(t,int_g)