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

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



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


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?





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





@Lotus yes I guess so!
– zack
Sep 5 '18 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 '18 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 '18 at 6:20


p


y




2 Answers
2



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


p(x)


DSolve


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





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


Interpolation


Fit



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!



Thanks for contributing an answer to Mathematica Stack Exchange!



But avoid



Use MathJax to format equations. MathJax reference.



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:



But avoid



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



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