Solving an ODE containing a function of the independent variable only known at discrete points

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I'm a little new at Mathematica and I'm trying to solve the following ordinary differential equation:



$qquad xfracdy(x)dx+y=-p(x)$.



I have data (in a list) for $x$, and the corresponding values of $p(x)$ (also in a list). I need to solve the ODE to get a list $y(x)$ for the $x$s in the 1st list. I've been trying to use NDSolve, like so:



NDSolve[x*y'[x] + y[x] == -p, y, x, a, b]


with no luck.



Is this possible in Mathematica? Is there a way to do this?










share|improve this question























  • In other words are you trying to fit the differential equation to your data ?
    – Lotus
    Sep 5 at 3:33










  • @Lotus yes I guess so!
    – zack
    Sep 5 at 3:40










  • Then here is what you do: Create a cost function with your differential equation (NDSolve etc..) and the Norm between the solution and your data. Use NMinimize to minimize the cost function to get y(x) which should be a good fit
    – Lotus
    Sep 5 at 3:42










  • @Lotus This is a misunderstanding. If I am not mistaken, OP has just some discrete data instead a function for the excitations p (the right hand side) and wants to simply solve the ODE for y.
    – Henrik Schumacher
    Sep 5 at 6:20














up vote
2
down vote

favorite












I'm a little new at Mathematica and I'm trying to solve the following ordinary differential equation:



$qquad xfracdy(x)dx+y=-p(x)$.



I have data (in a list) for $x$, and the corresponding values of $p(x)$ (also in a list). I need to solve the ODE to get a list $y(x)$ for the $x$s in the 1st list. I've been trying to use NDSolve, like so:



NDSolve[x*y'[x] + y[x] == -p, y, x, a, b]


with no luck.



Is this possible in Mathematica? Is there a way to do this?










share|improve this question























  • In other words are you trying to fit the differential equation to your data ?
    – Lotus
    Sep 5 at 3:33










  • @Lotus yes I guess so!
    – zack
    Sep 5 at 3:40










  • Then here is what you do: Create a cost function with your differential equation (NDSolve etc..) and the Norm between the solution and your data. Use NMinimize to minimize the cost function to get y(x) which should be a good fit
    – Lotus
    Sep 5 at 3:42










  • @Lotus This is a misunderstanding. If I am not mistaken, OP has just some discrete data instead a function for the excitations p (the right hand side) and wants to simply solve the ODE for y.
    – Henrik Schumacher
    Sep 5 at 6:20












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm a little new at Mathematica and I'm trying to solve the following ordinary differential equation:



$qquad xfracdy(x)dx+y=-p(x)$.



I have data (in a list) for $x$, and the corresponding values of $p(x)$ (also in a list). I need to solve the ODE to get a list $y(x)$ for the $x$s in the 1st list. I've been trying to use NDSolve, like so:



NDSolve[x*y'[x] + y[x] == -p, y, x, a, b]


with no luck.



Is this possible in Mathematica? Is there a way to do this?










share|improve this question















I'm a little new at Mathematica and I'm trying to solve the following ordinary differential equation:



$qquad xfracdy(x)dx+y=-p(x)$.



I have data (in a list) for $x$, and the corresponding values of $p(x)$ (also in a list). I need to solve the ODE to get a list $y(x)$ for the $x$s in the 1st list. I've been trying to use NDSolve, like so:



NDSolve[x*y'[x] + y[x] == -p, y, x, a, b]


with no luck.



Is this possible in Mathematica? Is there a way to do this?







list-manipulation differential-equations equation-solving ode






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 5 at 5:36









m_goldberg

82k869190




82k869190










asked Sep 5 at 3:10









zack

183




183











  • In other words are you trying to fit the differential equation to your data ?
    – Lotus
    Sep 5 at 3:33










  • @Lotus yes I guess so!
    – zack
    Sep 5 at 3:40










  • Then here is what you do: Create a cost function with your differential equation (NDSolve etc..) and the Norm between the solution and your data. Use NMinimize to minimize the cost function to get y(x) which should be a good fit
    – Lotus
    Sep 5 at 3:42










  • @Lotus This is a misunderstanding. If I am not mistaken, OP has just some discrete data instead a function for the excitations p (the right hand side) and wants to simply solve the ODE for y.
    – Henrik Schumacher
    Sep 5 at 6:20
















  • In other words are you trying to fit the differential equation to your data ?
    – Lotus
    Sep 5 at 3:33










  • @Lotus yes I guess so!
    – zack
    Sep 5 at 3:40










  • Then here is what you do: Create a cost function with your differential equation (NDSolve etc..) and the Norm between the solution and your data. Use NMinimize to minimize the cost function to get y(x) which should be a good fit
    – Lotus
    Sep 5 at 3:42










  • @Lotus This is a misunderstanding. If I am not mistaken, OP has just some discrete data instead a function for the excitations p (the right hand side) and wants to simply solve the ODE for y.
    – Henrik Schumacher
    Sep 5 at 6:20















In other words are you trying to fit the differential equation to your data ?
– Lotus
Sep 5 at 3:33




In other words are you trying to fit the differential equation to your data ?
– Lotus
Sep 5 at 3:33












@Lotus yes I guess so!
– zack
Sep 5 at 3:40




@Lotus yes I guess so!
– zack
Sep 5 at 3:40












Then here is what you do: Create a cost function with your differential equation (NDSolve etc..) and the Norm between the solution and your data. Use NMinimize to minimize the cost function to get y(x) which should be a good fit
– Lotus
Sep 5 at 3:42




Then here is what you do: Create a cost function with your differential equation (NDSolve etc..) and the Norm between the solution and your data. Use NMinimize to minimize the cost function to get y(x) which should be a good fit
– Lotus
Sep 5 at 3:42












@Lotus This is a misunderstanding. If I am not mistaken, OP has just some discrete data instead a function for the excitations p (the right hand side) and wants to simply solve the ODE for y.
– Henrik Schumacher
Sep 5 at 6:20




@Lotus This is a misunderstanding. If I am not mistaken, OP has just some discrete data instead a function for the excitations p (the right hand side) and wants to simply solve the ODE for y.
– Henrik Schumacher
Sep 5 at 6:20










2 Answers
2






active

oldest

votes

















up vote
5
down vote













Why not try to fit p(x) to the data and then using DSolve? Since you did not make a MWE, I made up some data



ClearAll[x, y, p]
xData = 1, 2, 3, 4;
pData = 1, 4, 9, 16;
data = Transpose[xData, pData];
p[x_] = Fit[data, 1, x, x^2, x]; (*change fit as needed*)
DSolve[x y'[x] + y[x] == -p[x], y[x], x]


Mathematica graphics






share|improve this answer
















  • 2




    Should the data be free of noise, using Interpolation instead of Fit also comes to mind.
    – Henrik Schumacher
    Sep 5 at 5:30

















up vote
2
down vote













It is a very simple equation and I recommend to first solve it analytically. It can be done by the, say, the so-called, u*v method. The solution is as follows:



enter image description here



Now one can directly integrate it numerically by summing up the areas of the trapezoids formed by the points x1,x2,p1 and p2. Alternatively, one can use the advice of @Nasser and integrate the fitted function, which is easier.



Have fun!






share|improve this answer




















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "387"
    ;
    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: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181247%2fsolving-an-ode-containing-a-function-of-the-independent-variable-only-known-at-d%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote













    Why not try to fit p(x) to the data and then using DSolve? Since you did not make a MWE, I made up some data



    ClearAll[x, y, p]
    xData = 1, 2, 3, 4;
    pData = 1, 4, 9, 16;
    data = Transpose[xData, pData];
    p[x_] = Fit[data, 1, x, x^2, x]; (*change fit as needed*)
    DSolve[x y'[x] + y[x] == -p[x], y[x], x]


    Mathematica graphics






    share|improve this answer
















    • 2




      Should the data be free of noise, using Interpolation instead of Fit also comes to mind.
      – Henrik Schumacher
      Sep 5 at 5:30














    up vote
    5
    down vote













    Why not try to fit p(x) to the data and then using DSolve? Since you did not make a MWE, I made up some data



    ClearAll[x, y, p]
    xData = 1, 2, 3, 4;
    pData = 1, 4, 9, 16;
    data = Transpose[xData, pData];
    p[x_] = Fit[data, 1, x, x^2, x]; (*change fit as needed*)
    DSolve[x y'[x] + y[x] == -p[x], y[x], x]


    Mathematica graphics






    share|improve this answer
















    • 2




      Should the data be free of noise, using Interpolation instead of Fit also comes to mind.
      – Henrik Schumacher
      Sep 5 at 5:30












    up vote
    5
    down vote










    up vote
    5
    down vote









    Why not try to fit p(x) to the data and then using DSolve? Since you did not make a MWE, I made up some data



    ClearAll[x, y, p]
    xData = 1, 2, 3, 4;
    pData = 1, 4, 9, 16;
    data = Transpose[xData, pData];
    p[x_] = Fit[data, 1, x, x^2, x]; (*change fit as needed*)
    DSolve[x y'[x] + y[x] == -p[x], y[x], x]


    Mathematica graphics






    share|improve this answer












    Why not try to fit p(x) to the data and then using DSolve? Since you did not make a MWE, I made up some data



    ClearAll[x, y, p]
    xData = 1, 2, 3, 4;
    pData = 1, 4, 9, 16;
    data = Transpose[xData, pData];
    p[x_] = Fit[data, 1, x, x^2, x]; (*change fit as needed*)
    DSolve[x y'[x] + y[x] == -p[x], y[x], x]


    Mathematica graphics







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 5 at 3:46









    Nasser

    56.7k485203




    56.7k485203







    • 2




      Should the data be free of noise, using Interpolation instead of Fit also comes to mind.
      – Henrik Schumacher
      Sep 5 at 5:30












    • 2




      Should the data be free of noise, using Interpolation instead of Fit also comes to mind.
      – Henrik Schumacher
      Sep 5 at 5:30







    2




    2




    Should the data be free of noise, using Interpolation instead of Fit also comes to mind.
    – Henrik Schumacher
    Sep 5 at 5:30




    Should the data be free of noise, using Interpolation instead of Fit also comes to mind.
    – Henrik Schumacher
    Sep 5 at 5:30










    up vote
    2
    down vote













    It is a very simple equation and I recommend to first solve it analytically. It can be done by the, say, the so-called, u*v method. The solution is as follows:



    enter image description here



    Now one can directly integrate it numerically by summing up the areas of the trapezoids formed by the points x1,x2,p1 and p2. Alternatively, one can use the advice of @Nasser and integrate the fitted function, which is easier.



    Have fun!






    share|improve this answer
























      up vote
      2
      down vote













      It is a very simple equation and I recommend to first solve it analytically. It can be done by the, say, the so-called, u*v method. The solution is as follows:



      enter image description here



      Now one can directly integrate it numerically by summing up the areas of the trapezoids formed by the points x1,x2,p1 and p2. Alternatively, one can use the advice of @Nasser and integrate the fitted function, which is easier.



      Have fun!






      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        It is a very simple equation and I recommend to first solve it analytically. It can be done by the, say, the so-called, u*v method. The solution is as follows:



        enter image description here



        Now one can directly integrate it numerically by summing up the areas of the trapezoids formed by the points x1,x2,p1 and p2. Alternatively, one can use the advice of @Nasser and integrate the fitted function, which is easier.



        Have fun!






        share|improve this answer












        It is a very simple equation and I recommend to first solve it analytically. It can be done by the, say, the so-called, u*v method. The solution is as follows:



        enter image description here



        Now one can directly integrate it numerically by summing up the areas of the trapezoids formed by the points x1,x2,p1 and p2. Alternatively, one can use the advice of @Nasser and integrate the fitted function, which is easier.



        Have fun!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 5 at 7:36









        Alexei Boulbitch

        20.4k2369




        20.4k2369



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181247%2fsolving-an-ode-containing-a-function-of-the-independent-variable-only-known-at-d%23new-answer', 'question_page');

            );

            Post as a guest













































































            這個網誌中的熱門文章

            How to combine Bézier curves to a surface?

            Mutual Information Always Non-negative

            Why am i infinitely getting the same tweet with the Twitter Search API?