differential equations – NDSolve. Can I use numerical ODE solution as PDE initial condition?

I want to use numerical solution of the ODE as an initial condition for the 1D linear PDE. But it seems Wolfram Mathematica has some issues with it.

Let’s consider an example. Here we have nonlinear ODE system.

s = NDSolve[{u'[x] == -3 v[x] + x, v'[x] == u[x] - v[x]^3, u[0] == -1,
    v[0] == 1}, {u, v}, {x, 0, 50}]

Then we use v[x] as an initial condition for 1D diffusion equation…

NDSolve[{D[H[x, t], t] == D[H[x, t], x, x], H[x, 2] == v[x]}, H, {t, 
  0, 10}, {x, 0, 10}]

… and receive the following error:

NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 2.`.

I know that

This message is generated when non-numerical expressions are encountered in a differential equation.

But what to do to solve such problem? I don’t want to substitute v[x] with an approximation.

Any way to write a system of three equations if two of them are ODE and the third on is a PDE?

recurrence relations – Numerical method to solve difference equation

I am solving the following problem:

Write a program to generate the first 60 terms in the sequence given by the difference equation: $$x_{k+1}=2.25x_{k}-0.5x_{k-1}$$
with starting values $x_1=frac{1}{3}, x_2=frac{1}{12}$. The exact solution is given by $x_k=frac{4^{1-k}}{3}$. Make a semilog plot of the values you obtain as a function of k.

My attemp:

My program (in Octave) is the following:

function x=diff
 n=60;
 x(1)=1/3;
 x(2)=1/12;
  for i=3:n
    x(i)=2.25*x(i-1)-0.5*x(i-2);
  endfor
   k=1:60;
 semilogy(k,x)
endfunction

I performed the operations with the solution that they give me in the problem and I obtained the following graph:
enter image description here

But I don’t know where the problem is, in my opinion my code is correct. I tried to see if the solution was not correct, but the general solution for the difference equation is the following: $x_k=c_1 4^{-k}+c_2 2^k$, and with the initial values it agrees with the one given in the exercise.

Beforehand thank you very much.

numerical value – How to extract angle $0 le varphi

Suppose you have a complex number of the standard shape $z = a + i , b$ (where $a$ and $b$ are real numbers). How can we convert it to the polar shape $z = A , e^{i varphi}$? More specifically, I would like to get a pair of positive numbers: $A$ and $varphi < 2pi$.

The command Abs(z) gives us the amplitude $A > 0$ (a real number). The command Arg(z) gives an angle, but apparently it’s not a always a positive value: $0 le varphi < 2pi$ (for example: Arg(-4.20 - 5.86 I) = -2.192, which is not what I need). So what should be the simplest way to get an angle $varphi$ that stays positive, and in the usual anti-clock sense of rotation?

numerical integration – NIntegrate results different when using 0 or 0.0

i was trying to compute a convolution numerically, but got unexpected results. In the end i was able to reduce it to the code below. As you can see, the results are different, depending on whether i write (0-x)^2, x^2 or (0.0-x)^2 (x is a real value). i have Mathematica 10.3.
can somebody explain that to me?
thank you very, very much for your help.
all my best wishes
esra

In(77):= NIntegrate(
 0.16076649110325864` E^(-9774.602659078126` Sqrt(    x^2)) (Erfc(-123.28828005937953` Sqrt(10^-4) + 
      10 Sqrt(110/7) Sqrt(x^2/10^-4)) - 
    E^(19549.20531815625` Sqrt(x^2))
      Erfc(123.28828005937953` Sqrt(10^-4) + 
       10 Sqrt(110/7) Sqrt(x^2/10^-4))) ((
    3 (0 - x)^2)/(0.000025` + (0 - x)^2)^(5/2) - 
    1/(0.000025` + (0 - x)^2)^(3/2)), {x, -Infinity, Infinity})

Out(77)= -410.318

