Thread Subject:
a function problem

Subject: a function problem

From: Sanaa

Date: 12 May, 2012 11:46:30

Message: 1 of 4

Hi,
I am having a problem with my code. I need r=0.1 to be multiplied by the iteration index''n'' in the loop. I know matlab doesn't accept fractions, only integers can exist in the loop.
I am plotting a bifurcation daigram for the riccati's delayed difference equation
x(t)_((n+1)*r) = 1- ru*x^2(t)_((n*r)) ,
n=0,1,2,.... r=0.1, t \in(n*r, (n+1)*r)

for that I have a function file

function [f]=bifurcation(n,t,ru)
f=0.3;
for i=1:n
        f=1- ru*f^2;
end
end

and an M-file

clear
itermax=200;
finalits=30;finits=itermax-(finalits-1);
for ru=0:0.005:2;
    x=0.1;
    xo=x; r=0.2;
    for n=1:itermax % for n steps
        t = n*r:0.001:(n+1)*r; % assuming the step : 0.001
        for i=1:length(t) % for t steps (2nd dimension)
            % should compute x for every combination of (t(i), n)
      x(n)=bifurcation(n,t,ru);
        end
    end
    plot(ru*ones(finalits),x(finits:itermax),'.','MarkerSize',1)
    hold on
end
fsize=15;
set(gca,'XTick',0:0.5:4,'FontSize',fsize)
set(gca,'YTick',-1:0.5:2)
xlabel('{ru}','FontSize',fsize)
ylabel('x','FontSize',fsize)
hold off
How to plug in ''r'' so it will be multiplied by n?
Any help will be appreciated.
Thanks a lot.

Subject: a function problem

From: dpb

Date: 13 May, 2012 14:19:10

Message: 2 of 4

On 5/12/2012 6:46 AM, Sanaa wrote:
> Hi,
> I am having a problem with my code. I need r=0.1 to be multiplied by the
> iteration index''n'' in the loop. I know matlab doesn't accept
> fractions, only integers can exist in the loop.

Well, that's not so altho array indices can only be integer-valued,
yes...but certainly loops can operate on floating point values...

 >> for x=0:.2:1,disp(x),end
      0
     0.2000
     0.4000
     0.6000
     0.8000
      1
 >>

(Oh, I see later on you used this ability anyway; so why you wrote the
question I've now no clue...)

> bifurcation daigram for the riccati's delayed difference equation
> x(t)_((n+1)*r) = 1- ru*x^2(t)_((n*r)) ,
> n=0,1,2,.... r=0.1, t \in(n*r, (n+1)*r)
>
> for that I have a function file
>
> function [f]=bifurcation(n,t,ru)
> f=0.3;
> for i=1:n
> f=1- ru*f^2;
> end
> end

Why is there a 't' in the argument list and no 't' in the function?


> and an M-file
>
> itermax=200;
> finalits=30;
   finits=itermax-(finalits-1);
> for ru=0:0.005:2;
> x=0.1;
> xo=x;
     r=0.2;
> for n=1:itermax
> t = n*r:0.001:(n+1)*r;
> for i=1:length(t)
> % should compute x for every combination of (t(i), n)
> x(n)=bifurcation(n,t,ru);
> end
> end
...
> end
...

> How to plug in ''r'' so it will be multiplied by n?

"Plug it in" where, precisely?

I've had not success in trying to decipher what it is you're actually
after in the time I've taken (which isn't a lot, but I just can't seem
to get any traction every time I try, sorry).

But, what about a variable rn that is used wherever it is that you want
r*n that is updated each pass through the loop?
   ...
   rn=r;
   for n=1:itermax
     rn=rn*n;
     %wherever r*n is wanted use rn...
     ...
   end
   ...

--

Subject: a function problem

From: Sanaa

Date: 13 May, 2012 19:04:07

Message: 3 of 4

Thanks a lot for your reply. I hope I explain it more clearly to you. I need to multiply (n+1) in the loop by r=0.2 ' or 0.3, 1.5,....' .So, I need a trick to let matlab accept the iteration in a fractional way.
I modified the code. Please see if there any problem

function [f]=bifurcation(n,t,ru)
f=0.3;r=0.2;
for i=1:n*r
        f=1- ru*f^2;
end
end
which I call in the m-file
clear
itermax=60;
finalits=30;finits=itermax-(finalits-1);
for ru=0:0.005:2;
    x=0.3;
    xo=x; r=0.2;
    for n=1:itermax % for n steps
        t = n*r:0.005:(n+1)*r; % assuming the step : 0.001
        for i=1:length(t) % for t steps (2nd dimension)
            % should compute x for every combination of (t(i), n)
      x(n)=bifurcation(n,t,ru);
        end
    end
    plot(ru*ones(finalits),x(finits:itermax),'b.','linewidth',2)
    hold on
end
fsize=15;
set(gca,'XTick',0:0.5:4,'FontSize',fsize)
set(gca,'YTick',-1:0.5:2)
xlabel('{ru}','FontSize',fsize)
ylabel('x','FontSize',fsize)
title('bifurcation of Reccati delayed equation r=0.2,n=60')
hold off

Do I have to put t in the function inputs? I mean when declaring f should it be like that?
function [f]=bifurcation(n,t,ru)? or what?

I really appreciate your help.
Thanks a lot.

Subject: a function problem

From: dpb

Date: 13 May, 2012 20:41:46

Message: 4 of 4

On 5/13/2012 2:04 PM, Sanaa wrote:
> Thanks a lot for your reply. I hope I explain it more clearly to you. I
> need to multiply (n+1) in the loop by r=0.2 ' or 0.3, 1.5,....' .So, I
> need a trick to let matlab accept the iteration in a fractional way.
> I modified the code. Please see if there any problem
>
> function [f]=bifurcation(n,t,ru)
> f=0.3;r=0.2;
> for i=1:n*r

Well, no, that undoubtedly isn't what you want (altho I still have a
hard time following what you really do want and why the previous "trick"
of a temporary/intermediate variable doesn't solve whatever the problem is).

The 1:n*r in the for loop iteration control expression is the same thing
as writing 1:1:fix(n*r). If you're trying to control the upper limit on
the loop it _might_ be what you want: I don't know.

> f=1- ru*f^2;
> end
...

> Do I have to put t in the function inputs? I mean when declaring f
> should it be like that?
> function [f]=bifurcation(n,t,ru)? or what?
...

Well, it doesn't hurt but what's the point of having t in the argument
list and passing it if you're not going to use it in the function?

Again, I can't answer to your intent; only can point out that it's a
curious thing to write as you have done.

If you really don't need t and it's not an oversight in the function
then there's no reason the function definition couldn't be

function [f]=bifurcation(n,ru)

and the call would have only two arguments, too, of course.

--

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us