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.
1) Firstly, I solved the simplified code without passing the arguments:
ClearAll[x, y,r, t];
ss = NDSolve[{
x'[t] == (NIntegrate[x[t]*r*r, {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}]
2) 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_] := x*r;
v[u_?NumericQ] := NIntegrate[u*r, {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?
3) 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_] := x*r;
v[x_?NumberQ] := NIntegrate[u*r, {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}];
4) 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_] := x*r;
v[x_?NumberQ] := NIntegrate[u*r, {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:
[![enter image description here][2]][2]
* *
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.}]
[1]: https://i.sstatic.net/iBd48.png
[2]: https://i.sstatic.net/Tv2s9.png