In(84):= NIntegrate(
 0.16076649110325864` E^(-9774.602659078126` Sqrt(    x^2)) (Erfc(-123.28828005937953` Sqrt(10^-4) + 
      10 Sqrt(110/7) Sqrt(x^2/10^-4)) - 
    E^(19549.20531815625` Sqrt(x^2))
      Erfc(123.28828005937953` Sqrt(10^-4) + 
       10 Sqrt(110/7) Sqrt(x^2/10^-4))) ((
    3 (0.0 - x)^2)/(0.000025` + (0.0 - x)^2)^(5/2) - 
    1/(0.000025` + (0.0 - x)^2)^(3/2)), {x, -Infinity, Infinity})

Out(84)= -205.159

In(78):= NIntegrate(
 0.16076649110325864` E^(-9774.602659078126` Sqrt(    x^2)) (Erfc(-123.28828005937953` Sqrt(10^-4) + 
      10 Sqrt(110/7) Sqrt(x^2/10^-4)) - 
    E^(19549.20531815625` Sqrt(x^2))
      Erfc(123.28828005937953` Sqrt(10^-4) + 
       10 Sqrt(110/7) Sqrt(x^2/10^-4))) ((3 x^2)/(0.000025` + x^2)^(
    5/2) - 1/(0.000025` + x^2)^(3/2)), {x, -Infinity, Infinity})

Out(78)= -410.318

simplifying expressions – Simplification and numerical evaluation differ substantially

My question is very simple. I am trying to simplify an expression that I was expecting to be vanishing. However, If I numerically evaluate the expression (for large values of one parameter) I find 1, whereas if I simplify it I get 0, see the picture

enter image description here

Here’s the code to reproduce it

(m Sqrt(P + Sqrt(m^2 + P^2)) - Sqrt(-P + Sqrt(m^2 + P^2)) (P + Sqrt(m^2 + P^2))) 1/Sqrt(P + Sqrt(m^2 + P^2)) /. m -> 1 /. P -> 10^8 // N
(m Sqrt(P + Sqrt(m^2 + P^2)) - Sqrt(-P + Sqrt(m^2 + P^2)) (P + Sqrt(m^2 + P^2))) 1/Sqrt(P + Sqrt(m^2 + P^2)) // Simplify(#, {m > 0, P > 0}) &

What’s happening?

Equivalently, I have rescaled $mrightarrow x P$ and try to take the $xrightarrow 0^+$ limit. I get the error "Unable to determine whether expressions .... are equal to zero. Assuming they are."
enter image description here

Here’s the code

(m Sqrt(P + Sqrt(m^2 + P^2)) - Sqrt(-P + Sqrt(m^2 + P^2)) (P + Sqrt(m^2 + P^2))) 1/Sqrt(P + Sqrt(m^2 + P^2)) /. m -> x P // Simplify(#, P > 0) &
Limit(x - Sqrt((-1 + Sqrt(1 + x^2))/(1 + Sqrt(1 + x^2))) (1 + Sqrt(1 + x^2)), x -> 0,Direction -> "FromAbove")

This is strange because if I FullySimplify the expression under the limit, I get 0. What am I doing wrong?

numerics – How to use parallel computation in mathematica for numerical Integration

I have a multi dimensional integral and I need to integrate it numerically and than plot it over an extra variable for zeta = 0.5.

