Solving ODEs with Matlab, with varying Parameters











up vote
1
down vote

favorite












Lets say I have a simple logistic equation



dx/dt = 2ax(1 - x/N)
where N is the carrying capacity, a is some growth rate, and both a and N are parameters I'd like to vary.



So what I want to do is to plot a 3D graph of my fixed point and the two parameters.



I understand how to find a fixed point of a single parameter.



Here is my sample code



function xprime = MyLogisticFunction(t,X) %% The ODE

% Parameters
N = 10 % Carrying Capacity
a = 0.5 % Growth Rate

x1prime = 2*a*X(1)*(1 - X(1)/N );

xprime = [x1prime ]';

end


Next my solver



% Initial Number 
x0 = 0.4;

%Time Window
tspan=[0 100];

[t,x]=ode45(@MyLogisticFunction,tspan,x0);

clf

x(end,1) % This gives me the fixed point for the parameters above.


So my real question is, how do I put a for loop across two functions, that allows me to vary a and N, so that I can plot out a 3D graph of a and N and my fixed point x*.



I've tried combining both functions into one .m file but it does not seem to work










share|improve this question






















  • What are the N and a information throughout time?
    – mehmet
    Dec 22 '14 at 14:41










  • I will vary it arbitrarily from say N = 10:1:20, a = 0.5:0.1:1.5
    – Candy Man
    Dec 22 '14 at 14:44















up vote
1
down vote

favorite












Lets say I have a simple logistic equation



dx/dt = 2ax(1 - x/N)
where N is the carrying capacity, a is some growth rate, and both a and N are parameters I'd like to vary.



So what I want to do is to plot a 3D graph of my fixed point and the two parameters.



I understand how to find a fixed point of a single parameter.



Here is my sample code



function xprime = MyLogisticFunction(t,X) %% The ODE

% Parameters
N = 10 % Carrying Capacity
a = 0.5 % Growth Rate

x1prime = 2*a*X(1)*(1 - X(1)/N );

xprime = [x1prime ]';

end


Next my solver



% Initial Number 
x0 = 0.4;

%Time Window
tspan=[0 100];

[t,x]=ode45(@MyLogisticFunction,tspan,x0);

clf

x(end,1) % This gives me the fixed point for the parameters above.


So my real question is, how do I put a for loop across two functions, that allows me to vary a and N, so that I can plot out a 3D graph of a and N and my fixed point x*.



I've tried combining both functions into one .m file but it does not seem to work










share|improve this question






















  • What are the N and a information throughout time?
    – mehmet
    Dec 22 '14 at 14:41










  • I will vary it arbitrarily from say N = 10:1:20, a = 0.5:0.1:1.5
    – Candy Man
    Dec 22 '14 at 14:44













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Lets say I have a simple logistic equation



dx/dt = 2ax(1 - x/N)
where N is the carrying capacity, a is some growth rate, and both a and N are parameters I'd like to vary.



So what I want to do is to plot a 3D graph of my fixed point and the two parameters.



I understand how to find a fixed point of a single parameter.



Here is my sample code



function xprime = MyLogisticFunction(t,X) %% The ODE

% Parameters
N = 10 % Carrying Capacity
a = 0.5 % Growth Rate

x1prime = 2*a*X(1)*(1 - X(1)/N );

xprime = [x1prime ]';

end


Next my solver



% Initial Number 
x0 = 0.4;

%Time Window
tspan=[0 100];

[t,x]=ode45(@MyLogisticFunction,tspan,x0);

clf

x(end,1) % This gives me the fixed point for the parameters above.


So my real question is, how do I put a for loop across two functions, that allows me to vary a and N, so that I can plot out a 3D graph of a and N and my fixed point x*.



I've tried combining both functions into one .m file but it does not seem to work










share|improve this question













Lets say I have a simple logistic equation



dx/dt = 2ax(1 - x/N)
where N is the carrying capacity, a is some growth rate, and both a and N are parameters I'd like to vary.



So what I want to do is to plot a 3D graph of my fixed point and the two parameters.



I understand how to find a fixed point of a single parameter.



Here is my sample code



function xprime = MyLogisticFunction(t,X) %% The ODE

% Parameters
N = 10 % Carrying Capacity
a = 0.5 % Growth Rate

x1prime = 2*a*X(1)*(1 - X(1)/N );

xprime = [x1prime ]';

end


Next my solver



% Initial Number 
x0 = 0.4;

%Time Window
tspan=[0 100];

[t,x]=ode45(@MyLogisticFunction,tspan,x0);

clf

x(end,1) % This gives me the fixed point for the parameters above.


So my real question is, how do I put a for loop across two functions, that allows me to vary a and N, so that I can plot out a 3D graph of a and N and my fixed point x*.



I've tried combining both functions into one .m file but it does not seem to work







matlab for-loop ode






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 22 '14 at 14:34









Candy Man

6911




6911












  • What are the N and a information throughout time?
    – mehmet
    Dec 22 '14 at 14:41










  • I will vary it arbitrarily from say N = 10:1:20, a = 0.5:0.1:1.5
    – Candy Man
    Dec 22 '14 at 14:44


















  • What are the N and a information throughout time?
    – mehmet
    Dec 22 '14 at 14:41










  • I will vary it arbitrarily from say N = 10:1:20, a = 0.5:0.1:1.5
    – Candy Man
    Dec 22 '14 at 14:44
















