From: Benfeitas on 23 Jan 2010 07:33 Hello! I have a tricky job to do: I want to simulate something, and want to assign a value to a variable under certain conditions. Those are, if time is within a certain interval, I want that variable to have that value. Otherwise, I want it to have other value. Until now, I have been trying to use the following command: Funcao[first_, last_] := Plot[A[t] /. NDSolve[ {A'[t] == k0 A0 - k1 A[t], B'[t] == k1 A[t] - k2 B[t], C'[t] == k2 B[t] - k3 C[t], A[0] == 2 10^-6, B[0] == 0, C[0] == 0} /. k0 -> 10^-2 /. k1 -> 10^-5 /. k2 -> 10^-6 /. k3 -> 10^1 /. If[first < t < last, A0 -> 1, 0], {A, B, C}, {t, 0, 1000}], {t, 0, 1000}] for some reason, it is not working, and I cannot figure out why... With that command I will want to plot A[t], for t->{0,1000}, and considering that A0->1 if first<t<last, and A0->0 if t<first or t>last. That way, A[t] should be higher if first<t<last. Can you guys please give me some tips? Thanks
From: David Park on 24 Jan 2010 05:43 Maxim 1: Develop calculations in steps, making sure one step works before proceeding to the next. And specifically don't nest a complex calculation as the first argument of a Plot statement! Maxim 2: Start your variables with small case letters to avoid conflict with predefined Mathematica symbols. Step 1. Define your data so that it can be easily used and changed if necessary. (data = {k0 -> 10^-2, k1 -> 10^-5, k2 -> 10^-6, k3 -> 10^1}) // Column Step 2. Write a routine to generate your differential equations and check that it is working properly. Here I use Piecewise as a t function because I think it will merge with differential equation solving better than an If statement. Clear[a, b, c] deqns[first_, last_] := {a'[t] == k0 Piecewise[{{0, t <= first}, {1, first < t < last}, {0, t > last}}] - k1 a[t], b'[t] == k1 a[t] - k2 b[t], c'[t] == k2 b[t] - k3 c[t], a[0] == 2 10^-6, b[0] == 0, c[0] == 0} /. Data deqns[100, 200] // Column Step 3. Solve the equations and define the a, b and c functions. abcsols = First(a)NDSolve[deqns[100, 200], {a, b, c}, {t, 0, 1000}]; {a[t_], b[t_], c[t_]} = {a[t], b[t], c[t]} /. abcsols Step 4. Plot the solutions. Here I scaled the functions so they would all fit on one plot. Plot[{a[t], 1000 b[t], 1/2 10^10 c[t]}, {t, 0, 1000}, Frame -> True, Axes -> False] So maybe everything is working. The plot could be improved with labeling David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: Benfeitas [mailto:rui.benfeitas(a)gmail.com] Hello! I have a tricky job to do: I want to simulate something, and want to assign a value to a variable under certain conditions. Those are, if time is within a certain interval, I want that variable to have that value. Otherwise, I want it to have other value. Until now, I have been trying to use the following command: Funcao[first_, last_] := Plot[A[t] /. NDSolve[ {A'[t] == k0 A0 - k1 A[t], B'[t] == k1 A[t] - k2 B[t], C'[t] == k2 B[t] - k3 C[t], A[0] == 2 10^-6, B[0] == 0, C[0] == 0} /. k0 -> 10^-2 /. k1 -> 10^-5 /. k2 -> 10^-6 /. k3 -> 10^1 /. If[first < t < last, A0 -> 1, 0], {A, B, C}, {t, 0, 1000}], {t, 0, 1000}] for some reason, it is not working, and I cannot figure out why... With that command I will want to plot A[t], for t->{0,1000}, and considering that A0->1 if first<t<last, and A0->0 if t<first or t>last. That way, A[t] should be higher if first<t<last. Can you guys please give me some tips? Thanks
From: DrMajorBob on 24 Jan 2010 05:43 Oh, what a tangled web you weave. Clear[funcao] funcao[first_, last_] := Module[{k0 = 10^-2, k1 = 10^-5, k2 = 10^-6, k3 = 10, a, b, c}, a = a /. First@ NDSolve[{a'[t] == k0 Boole[first <= t <= last] - k1 a[t], b'[t] == k1 a[t] - k2 b[t], c'[t] == k2 b[t] - k3 c[t], a[0] == 2 10^-6, b[0] == 0, c[0] == 0}, {a, b, c}, {t, 0, 1000}]; Plot[a@t, {t, 0, 1000}, PlotRange -> All] ] funcao[0, 100] funcao[0, 900] funcao[100, 900] Bobby On Sat, 23 Jan 2010 06:33:50 -0600, Benfeitas <rui.benfeitas(a)gmail.com> wrote: > Hello! > > I have a tricky job to do: I want to simulate something, and want to > assign a value to a variable under certain conditions. Those are, if > time is within a certain interval, I want that variable to have that > value. Otherwise, I want it to have other value. > > Until now, I have been trying to use the following command: > > Funcao[first_, last_] := > Plot[A[t] /. > NDSolve[ > {A'[t] == k0 A0 - k1 A[t], B'[t] == k1 A[t] - k2 B[t], > C'[t] == k2 B[t] - k3 C[t], A[0] == 2 10^-6, B[0] == 0, > C[0] == 0} /. k0 -> 10^-2 /. k1 -> 10^-5 /. k2 -> 10^-6 /. > k3 -> 10^1 /. If[first < t < last, A0 -> 1, 0], {A, B, C}, > {t, > 0, 1000}], {t, 0, 1000}] > > > > for some reason, it is not working, and I cannot figure out why... > > With that command I will want to plot A[t], for t->{0,1000}, and > considering that A0->1 if first<t<last, and A0->0 if t<first or > t>last. That way, A[t] should be higher if first<t<last. > > Can you guys please give me some tips? Thanks > -- DrMajorBob(a)yahoo.com
From: RBenf on 25 Jan 2010 05:06 Hello Bobby, thanks for all the help! It was good to see how you can simplify what I wrote into just a few coding lines. I'm still learning how to work with Mathematica, and from what I've seen, it will still be a long way until I can use it effectively. Although the code that you gave me does not do exactly what I want to do, I will surely use it in order to solve my problem. Thanks a lot! > Oh, what a tangled web you weave. > > Clear[funcao] > funcao[first_, last_] := > Module[{k0 = 10^-2, k1 = 10^-5, k2 = 10^-6, k3 = > = 10, a, b, c}, > a = a /. First@ > NDSolve[{a'[t] == k0 Boole[first <= t <= last] > last] - k1 a[t], > b'[t] == k1 a[t] - k2 b[t], c'[t] == k2 b[t] > k2 b[t] - k3 c[t], > a[0] == 2 10^-6, b[0] == 0, c[0] == 0}, {a, > 0}, {a, b, c}, {t, 0, > 1000}]; > Plot[a@t, {t, 0, 1000}, PlotRange -> All] > ] > funcao[0, 100] > funcao[0, 900] > funcao[100, 900] > > Bobby
From: RBenf on 25 Jan 2010 05:07 Hello David, thanks for all the help! Please, let me say that your maxim's are really good advices to someone that is starting to do coding in Mathematica (as it is my case). In fact, I am trying to do Maxim 1 already - the example that I gave was a simplified version of the problem that I want to answer. But apparently I'm still unsuccessfull in doing it effectively. The code that you provided isn't fully working. However, it was allready really helpfull! Using your advices and the code examples that you provided taught me some stuff allready, thanks so much. I will use all your advices and try to solve my problem. In case I have any other questions I will reply in here. Thanks again! > Maxim 1: Develop calculations in steps, making sure > one step works before > proceeding to the next. And specifically don't nest a > complex calculation as > the first argument of a Plot statement! > > Maxim 2: Start your variables with small case letters > to avoid conflict with > predefined Mathematica symbols. > > Step 1. Define your data so that it can be easily > used and changed if > necessary. > > (data = {k0 -> 10^-2, k1 -> 10^-5, k2 -> 10^-6, k3 -> > 10^1}) // Column > > Step 2. Write a routine to generate your differential > equations and check > that it is working properly. Here I use Piecewise as > a t function because I > think it will merge with differential equation > solving better than an If > statement. > > Clear[a, b, c] > deqns[first_, > last_] := {a'[t] == > k0 Piecewise[{{0, t <= first}, {1, first < t < > t < last}, {0, > t > last}}] - k1 a[t], b'[t] == k1 a[t] - k2 > a[t] - k2 b[t], > c'[t] == k2 b[t] - k3 c[t], a[0] == 2 10^-6, b[0] > [0] == 0, > c[0] == 0} /. Data > > deqns[100, 200] // Column > > Step 3. Solve the equations and define the a, b and c > functions. > > abcsols = First(a)NDSolve[deqns[100, 200], {a, b, c}, > {t, 0, 1000}]; > {a[t_], b[t_], c[t_]} = {a[t], b[t], c[t]} /. abcsols > > Step 4. Plot the solutions. Here I scaled the > functions so they would all > fit on one plot. > > Plot[{a[t], 1000 b[t], 1/2 10^10 c[t]}, {t, 0, 1000}, > Frame -> True, > Axes -> False] > > So maybe everything is working. The plot could be > improved with labeling > > > David Park > djmpark(a)comcast.net > http://home.comcast.net/~djmpark/
|
Next
|
Last
Pages: 1 2 Prev: Creating new ColorFunction / ColorDataFunction Next: The formula of Abraham Moivre |