MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

New to MATLAB?

# Thread Subject: problem with cumtrapz

Subject: problem with cumtrapz

From: Sanaa

### Sanaa (view profile)

Date: 7 Dec, 2012 17:51:08

Message: 1 of 4

Hi,
I want to integrate the discrete function a*x_n*(1-x_n) from nr to t (take r=0.005)
> > where a:0:0.001:4 and n=0,1,2,3,...., and t\in[nr,(n+1) r]
I have tried the follwoing
% define the vector of colors, to plot the dat aof reach value of r in
% different color
color_vec = ['b'];
r=0.05;
for rho = 0: 0.001:5
r_vec=r;
% do a loop of values of r
for j = 1 : size(r_vec,2)
% define delay
r = r_vec(j);
% define coefficient rho
%rho = 0.5;
% define the number of deiscrete times in interval [k*r, (k+1)*r]
n = 50;
% define the nimber of iterations k = 1,.....,Nit
Nit = 300;
% define the initial vector which is a vector of size n
x0 = 0.3*ones(1,n);
% define vectors x_next and x_previous
x_next = zeros(1,n); %x_(n+1)
x_previous = zeros(1,n); %x_n

time = zeros(1,n);

% initialize x_previous
x_previous = x0;
time = zeros(1,n);
%x_next = x_previous;
time = linspace(0,r,n);

hold on
for i = 1:Nit
time = linspace(i*r,(i+1)*r,Nit);
y_next=rho.*x_previous.*(1-x_previous);
x_next = x_previous+ cumtrapz(y_next);
x_previous = x_next;
end
end
plot(rho,x_next(1),'.', 'Color', char(color_vec(j)))
end

When I execute the results, I found out that x and y go to starting from the second value for each? What's wrong in my code?
Moreover, I want to integrate ''y'' from nr to time; how can I do this using cumtrapz?

Thanks a lot.

Subject: problem with cumtrapz

From: someone

### someone (view profile)

Date: 7 Dec, 2012 18:51:08

Message: 2 of 4

"Sanaa" wrote in message <k9taac$ac1$1@newscl01ah.mathworks.com>...
> Hi,
> I want to integrate the discrete function a*x_n*(1-x_n) from nr to t (take r=0.005)
> > > where a:0:0.001:4 and n=0,1,2,3,...., and t\in[nr,(n+1) r]
> I have tried the follwoing
> % define the vector of colors, to plot the dat aof reach value of r in
> % different color
> color_vec = ['b'];
> r=0.05;
> for rho = 0: 0.001:5
> r_vec=r;
> % do a loop of values of r
> for j = 1 : size(r_vec,2)
> % define delay
> r = r_vec(j);
> % define coefficient rho
> %rho = 0.5;
> % define the number of deiscrete times in interval [k*r, (k+1)*r]
> n = 50;
> % define the nimber of iterations k = 1,.....,Nit
> Nit = 300;
> % define the initial vector which is a vector of size n
> x0 = 0.3*ones(1,n);
> % define vectors x_next and x_previous
> x_next = zeros(1,n); %x_(n+1)
> x_previous = zeros(1,n); %x_n
>
> time = zeros(1,n);
>
> % initialize x_previous
> x_previous = x0;
> time = zeros(1,n);
> %x_next = x_previous;
> time = linspace(0,r,n);
>
> hold on
> for i = 1:Nit
> time = linspace(i*r,(i+1)*r,Nit);
> y_next=rho.*x_previous.*(1-x_previous);
> x_next = x_previous+ cumtrapz(y_next);
> x_previous = x_next;
> end
> end
> plot(rho,x_next(1),'.', 'Color', char(color_vec(j)))
> end
>
> When I execute the results, I found out that x and y go to starting from the second value for each? What's wrong in my code?

I'm not sure I understand the question and I don't have MATLAB installed on this computer, but I don't think your for loop is incrementing properly. For example, from the above, look at the section:

r=0.05;
for rho = 0: 0.001:5
r_vec=r;
% do a loop of values of r
for j = 1 : size(r_vec,2)
% define delay
r = r_vec(j);

Since r & r_vec are scalars, the for loop will ALWAYS evaluate to:

for j = 1 : 1

(I don't see where the values of r or r_vec are changed anywhere else in your code.) Perhaps you mean to use rho in place of one (or more) of the r or r_vec variables.
Also, look at:

> % define the initial vector which is a vector of size n
> x0 = 0.3*ones(1,n);
> % define vectors x_next and x_previous
> x_next = zeros(1,n); %x_(n+1)
> x_previous = zeros(1,n); %x_n
>
> time = zeros(1,n);
>
> % initialize x_previous
> x_previous = x0;
> time = zeros(1,n);
> %x_next = x_previous;
> time = linspace(0,r,n);

Theres a lot of neeedless redundancy here. You define and overwrite time twice. Only the last statement is needed. Is that what you meant? Similar situation with x_previous. Perhaps its just extranious code leftover from debugging. You should cllean it up (or at least comment-out) the unneeded statements and resubmit your question if you still can't see an obvious problem.

> Moreover, I want to integrate ''y'' from nr to time; how can I do this using cumtrapz?
>
> Thanks a lot.

Subject: problem with cumtrapz

From: Sanaa

### Sanaa (view profile)

Date: 8 Dec, 2012 18:06:08

Message: 3 of 4

I am sorry for the miss forming of the code. Please see this
% define the vector of colors, to plot the data of reach value of r in
% different color
color_vec = ['b'];
r=0.05;
for rho = 0: 0.001:10
% define the number of discrete times in interval [k*r, (k+1)*r]
n = 50;
% define the number of iterations k = 1,.....,Nit
Nit = 150;
% define the initial vector which is a vector of size n
x0 = 0.3*ones(1,n);
% define vectors x_next and x_previous
x_next = zeros(1,n); %x_(n+1)
x_previous = zeros(1,n); %x_n
time = zeros(1,n);
% initialize x_previous
x_previous = x0;
x_next = x_previous;
hold on
for i = 1:Nit
time = linspace(i*r,(i+1)*r,Nit);
y_next=rho.*x_previous.*(1-x_previous);
x_next = x_previous+ cumtrapz(y_next);
x_previous = x_next;
end
end
plot(rho,x_next(1),'.', 'Color', char(color_vec))
Now I have only one point in the plot!! I also want to integrate from n*r to t!?

Subject: problem with cumtrapz

From: Sanaa

### Sanaa (view profile)

Date: 8 Dec, 2012 18:09:08

Message: 4 of 4

I am sorry for the miss forming of the code. Please see this
% define the vector of colors, to plot the data of reach value of r in
% different color
color_vec = ['b'];
r=0.05;
for rho = 0: 0.001:10
% define the number of discrete times in interval [k*r, (k+1)*r]
n = 50;
% define the number of iterations k = 1,.....,Nit
Nit = 150;
% define the initial vector which is a vector of size n
x0 = 0.3*ones(1,n);
% define vectors x_next and x_previous
x_next = zeros(1,n); %x_(n+1)
x_previous = zeros(1,n); %x_n
time = zeros(1,n);
% initialize x_previous
x_previous = x0;
x_next = x_previous;
hold on
for i = 1:Nit
time = linspace(i*r,(i+1)*r,Nit);
y_next=rho.*x_previous.*(1-x_previous);
x_next = x_previous+ cumtrapz(y_next);
x_previous = x_next;
end
end
plot(rho,x_next(1),'.', 'Color', char(color_vec))
Now I have only one point in the plot!! I also want to integrate from n*r to t!?