- 2

- 0

## Main Question or Discussion Point

Hi everyone, I have a problem in finding an integral of a function. I'm given the following function: x(t) = t + 1 (-1 <= t < o ); e^-t (0<= t <= 2); 0 (otherwise).I have to find the integral and plot it with its derivative and the given function.

Here is my code:

%Set up integral vector

u(1:1000) = 0;

%Find the integral

for n = 1:999;

if( (t(n)>= -1) & (t(n)<0) );

u(n) = 0.5.*(t.^2) + t;

end

if ( (t(n)>=0) & (t(n)<2) );

u(n) = -1.*(exp(-t));

end

if( (t(n)>2 ) )

u(n) = 1.*t;

end

end

subplot(3,1,3);

plot(t,u);

I get the following error when trying to run the code:

??? In an assignment A(I) = B, the number of elements in B and

I must be the same.

Error in ==> tut1 at 32

u(n) = 0.5.*(t.^2) + t;

Here is my code:

%Set up integral vector

u(1:1000) = 0;

%Find the integral

for n = 1:999;

if( (t(n)>= -1) & (t(n)<0) );

u(n) = 0.5.*(t.^2) + t;

end

if ( (t(n)>=0) & (t(n)<2) );

u(n) = -1.*(exp(-t));

end

if( (t(n)>2 ) )

u(n) = 1.*t;

end

end

subplot(3,1,3);

plot(t,u);

I get the following error when trying to run the code:

??? In an assignment A(I) = B, the number of elements in B and

I must be the same.

Error in ==> tut1 at 32

u(n) = 0.5.*(t.^2) + t;