g(kx_, ky_, k1x_, k1y_, qx_, qy_, zeta_, zz_) := (1/((1 + Exp(-Sqrt(k1x^2 + k1y^2) + zeta*k1x*Cos(zz) + 
         zeta*k1y*Sin(zz)))*(1 + 
       Exp(-Sqrt(kx^2 + ky^2) + zeta*kx*Cos(zz) + 
         zeta*ky*Sin(zz)))*(1 + 
       Exp(Sqrt((kx + qx)^2 + (ky + qy)^2) + zeta*(kx + qx)*Cos(zz) + 
          zeta*(ky + qy)*Sin(zz))*(1 + 
          Exp(Sqrt((k1x - qx)^2 + (k1y - qy)^2) + 
            zeta*(k1x - qx)*Cos(zz) + zeta*(k1y - qy)*Sin(zz))))));
  PolarPlot(NIntegrate(g(kx, ky, k1x, k1y, qx, qy, 0.5, zz), {kx, -1, 1}, {ky, -1, 
     1}, {k1x, -1, 1}, {k1y, -1, 1}, {qx, -1, 1}, {qy, -1, 1}), {zz, 
    0, 2 Pi})

my problem is that, it take many hours (more than 10 hours) without answer. my solution was to calculate the integral for some discrete set of angles, and than plot it, but I am searching for more better way.
based on searching in site,

Parallelizing Numerical Integration in Mathematica

it may be possible to use parallel computation to plot it. my knowledge in Mathematica is limited and I couldn’t understand what the role of index “i” is in that link.
Would someone please tell me how can I use all cores to decrease the time of calculation.

thanks

numerical integration – Solving Fredholm Equation with composite unknown function

This equation can be solved with Bernoulli polynomials. First we substitute $y=x^2/2-1$, $t=s^2/2-1$, then define colocation points, solution and system of equations as follows

nN = 12; L = Pi^2/2 - 1; xcol = 
 Table(-1 + Pi^2/2 (j + 1/2)/nN, {j, 0, nN - 1}); 
v(x_) := Table(BernoulliB( n, x/L), {n, 0, nN - 1}); A = 
 Array(a, {nN}); u(x_) = A . v(x);
eqs = Table(
   u(xcol((i))) - 1 - 
    1/2 A . NIntegrate(
       v(t ) Cos(Sqrt(2 (1 + xcol((i)))) - Sqrt(2 (1 + t)))/
         Sqrt(2 (1 + t)), {t, -1, L}, AccuracyGoal -> 10), {i, 
    Length(xcol)});

Numerical solution can be evaluated in two step

sol = NMinimize(Norm(eqs), A)

sol1 = FindRoot(Table(eqs((i)) == 0, {i, Length(eqs)}), 
  Table({a(i), a(i) /. sol((2))}, {i, Length(A)}))

Finally we plot solution

Plot({u(x^2/2 - 1) /. sol1, u(x^2/2 - 1) /. sol((2))}, {x, 0, Pi}, 
 PlotRange -> All, Frame -> True, 
 PlotLegends -> {"FindRoot", "NMinimize"}, 
 FrameLabel -> {"x", "(Phi)"}, PlotLabel -> Row({"n = ", nN}))

Figure 1

calculus and analysis – Numerical Mixed Partial Derivatives: How to Choose Scale?

For illustration purposes, consider the function
$$ f(x, y) = (x – 1)^2 + (y – 1)^2 + sin(25(x+y)) . $$
The mixed partial derivatives at the origin are $f^{(x,x)}(0,0) = f^{(y,y)}(0,0) = 2$ and $f^{(x,y)}(0,0) = 0$ (the $sin$-term does not give any contribution).

I want to determine these derivatives numerically.
That is, let’s imagine I have only access to the function

f((x_)?NumericQ, (y_)?NumericQ) := (x - 1)^2 + (y - 1)^2 + Sin(25*(x + y))

as a numerical black box.
I would first try to solve the problem as follows:

N(D(f(x, y), {x}, {x}) /. x -> 0 /. y -> 0)
(* 2. *)

N(D(f(x, y), {y}, {y}) /. x -> 0 /. y -> 0)
(* 2. *)

N(D(f(x, y), {x}, {y}) /. x -> 0 /. y -> 0)
(* Derivative(1, 1)(f)(0., 0.) *)

In the first two cases, it just works.
However, for the mixed derivative, it is well known that the simple approach fails and one must use nested calls to ND instead. (To keep it short, I will do that the simple way, not using the trick described here to reduce the number of function calls.) Let’s try it:

Needs("NumericalCalculus`");
ND(f(x, 0), {x, 2}, 0)
(* 54.6838 *)

ND(f(0, y), {y, 2}, 0)
(* 54.6838 *)

ND(ND(f(x, y), x, 0), y, 0)
(* -8.40318 *)

Disaster!

The problem, of course, lies in the “scale” used for the differentiation.
We can explore how the results change as a function of the employed scale:

Table({
   ND(f(x, 0), {x, 2}, 0, Scale -> 10^-s),
   ND(f(0, y), {y, 2}, 0, Scale -> 10^-s),
   ND(ND(f(x, y), x, 0, Scale -> 10^-s), y, 0, Scale -> 10^-s)
   }, {s, 0, 6}
)

(*
{{54.6838, 54.6838, -8.40318},
 {2.00013, 2.00013, 7.46312*10^-6},
 {2., 2., -1.89464*10^-7},
 {2.00001, 2.00001, 9.40594*10^-6},
 {2.00063, 2.00063, 0.00106619},
 {2.08502, 2.08502, -0.0788629},
 {-5.68049, -5.68049, -20.9135}}
*)

As we can see, there is only a very narrow window for Scale where ND yields great results.
Yet, using N(D(...) /. ...) in the first example, the results for the non-mixed derivatives were perfect.
My question is: how does Mathematica manage to choose the scale perfectly using that approach?
Is there a way to make Mathematica automatically choose the correct scale also for the mixed derivatives?


Bonus question: could someone explain why the accuracy of the results starts to get worse at a scale of $10^{-3}$ already? I would have expected the accuracy to increase until we get close to the machine precision.
This works, for example:

h = 10^-8;
(f(h, 0) + f(-h, 0) - 2*f(0, 0))/h^2
(* 2 *)

numerical integration – NDSolve big system non linear differential first order coupled equations time constraint exceeded

I’m trying to develop a continuous time unscented Kalman filter for a dynamic system inspired to this article: “On Unscented Kalman Filtering for State Estimation of Continuous-Time Nonlinear Systems” of the author Simo Särkkä. In particular the Square Root one.

My systems has dimension 5 so I have a system of 11 x 5 + 15 non linear coupled first order differential equations.

The code of my notebook is very long and laborious, in the end I simulate a dynamic system and the filter receive the output.

When in the end I try to solve the system with NDSolve it says:

enter image description here

If I try with the suggest the NDSolve runs infinite time with no solutions even if I put t0 = 0 and tend = 0.04 for the interval.

Someone knows if there is something I could do or is just the fact that a system of 70 non linear coupled first order differential equations is too much for the system ?

I’ll make a short version of my code to make the question clearer.

numerical value – N won’t evaluate expression numerically

Using N(expr) is not evaluating this expression numerically for me and I can’t figure out why. I originally thought it’s because I wasn’t doing NIntegrate but I’ve seen multiple examples of people using N(integral expr) to get a numerical result.

c1 = 1/(Integrate(e^(-0.04 x), {x, 5, 60}));
c2 = 1/(Integrate(e^(-0.16 x), {x, 5, 60}));
f1(x_) := c1*e^(-0.04 x);
f2(x_) := c2 * e^(-0.16 x);
P1 = N(f1(10)*f1(32)*f1(38)*f1(40))
P2 = N(f2(10)*f2(32)*f2(38)*f2(40))

out $$frac{log ^4(e)}{left(frac{25.}{e^{0.2}}-frac{25.}{e^{2.4}}right)^4 e^{4.8}}$$
out $$frac{log ^4(e)}{left(frac{6.25}{e^{0.8}}-frac{6.25}{e^{9.6}}right)^4 e^{19.2}}$$

I’ve tried evaluating it numerically in different stages like at c1, or in the function and still no numerical result. Any help would be appreciated