Using DSolve for a real part of a differential equation (Mathematica) - wolfram-mathematica

I try to solve a differential equation (using DSolve) of the form:
Sys = {(Re[a T'[x] + b T[x]] == 0), T[1] == 3};
Sol = DSolve[Sys, T[x], x];
Plot[T[x]/.Sol, {x, 0, 1}
Where 'a' and 'b' are complex coefficients (non dependent of x).
I know this can't be the right syntax but I don't seem to find the right one anywhere. I can, of course, do the following:
Sys = {(a T'[x] + b T[x]] == 0), T[1] == 3};
Sol = DSolve[Sys, T[x], x];
Plot[Re[T[x]]/.Sol, {x, 0, 1}]
But this doesn't necessarily give me the same solution. How do I force Mathematica to take only the real part and solve the differential equation?
Thanks.

Related

Speed up symbolic claculation with mathematica

I have wirtten the following code. It is a code for mathematica and I would like to do some "simple" linear algebra with symbols.
The code sets up a matrix (called A) and a vector (called b). Then it solves the euqation A*k=b for k.
Unfortunately, my code is super slow, e.g. for n=5 it takes hours.
Is there any better way for solving this problem? I am not that familiar with mathematica and my code is rather unprofessional, so do you have any hints for speeding things up?
Here is my code.
clear[all];
n = 3;
MM = Table[Symbol["M" <> ToString#i], {i, 1, n}];
RB = Table[
Symbol["RA" <> FromCharacterCode[65 + i] <> ToString#(i + 1)], {i,
1, n - 1}];
mA = Table[Symbol["mA" <> FromCharacterCode[65 + i]], {i, 1, n - 1}];
mX = Table[
Symbol["m" <> FromCharacterCode[65 + i] <> "A"], {i, 1, n - 1}];
R = Table[
Symbol["R" <> FromCharacterCode[64 + i] <> ToString#(j + 1)], {i,
1, n}, {j, 1, n - 1}];
b = Table[-MM[[1]]*(1/(mA[[i]]*(R[[1, i]] - RB[[i]])) -
1/(mX[[i]]*(-R[[i + 1, i]] + RB[[i]]))), {i, 1, n - 1}];
A = Table[
MM[[j + 1]]*(R[[1, j]]/(mA[[i]]*(R[[1, i]] - RB[[i]])) -
R[[i + 1, j]]/(mX[[i]]*(-R[[i + 1, i]] + RB[[i]]))), {i, 1,
n - 1}, {j, 1, n - 1}];
K = LinearSolve[A, b];
MatrixForm[K]
Thanks for any hints!
P.S. The code should run!
You have lots of variables and lots of denominators, both of which can often make things very slow.
Let's try a simpler faster method that solves a generic form of your problem and then substitutes in all your variables and denominators.
n = 5;
MM = ...
...
A = ...
m={{m1,m2,m3,m4},{m5,m6,m7,m8},{m9,m10,m11,m12},{m13,m14,m15,m16}};
sol=Inverse[m].b/.Thread[Rule[Flatten[m],Flatten[A]]]
which gives a solution in fraction of a second. But you need to carefully check this to justify that there are no zero denominators hiding in your problem or this solution.
This method is faster than Inverse[A].b and far faster than LinearSolve[A, b] for your problem, but that time is only for the calculation of the solution and does not include any potentially large amount of time spent using the solution. It also does not include any of the programming hidden inside LinearSolve to deal with potential problems and special cases.
But I am not certain as your n grows larger and your forest of denominators grows far larger that this will continue to be fast or feasible.
Test this carefully before you assume everything works.
P.S. Thank you for the code that actually ran! (I didn't even use the clear[all])

Wolfram Mathematica, entering differential equations

I am trying to model the spread of disease using the differential equations given on this site http://www.maa.org/press/periodicals/loci/joma/the-sir-model-for-spread-of-disease-the-differential-equation-model with Wolfram Mathematica.
I entered:
NDSolve[{i'[t]== 1/2s[t]i[t]-1/3i[t], s[t]==-1/2s[t]i[t],r[t]==1/3i[t], r[0] ==0, s[0]==1, i[0]==1.27*10^-6,s'[0]==0} i, {t, 0, 100}]
and received the error
NDSolve called with 2 arguments; 3 or more arguments are expected.
I also tried
NDSolve[{i'[t]== 1/2s[t]i[t]-1/3i[t], s[t]==-1/2s[t]i[t],r[t]==1/3i[t], r[0] ==0, s[0]==1, i[0]==1.27*10^-6,s'[0]==0} i, {t, 0, 100}]
and got the same error
I am a newcomer to both differential equations and Mathematica, so I'd be grateful if someone can help.
As Bill told these was no coma. Second argument of NDSolve is set of function. You can type it without arguments or with arguments. Your code should look like this:
sol = NDSolve[
{i'[t] == 1/2 s[t] i[t] - 1/3 i[t],
s[t] == -1/2 s[t] i[t], r[t] == 1/3 i[t],
r[0] == 0,
s[0] == 1,
i[0] == 1.27*10^-6,
s'[0] == 0}, {i[t], s[t], r[t]}, {t, 0, 10}]
It generates error connected with numerical problems:
NDSolve::ivres: NDSolve has computed initial values that give a zero residual for the differential-algebraic system, but some components are different from those specified. If you need them to be satisfied, giving initial conditions for all dependent variables and their derivatives is recommended. >>
But you can print your results:
Plot[{Evaluate[i[t] /. sol], Evaluate[s[t] /. sol],
Evaluate[r[t] /. sol]}, {t, 0, 10}]

Mathematica FindRoot solve equation

I have a task to solve equation system with FindRoot:
a*x+b*y^2-c*x^2=a-b, a*x^2+b*y^2+b*x-cy=b+c , where a = 10, b = 10, c = 6;
I'm very(!) new to Mathematica and have one day to get to know it.
Any comments on how to solve that equation will be much appreciated!
Thanks!
This starts searching for root at x = 0 and y = 0
eq = {a*x + b*y^2 - c*x^2 == a - b, a*x^2 + b*y^2 + b*x - c*y == b + c}
param = {a -> 10, b -> 10, c -> 6}
result = FindRoot[eq /. param, {x, 0}, {y, 0}]
This only gives you 1 of the two Real solutions. Solve will give you both (and even some solutions with Complex numbers). To test it:
eq /. param /. result
This returns (True, True) so you know you've found the correct root.
To find the solution graphically, use ContourPlot:
ContourPlot[Evaluate[eq /. param], {x, -5, 5}, {y, -5, 5}]
Here is the version with Solve/NSolve
NSolve[{a*x + b*y^2 - c*x^2 == a - b,
a*x^2 + b*y^2 + b*x - c*y == b + c} /. {a -> 10, b -> 10, c -> 6},
{x, y}]
It will give the 4 solutions. If you use Solve instead of NSolve you will have a (large) closed form of each component of each solution.
If you need more digits for the solution, add at the end of the NSolve command the option WorkingPrecision->30 (or any other number of digits. These are quadratics, they can computed to any precision necessary).

How to find a desired initial condition?

I am solving a set of differential equations, where the coefficient is determined by the solution itself. Here is a minimal example:
s = NDSolve[{M'[r] == a r^2, M[0] == 0}, M, r];
Plot[Evaluate[M[r] /. s], {r, 0, 1}]
where a is determined by requiring M[r=1]=1. Once the correct a is found, I then solve the equations normally and plot M[r]. In fortran I could iterate over a until such a requirement is met. I would like to know how to do this with Mathematica, or better still, do this more elegantly (not iterating, since it is time consuming in Mathematica).
Or if you find the above example too silly, here is the original problem:
s = NDSolve[{M'[r] == r^2 Exp[lnp[r]], lnp'[r] == - M[r]/r^2, M[0.01] == 0, lnp[0.01] == a}, {M, lnp}, {r, 0.01, 1}]
Plot[Evaluate[M[r] /. s], {r, 0.01, 1}]
where a is determined by requiring M[1]=1.
Thanks!
There is probably a neater way of doing this.
DSolve[D[M[r], r] == a r^2, M[r], r]
{{M[r] -> (a r^3)/3 + C[1]}} . . . (eqn. 1)
From eqn. 1, when r = 0, M[0] == C[1], therefore
M[r] == (a r^3)/3 + M[0]
Given M[0] = 0
M[r] == (a r^3)/3
Also given M[1] = 1, a == 3, therefore
M[r_] := r^3
Plot[M[r], {r, 0, 10}]

Solution of non-linear differential equation

I don't use Mathematica in general and I need it to compare with an other program. I want to solve a system of three differential and non linear equations. For this I use Dsolve. Everything goes wrong when I put the nonlinear term (exponential).
Here is my code:
equa = {x'[t] == z[t] - Exp[y[t]],
y'[t] == z[t] - y[t],
z'[t] == x[t] + y[t] - z[t],
x[0] == 0,
y[0] == 0,
z[0] == 0};
slt = DSolve[equa, {x, y, z}, t]
Plot[{x[t] /. slt}, {t, 0, 10}]
And the errors are like this :
DSolve::dsvar: 0.1 cannot be used as a variable.
ReplaceAll::reps:{Dsolve[<<1>>]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing
Does someone know why the exponential term makes troubles ?
Thanks
You may try
s = NDSolve[equa, {x, y, z}, {t, 0, 10}];
Plot[Evaluate[({x[t], y[t], z[t]} /. s)], {t, 0, 1}]

Resources