Quantcast
Channel: How does NDSolve treat matrices? - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by andre314 for How does NDSolve treat matrices?

$
0
0

I think that what you want to do with your broken code :

f[rho_,t_]:=Table[rho[[n,m]],{n,1,2},{m,1,2}]Cos[t]

is something similar to this code :

Table[Sum[Indexed[rho, n] Indexed[M, {n, m}], {n, 1, 2}], {m, 1,  2}] If[t < .5, 1, 2]  

I use If[...] instead of Cos[t] because it makes the control of the plot below easier.

rho0 = {1, 2}M = {{2, 0}, {0, 3}}ClearAll[f]; f[rho_, t_] :=  Table[Sum[Indexed[rho, n] Indexed[M, {n, m}], {n, 1, 2}], {m, 1,     2}] If[t < .5, 1, 2] (* Cos[t] *)sol = NDSolveValue[{D[rho[t], {t}] == f[rho[t], t], rho[0] == rho0},   rho, {t, 0, 2}]Plot[{Indexed[sol[t], {1}], Indexed[sol[t], {2}], Exp[2 t],   2 Exp[3 t]}, {t, 0, 1}, PlotLegends -> Automatic , PlotStyle -> {Directive[AbsoluteThickness[3], Black],    Directive[AbsoluteThickness[3], Red], Directive[Green],    Directive[Blue]} , PlotRange -> {{0, .8}, {0, 12}}]  

enter image description here


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>