What are the N and a information throughout time?
– mehmet
Dec 22 '14 at 14:41




What are the N and a information throughout time?
– mehmet
Dec 22 '14 at 14:41












I will vary it arbitrarily from say N = 10:1:20, a = 0.5:0.1:1.5
– Candy Man
Dec 22 '14 at 14:44




I will vary it arbitrarily from say N = 10:1:20, a = 0.5:0.1:1.5
– Candy Man
Dec 22 '14 at 14:44












1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










You need to pass the parameters to your function:



function xprime = MyLogisticFunction(t,X,a,N) %% The ODE

% Parameters (passed as function arguments)
% N = 10 % Carrying Capacity
% a = 0.5 % Growth Rate

x1prime = 2*a*X(1)*(1 - X(1)/N );

xprime = [x1prime ]';

end


and then when you call the ode solver:



% Initial Number 
x0 = 0.4;

%Time Window
tspan=[0 100];

a = 0.1:0.1:1; % or whatever
N = 1:10; % or whatever
x_end = zeros(length(a),length(N));

for ii = 1:length(a)
for jj = 1:length(N)
[t,x]=ode45(@(t,X)MyLogisticFunction(t,X,a(ii),N(jj)),tspan,x0);
x_end(ii,jj) = x(end,1);
end
end





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27604755%2fsolving-odes-with-matlab-with-varying-parameters%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    You need to pass the parameters to your function:



    function xprime = MyLogisticFunction(t,X,a,N) %% The ODE

    % Parameters (passed as function arguments)
    % N = 10 % Carrying Capacity
    % a = 0.5 % Growth Rate

    x1prime = 2*a*X(1)*(1 - X(1)/N );

    xprime = [x1prime ]';

    end


    and then when you call the ode solver:



    % Initial Number 
    x0 = 0.4;

    %Time Window
    tspan=[0 100];

    a = 0.1:0.1:1; % or whatever
    N = 1:10; % or whatever
    x_end = zeros(length(a),length(N));

    for ii = 1:length(a)
    for jj = 1:length(N)
    [t,x]=ode45(@(t,X)MyLogisticFunction(t,X,a(ii),N(jj)),tspan,x0);
    x_end(ii,jj) = x(end,1);
    end
    end





    share|improve this answer

























      up vote
      5
      down vote



      accepted










      You need to pass the parameters to your function:



      function xprime = MyLogisticFunction(t,X,a,N) %% The ODE

      % Parameters (passed as function arguments)
      % N = 10 % Carrying Capacity
      % a = 0.5 % Growth Rate

      x1prime = 2*a*X(1)*(1 - X(1)/N );

      xprime = [x1prime ]';

      end


      and then when you call the ode solver:



      % Initial Number 
      x0 = 0.4;

      %Time Window
      tspan=[0 100];

      a = 0.1:0.1:1; % or whatever
      N = 1:10; % or whatever
      x_end = zeros(length(a),length(N));

      for ii = 1:length(a)
      for jj = 1:length(N)
      [t,x]=ode45(@(t,X)MyLogisticFunction(t,X,a(ii),N(jj)),tspan,x0);
      x_end(ii,jj) = x(end,1);
      end
      end





      share|improve this answer























        up vote
        5
        down vote



        accepted







        up vote
        5
        down vote



        accepted






        You need to pass the parameters to your function:



        function xprime = MyLogisticFunction(t,X,a,N) %% The ODE

        % Parameters (passed as function arguments)
        % N = 10 % Carrying Capacity
        % a = 0.5 % Growth Rate

        x1prime = 2*a*X(1)*(1 - X(1)/N );

        xprime = [x1prime ]';

        end


        and then when you call the ode solver:



        % Initial Number 
        x0 = 0.4;

        %Time Window
        tspan=[0 100];

        a = 0.1:0.1:1; % or whatever
        N = 1:10; % or whatever
        x_end = zeros(length(a),length(N));

        for ii = 1:length(a)
        for jj = 1:length(N)
        [t,x]=ode45(@(t,X)MyLogisticFunction(t,X,a(ii),N(jj)),tspan,x0);
        x_end(ii,jj) = x(end,1);
        end
        end





        share|improve this answer












        You need to pass the parameters to your function:



        function xprime = MyLogisticFunction(t,X,a,N) %% The ODE

        % Parameters (passed as function arguments)
        % N = 10 % Carrying Capacity
        % a = 0.5 % Growth Rate

        x1prime = 2*a*X(1)*(1 - X(1)/N );

        xprime = [x1prime ]';

        end


        and then when you call the ode solver:



        % Initial Number 
        x0 = 0.4;

        %Time Window
        tspan=[0 100];

        a = 0.1:0.1:1; % or whatever
        N = 1:10; % or whatever
        x_end = zeros(length(a),length(N));

        for ii = 1:length(a)
        for jj = 1:length(N)
        [t,x]=ode45(@(t,X)MyLogisticFunction(t,X,a(ii),N(jj)),tspan,x0);
        x_end(ii,jj) = x(end,1);
        end
        end






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 22 '14 at 14:44









        am304

        12.1k21431




        12.1k21431






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27604755%2fsolving-odes-with-matlab-with-varying-parameters%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin