Wolfram Mathematica, entering differential equations - wolfram-mathematica

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}]

Related

Mathematica equations returning true instead of solving ODE?

I want to solve a system of two ODEs using mathematica's DSolve function. Here is what I have written down:
DSolve[{x'[t] == 0.02*x - .00004*x*y, y'[t] == .0004*x*y - .04*y,
x[0] == 500, y[0] == 200}, {x[t], y[t]}, t]
But for some reason it just keeps telling me that the equations return true and I shouldn't have that in my syntax, and it won't solve it. Not sure why this is happening or how to fix it.
Prolbmes:
DSolve::dvnoarg: The function x appears with no arguments. >>
You should place x[t] instead of x in equations.
NIntegrate::nlim: K[1] = x[t] is not a valid limit of integration. >>
You should use numerical solution because there is problem with analytical.
Solution:
s = NDSolve[
{
x'[t] == 0.02*x[t] - .00004*x[t]*y[t],
y'[t] == .0004*x[t]*y[t] - .04*y[t],
x[0] == 500,
y[0] == 200
}, {x, y}, {t, 0, 100}]
Plot[{Evaluate[x[t] /. s], Evaluate[y[t] /. s]}, {t, 0, 100}]
Result:

Using DSolve for a real part of a differential equation (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.

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}]

Mathematica won't NIntegrate a conditional function

To illustrate my problem here is a toy example:
F[x_] := Module[{out},
If[x > 1,
out = 1/2,
out = 1
];
out
];
The function can be evaluated and plotted. However when I try to numerically integrate it I get an error
NIntegrate[F[x], {x, 0, 2}]
NIntegrate::inumr: The integrand out$831 has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,2}}. >>
Integrate and some other functions will do some probing with symbolic values first. Your Module when given only the symbol x will return only the (aliased) out.
Fix this by defining your F only for numeric values. NIntegrate will discover this and then use numeric values.
In[1]:= F[x_?NumericQ] := Module[{out},
If[x > 1, out = 1/2, out = 1]; out];
NIntegrate[F[x], {x, 0, 2}]
Out[2]= 1.5

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