# NDSolve does not give me the expected dynamics of a variable over time [closed]

I am using NDSolve to find a solution to a system of equations within Ramsey growth model, including for instance

(* 1 *) k'[t] == -(Z[t] Y[t] - G[t] F[t] - r[t] R[t] - c[t] - δ k[t])


and

(* 2 *) r'[t] == -(Exp[t* Subscript[ϒ, 3]]*
Subscript[ϒ, 2] *
Subscript[ϒ, 3])


The thing is that I want to find a solution for e.g. k[t] and c[t] (their dynamics over time t) but I want the variable r[t] to have a certain dynamics over time (as described in equation (2), r[t] is a function of time and some parameters; while some other variables e.g. k[t] and c[t] depend on r[t]).

If I add the equations for r[t] in NDSolve like this

(* 3 *) nsba1 = NDSolve[{c'[t]==..., k'[t]==..., (....), r'[t]==...}, {c[0]==..., k[0]==..., (....), r[0]==...}, {c[t], k[t], (...), r[t]}, {t,0,60}]


I am getting, as a result, different dynamics than expected (the plot of r[t] over time is different than described by equation (2) - blue line is the plot of r[t] that I expect and the orange line is a plot that I get by plotting the solution to NDSolve for r[t]; the value of r[t] in the initial period t and the final period t are correct but between the two the values differ

Is there a way to cause NDSolve give me a solution (dynamics over time) that I expect, e.g. by adding some additional equations for r[t]?

I have tried moving the equation for r[t] outside NDSolve (in a form of an algebraic equation instead of a differential equation) and replacing the differential equation r'[t]==... by an algebraic equation r[t]==... in NDSolve but both did not work well. What else can I try?

= EDIT =

The full code is pretty long but I have chosen parts that probably show best where I have a problem.

This is the dynamics (plot of r[t] over time) that I expect:

Subscript[ϒ, 1] = 0.4
Subscript[ϒ, 2] = 0.375
Subscript[ϒ, 3] = -0.1
Plot[{r[t] =
Subscript[ϒ, 1] +
Subscript[ϒ, 2]*
Exp[Subscript[ϒ, 3] t]}, {t, 0, 60}]


Then I calculate a derivative of r[t] to use it in NDSolve (I want to use NDSolve because for other variables I only have differential equations).

r[t] = Subscript[ϒ, 1] + Subscript[ϒ, 2] * Exp[Subscript[ϒ, 3] t]
Hr = D[r[t], t]


And the code that I use for NDSolve looks like this:

Subscript[ϒ, 1] = 0.4
Subscript[ϒ, 2] = 0.375
Subscript[ϒ, 3] = -0.1
r1 = 0.400929532
b = 0.4
nsba1 = NDSolve[{
r'[t] == -(Exp[t* Subscript[ϒ, 3]]* Subscript[ϒ, 2] * Subscript[ϒ, 3]),
r[0] == r1
}, {r[t]}, {t, 0, 60}]
rb1[t_] := Evaluate[nsba1[[1, 1, 2]]]
tende1 = 60
rf1[t_] := rb1[tende1 - t]
plotb = Plot[{rf1[t], b}, {t, 0, tende1}, AxesLabel -> {t, r},
PlotLegends -> LineLegend[{"r", "b"}]]


So the plot that I get using NDSolve looks different than expected and I am not sure if there is any way to make it look as I expected.

## closed as off-topic by xzczd, MarcoB, Henrik Schumacher, AccidentalFourierTransform, gwrJul 9 '18 at 11:03

• The question does not concern the technical computing software Mathematica by Wolfram Research. Please see the help center to find out about the topics that can be asked here.
If this question can be reworded to fit the rules in the help center, please edit the question.

• Not likely the source of your problem, but for future reference, you should avoid using Subscript while defining symbols (variables). Subscript[x, 1] is not a symbol, but a composite expression where Subscript is an operator without built-in meaning. You expect to do $x_1=2$ but you are actually doing Set[Subscript[x, 1], 2] which is to assign a Downvalue to the oprator Subscript and not an Ownvalue to an indexed x as you may intend. Read how to properly define indexed variables here – rhermans Jun 5 '18 at 21:28
• Please share all the code necessary to reproduce your plot and try to keep all your code in a way that we can Copy&Paste into a notebook and run. That is in a formatted form, but also complete. For example, I have transformed your equation numbers into comments. You should fill the omitted equations .... Until then, we don't have enough information to reproduce the problem. – rhermans Jun 5 '18 at 21:38
• It is impossible to suggest alterations to code that is not provided. – Daniel Lichtblau Jun 5 '18 at 22:08
• I am voting to close this: a) The uses has not been seen around again, b) the scope of the question is very limited and of little interest to other uses, c) from what is apparent, the solution is either trivial or the difficulties arise from a simple mistake. – gwr Jun 19 '18 at 10:26
• I'm voting to close this question as off-topic because it's too localized and unlikely to help future visitors. – xzczd Jul 7 '18 at 12:33

While I am not sure, what you are really doing there, I believe there are some mistakes in your equations.

While I do not understand why you need to have $r(t)$ solved for by NDSolve as you do know it in advance, let us for now assume that you simply need this to be the case. So, $r'(t)$ has to be the correct differential equation for this to be achieved. But note the following (I have replaced your "greeks" by y's for better readability and I have also avoided the use of Subscript):

(* r'[t] = *) D[ y1 + y2 Exp[ y3 t] , t ] // TraditionalForm


$y2\,y3\,e^{t\,y3}$

And also note the initial value $r(0)$ being different from yours:

y1 = 0.4;
y2 = 0.375;
y3 = -0.1;

(* r[0] = *) y1 + y2 Exp[ y3 0 ]


0.775

So with this information we can do:

func = NDSolveValue[
{
r'[t] == y2 y3 Exp[y3 t],
r[0] == 0.775
},
r,
{t, 0, 60}
];

Plot[ func[t], {t, 0 , 60} ]