I am solving PDEs with NDSolve and NIntegrate, but I do not know how to pass arguments correctly. My orignial code is very complicated, so I simplfied it to clarify the problems I met.
Firstly, I solved the simplified code without passing the arguments:
ClearAll[x, y,r, t]; ss = NDSolve[{ x'[t] == (NIntegrate[x[t]rr, {r, 0, 5}] + y[t] + t)*y[t], y'[t] == -x[t], x[0] == 1, y[0] == 1}, {x, y}, {t, 10}]; Plot[Evaluate[{x[t], y[t]} /. ss], {t, 0, 10}]
Then I used the skil of passing arguments to solve the problems, and the code becomes:
ClearAll[x, y, z, zz, t]; u[x_, r_] := xr; v[u_?NumericQ] := NIntegrate[ur, {r, 0, 5}]; z[v_, y_, t_] := v + y + t;
s = NDSolve[{ x'[t] == z[t, t, t]*y[t], y'[t] == -x[t], x[0] == 1, y[0] == 1}, {x, y}, {t, 10}]; Plot[Evaluate[{x[t], y[t]} /. s], {t, 0, 10}] But I got different and wrong results. Why?
I also tried to pass the arguments in another way, but I just got the error messages. Why?
ClearAll[x, y, u, v, z, zz, t, f]; u[x_, r_] := xr; v[x_?NumberQ] := NIntegrate[ur, {r, 0, 5}]; z[v_, y_, t_] := v + y + t;
s = NDSolve[{ x'[t] == z[v[u[x[t]]], y[t], t]*y[t], y'[t] == -x[t], x[0] == 1, y[0] == 1}, {x, y}, {t, 10}];
I also tried another way to pass the arguments, and the result is correct and same to 1). But I prefer not to use it due to my complicated original code.
ClearAll[x, y, u, v, z, zz, t, f]; u[x_, r_] := xr; v[x_?NumberQ] := NIntegrate[ur, {r, 0, 5}]; f = NIntegrate[u[x[t], r]*r, {r, 0, 5}]; z[v_, y_, t_] := v + y + t;
s = NDSolve[{x'[t] == z[f, y[t], t]*y[t], y'[t] == -x[t], x[0] == 1, y[0] == 1}, {x, y}, {t, 10}];
Plot[Evaluate[{x[t], y[t]} /. s], {t, 0, 10}]
How to pass arguments in complicated codes correctly?
The original equations look like this. They are integration-differential equations.
The only independent variables is z, the aim is to solve $\nu$[z], qr[z] and qi[z] from z=0 to z.
The parameters are: A[z], B[z], D[z], d[z], and ne[$\rho$,z], $\gamma$[$\rho$,z], $\epsilon$r[$\rho$,z] and $\epsilon$i[$\rho$,z]. The integration of $\rho$ ranges from d to infinite.
Equations:
I wrote the code with error messages: NDSolve::ndnum: Encountered non-numerical value for a derivative at z == 0.1.
From equations to code, different names are used:
$\rho$ -->r, D-->D0, d-->dmin. $\gamma$ -->$\gamma_0$
ClearAll[ν, qr, qi, r, γ, ne, ϵr, ϵi, dmin, A, B, D0, aL, Zni, νei];
aL = 10;
Zni = 0.001;
νei = 0.001;
γ= (1 + aL^2*ν^2*Exp[-2*qr*r^2])^0.5;
ne= Zni - 4*qr*γ*(1 -γ^(-2))*(1 - qr*r^2*(1 + γ^(-2)));
ϵr= 1 - ne/γ;
ϵi= 4*Pi*νei*ne;
dmin= Re[qr^(-0.5)*(-0.5*Log[Zni/(8*aL^2*ν^2*qr) (1+sqrt[4+(Zni/(4*qr))^2])])^0.5];
D0[ne_?NumericQ, qr_?NumericQ] := NIntegrate[16*Pi*νei*ne*Exp[-2*qr*r^2]*qr*r, {r, dmin, Infinity}];
A[ν_?NumericQ, qr_?NumericQ, ϵr_?NumericQ, qi_?NumericQ,ϵi_?NumericQ, dmin_?NumericQ] :=
ν*NIntegrate[r*Exp[-qr*r^2]*((1-ϵr)*Sin[qi*r^2]-ϵi*Cos[qi*r^2]), {r, dmin, Infinity}];
B[ν_?NumericQ, qr_?NumericQ, ϵr_?NumericQ, qi_?NumericQ,ϵi_?NumericQ, dmin_?NumericQ] :=
ν*NIntegrate[r*Exp[-qr*r^2]*((1-ϵr)*Cos[qi*r^2]+ϵi*Sin[qi*r^2]), {r, dmin, Infinity}];
s = NDSolve[{
ν'[z] == -A[ν[z], qr[z], ϵr,qi[z], ϵi, dmin]qr[z] - D0[ne, qr[z]]ν[z],
qr'[z] == -2 A[ν[z], qr[z], ϵr,qi[z], ϵi, dmin]qr[z]^2/ν[z]-D0[ne,qr[z]]qr[z],
qi'[z] == -3 A[ν[z], qr[z], ϵr,qi[z], ϵi, dmin]qr[z]qi[z]/ν[z] +
B[ν[z], qr[z], ϵr,qi[z], ϵi, dmin]qr[z]^2/ν[z] -
D0[ne, qr[z]]qi[z],
ν[0.1] == 1., qr[0.1] == 10^(-4), qi[0.1] == 0.}, {ν, qr, qi}, {z, 0.1, 10000.}]


z[t, t, t]separately and think about why this happens. You may also want to read this question. Also,x[t]should be taken out ofNIntegratei.e. it should bex[t] NIntegrate[r*r, {r, 0, 5}]. – xzczd Jun 13 '16 at 06:51xis a function ofr, so it wasx[r]notx[t]. – sixpenny Jun 13 '16 at 07:11NDSolvewill handle your code correctly. As far as I can tell,NDSolveisn't able to directly solve this type of integro-differential equation (at least currently). – xzczd Jun 13 '16 at 07:27NDSolvecan't directly handle this type of DE, Mathematica can solve several types of integro-differential equation (yeah there's a variety of integro-differential equations!) with just a little additional programming, for example this, this, this... – xzczd Jun 13 '16 at 15:07NDSolve!) is easy in Mathematica, too, for example this , but I really doubt if such a pedagogic algorithm (Yeah RK4 is just a relatively practical pedagogic algorithm!) will be able to handle a really complicated DE system.(You may want to read this). Finally, with all due respect, though the learning curve of MMA is deemed to be steep, if you've really learned MMA for weeks, I should say you're... – xzczd Jun 13 '16 at 15:23z[t, t, t]in 1) and 2), and they are the same. I still do not know why, 1) and 2) give different results. I will post my original complicated questions elsewhere. – sixpenny Jun 20 '16 at 02:16z[t, t, t]isn't used in 1) ? – xzczd Jun 20 '16 at 03:07z[t,t,t]in 2) and 4). – sixpenny Jun 20 '16 at 04:45z[f, y[t], t]in 4). If you still have difficulty in understanding, evaluatez[aa, bb, cc],z[t1, t2, t3],z[ff, yy, zz]and compare the result. – xzczd Jun 20 '16 at 05:21xis a function ofr" is wrong, and your equation set turns out to be one of the simplest type of integro-differential equation, which can indeed be solved by the?NumericQtechnique. I'm a little busy at the moment, but will probably write an answer within several hours. – xzczd Jun 20 '16 at 08:45