Mathematica: Plotting a module with FindRoot in it - function

I run into this problem occasionally, and I haven't found a way around it.
It usually happens when I'm finding the root of an equation, and want to maximize/minimize/plot that root according to some parameter. So I try to wrap the the code in a module so it can all be executed with just an input number, but it won't work inside functions like Plot.
For example:
f[din_] := Module[{d = din},
sol = NDSolve[{y'[x] == y[x], y[0] == 1}, y[x], {x, 0, 10}];
t1 = Flatten[FindRoot[y[x] - d /. sol, {x, 1}]];
x /. t1
]
f[2]
f[2.5]
f[3]
Plot[f[x], {x, 2, 3}]
The calls to f with a number all work as expected, but the f in the Plot function seems to be evaluated with the symbol 'x' - or something and just gives a lot of error text.
Any way around this?
Looking around the forums I found some suggestions for similar problems - like making the definition like this:
f[din_?NumericQ]:=
and I tried everything I could but nothing seems to make a difference.
I'm using Mathematica 8.0

The main fix is to take the sol = NDSolve[... out of the module. The module itself can also be simplified as shown:-
sol = NDSolve[{y'[x] == y[x], y[0] == 1}, y[x], {x, 0, 10}];
f[din_] := x /. FindRoot[y[x] - din /. sol, {x, 1}]
Plot[f[x], {x, 2, 3}]

Try :
f[din_?NumericQ] := Module[{LocalDummy, Localy, LocalSol},
Localy = y /. NDSolve[{y'[LocalDummy] == y[LocalDummy], y[0] == 1}, y, {LocalDummy, 0, 10}][[1]];
LocalSol = FindRoot[Localy[LocalDummy] - din == 0, {LocalDummy, 1}][[1, 2]] ]

Related

How to graph the function in matlab?

I have the following 2n*π-periodic function F(x) = sin(x/n) and I need to graph the dx/dt = γ - F(x) on the segment from 0 to 2pi. So it should look like this. I tried to do it matlab this way:
gamma = 1.01;
n=3;
[t,phi] = ode45(#(t,x)gamma-sin(x/n), [0,400], pi);
[t1,phi1] = ode45(#(t,x)gamma-sin(x/n), [112,400], 0);
[t2,phi2] = ode45(#(t,x)gamma-sin(x/n), [231,250], 0);
figure();
plot(t, phi, 'k', t1, phi1, 'k', t2, phi2, 'k');
ylim([0 2*pi]);
yticks([0 pi 2*pi]);
yticklabels(["0" "\pi" "2\pi"]);
grid on; grid minor;
title('\itsin(x/n)')
but I only got something like this. So there the lines are not transferred, but "begin anew". does anyone here know how to do that?
I get a plot similar to your first sketch, and based on your code in the comments (in future, put such additions into the question itself, use formatting to mark it as addition, and cite it then in the comment) with the changes
use pi as initial point as seen in the drawing,
use the options of the ODE solver to restrict the step size, directly and by imposing error tolerances
your original time span covers about 3 periods, reduce this to [0, 200] to get the same features as the drawing.
gamma = 1.01; n=3;
opts = odeset('AbsTol',1e-6,'RelTol',1e-9,'MaxStep',0.1);
[t, phi] = ode45(#(t,x)gamma-sin(x/n), [0,200], pi, opts);
phi = mod(phi, 2*pi);
plot(t, phi, 'k');
ylim([0 2*pi]); yticks([0 pi 2*pi]); yticklabels(["0" "\pi" "2\pi"]);
grid on; grid minor;
title('\itsin(x/n)')
To get more elaborate, use events to get points on the numerical solution where it exactly crosses the 2*pi periods, then use that to segment the solution plot (styling left out)
function [ res, term, dir ] = event(t,y)
y = mod(y+pi,2*pi)-pi;
res = [ y ];
dir = [1]; % only crossing upwards
term = [0]; % do not terminate
end%function
opts = odeset(opts,'Events',#(t,y)event(t,y));
sol = ode45(#(t,x)gamma-sin(x/n), [0,200], pi, opts);
tfs = [ sol.xe; sol.x(end) ]
N = length(tfs)
clf;
t0 = 0;
for i=1:N
tf = tfs(i);
t = linspace(t0+1e-2,tf-1e-2,150);
y = deval(sol,t); % octave: deval=#(res,t) interp1(res.x, res.y,t)
y = mod(y,2*pi);
plot(t, y);
hold on;
t0=tf;
end;
hold off;

Plotting a function as a function of another function in Mathematica

I wasn't entirely sure what to search for, so if this question has been asked before, I apologize in advance:
I have two functions
R := Rref*mx(mx^(4/3) - C0)^(-1)
M := Mref*(mx + C1*mx^(-1)*((1 - C0*mx^(-4/3))^(-3) - 1))
where Rref, Mref, C0 and C1 are constants and mx is the variable. I need to plot R as a function of M. Surely there must be something available in Mathematica to do such a plot - I just can't seem to find it.
The comment is correct, in that what you have is a set of two "parametric equations". You would use the ParametricPlot command. However, the syntax of functions with parameters is sometimes tricky, so let me give you my best recommendation:
R[Rref_, C0_, C1_][mx_] = Rref*mx (mx^(4/3) - C0)^(-1);
M[Mref_, C0_, C1_][mx_] = Mref*(mx + C1*mx^(-1)*((1 - C0*mx^(-4/3))^(-3) - 1));
I like that notation better because you can do things like derivatives:
R[Rref,C0,C1]'[mx]
(* Output: -((4 mx^(4/3) Rref)/(3 (-C0 + mx^(4/3))^2)) + Rref/(-C0 + mx^(4/3)) *)
Then you just plot the functions parametrically:
ParametricPlot[
{R[0.6, 0.3, 0.25][mx], M[0.2, 0.3, 0.25][mx]},
{mx, -10, 10},
PlotRange -> {{-10, 10}, {-10, 10}}
]
You can box this up in a Manipulate command to play with the parameters:
Manipulate[
ParametricPlot[
{R[Rref, C0, C1][mx], M[Mref, C0, C1][mx]},
{mx, -mmax, mmax},
PlotRange -> {{-10, 10}, {-10, 10}}
],
{Rref, 0, 1},
{Mref, 0, 1},
{C0, 0, 1},
{C1, 0, 1},
{mmax, 1, 10}
]
That should do it, I think.

How to solve discrete time Markov Chains in Sage in a short way

I'm new to Sage.
I'm able to solve DTMC on Octave by using this short code:
a = 0.2
s = 0.6
P = [
(1-a)*(1-a), (1-a)*a, a*(1-a), a*a;
(1-a)*s, (1-a)*(1-s), a*s, a*(1-s);
s*(1-a), s*a, (1-s)*(1-a), (1-s)*a;
0, s*(1-s), (1-s)*s, (1-s)*(1-s)+s*s;
]
pis = [P' - eye(size(P)); ones(1, length(P))] \ [zeros(length(P), 1); 1]
I would like to be able to do something similar in Sage. So far I'm able to solve them by using this code:
a = 0.2
s = 0.6
P = matrix(RR, 4, [
[(1-a)*(1-a), (1-a)*a, a*(1-a), a*a],
[(1-a)*s, (1-a)*(1-s), a*s, a*(1-s)],
[s*(1-a), s*a, (1-s)*(1-a), (1-s)*a],
[0, s*(1-s), (1-s)*s, (1-s)*(1-s)+s*s]
]);
I = matrix(4, 4, 1); # I; I.parent()
s0, s1, s2, s3 = var('s0, s1, s2, s4')
eqs = vector((s0, s1, s2, s3)) * (P-I); eqs[0]; eqs[1]; eqs[2]; eqs[3]
pis = solve([
eqs[0] == 0,
eqs[1] == 0,
eqs[2] == 0,
eqs[3] == 0,
s0+s1+s2+s3==1], s0, s1, s2, s3)
Unfortunately that code does not scale well, I have to manually edit the code to include the conditions of the equations equals to zero.
It is possible to achieve this in a way such as in Octave? It is possible to return real numbers instead of fractions?
Thanks a lot.
I think we can create your matrices in code more analogous to what you are using in Octave, but with Python syntax. There are shortcut constructors, they just have different names.
a = 0.2
s = 0.6
P = matrix(RR, 4, [
[(1-a)*(1-a), (1-a)*a, a*(1-a), a*a],
[(1-a)*s, (1-a)*(1-s), a*s, a*(1-s)],
[s*(1-a), s*a, (1-s)*(1-a), (1-s)*a],
[0, s*(1-s), (1-s)*s, (1-s)*(1-s)+s*s]
]);
M = (P.conjugate_transpose() - identity_matrix(P.ncols())).stack(matrix(1,P.ncols(),[1]*P.ncols()))
V = vector( [0]*P.ncols()+[1])
However, maybe I missed something in the translation; I am not very familiar with Octave, and just looked through the documentation. But the prime seems to be conjugate transpose, eye seems to be identity, and ones is all-ones, and zeros is zeros.
sage: M \ V
ValueError: matrix equation has no solutions
I note your answer here so I assume you can take it from here. Maybe there was a transpose difference in the Sage implementation of the backslash operator?
As to the fractions, this is because Maxima provides Sage's solving mechanisms, and it is more symbolically oriented - so uses fractions whenever possible. We try to keepfloat:true there as much as we can, but it doesn't always like this.

find function matlab in numpy/scipy

Is there an equivalent function of find(A>9,1) from matlab for numpy/scipy. I know that there is the nonzero function in numpy but what I need is the first index so that I can use the first index in another extracted column.
Ex: A = [ 1 2 3 9 6 4 3 10 ]
find(A>9,1) would return index 4 in matlab
The equivalent of find in numpy is nonzero, but it does not support a second parameter.
But you can do something like this to get the behavior you are looking for.
B = nonzero(A >= 9)[0]
But if all you are looking for is finding the first element that satisfies a condition, you are better off using max.
For example, in matlab, find(A >= 9, 1) would be the same as [~, idx] = max(A >= 9). The equivalent function in numpy would be the following.
idx = (A >= 9).argmax()
matlab's find(X, K) is roughly equivalent to numpy.nonzero(X)[0][:K] in python. #Pavan's argmax method is probably a good option if K == 1, but unless you know apriori that there will be a value in A >= 9, you will probably need to do something like:
idx = (A >= 9).argmax()
if (idx == 0) and (A[0] < 9):
# No value in A is >= 9
...
I'm sure these are all great answers but I wasn't able to make use of them. However, I found another thread that partially answers this:
MATLAB-style find() function in Python
John posted the following code that accounts for the first argument of find, in your case A>9 ---find(A>9,1)-- but not the second argument.
I altered John's code which I believe accounts for the second argument ",1"
def indices(a, func):
return [i for (i, val) in enumerate(a) if func(val)]
a = [1,2,3,9,6,4,3,10]
threshold = indices(a, lambda y: y >= 9)[0]
This returns threshold=3. My understanding is that Python's index starts at 0... so it's the equivalent of matlab saying 4. You can change the value of the index being called by changing the number in the brackets ie [1], [2], etc instead of [0].
John's original code:
def indices(a, func):
return [i for (i, val) in enumerate(a) if func(val)]
a = [1, 2, 3, 1, 2, 3, 1, 2, 3]
inds = indices(a, lambda x: x > 2)
which returns >>> inds [2, 5, 8]
Consider using argwhere in Python to replace MATLAB's find function. For example,
import numpy as np
A = [1, 2, 3, 9, 6, 4, 3, 10]
np.argwhere(np.asarray(A)>=9)[0][0] # Return first index
returns 3.
import numpy
A = numpy.array([1, 2, 3, 9, 6, 4, 3, 10])
index = numpy.where(A >= 9)
You can do this by first convert the list to an ndarray, then using the function numpy.where() to get the desired index.

Mathematica NDSolve: Is there a way to have variable coefficients?

Is there a way in mathematica to have variable coefficients for NDSolve? I need to vary the coefficient values and create multiple graphs, but I cannot figure out a way to do it short of reentering the entire expression for every graph. Here is an example (non-functional) of what I would like to do; hopefully it is close to working:
X[\[CapitalDelta]_, \[CapitalOmega]_, \[CapitalGamma]_] =
NDSolve[{\[Rho]eg'[t] ==
(I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ee[t] +
I*.5*\[CapitalOmega]*\[Rho]gg[t],
\[Rho]ge'[t] == (-I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]ge[t] +
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]ee[t] -
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]gg[t],
\[Rho]ee'[t] == -I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] +
I*.5*\[CapitalOmega]*\[Rho]ge[t] - \[CapitalGamma]*\[Rho]ee[t],
\[Rho]gg'[t] == I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ge[t] + \[CapitalGamma]*\[Rho]ee[t],
\[Rho]ee[0] == 0, \[Rho]gg[0] == 1, \[Rho]ge[0] == 0, \[Rho]eg[0] == 0},
{\[Rho]ee, \[Rho]eg, \[Rho]ge, \[Rho]gg}, {t, 0, 12}];
Plot[Evaluate[\[Rho]ee[t] /. X[5, 2, 6]], {t, 0, 10},PlotRange -> {0, 1}]
In this way I would only have to re-call the plot command with inputs for the coefficients, rather than re-enter the entire sequence over and over. That would make things much cleaner.
PS: Apologies for the awful looking code. I never realized until now that mathematica didn't keep the character conversions.
EDIT a nicer formatted version:
You should just use SetDelayed (":=") instead of Set in the function definition:
X[\[CapitalDelta]_, \[CapitalOmega]_, \[CapitalGamma]_] :=
NDSolve[{\[Rho]eg'[
t] == (I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ee[t] +
I*.5*\[CapitalOmega]*\[Rho]gg[t], \[Rho]ge'[
t] == (-I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]ge[t] +
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]ee[t] -
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]gg[t], \[Rho]ee'[
t] == -I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] +
I*.5*\[CapitalOmega]*\[Rho]ge[t] - \[CapitalGamma]*\[Rho]ee[
t], \[Rho]gg'[t] ==
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ge[t] + \[CapitalGamma]*\[Rho]ee[
t], \[Rho]ee[0] == 0, \[Rho]gg[0] == 1, \[Rho]ge[0] ==
0, \[Rho]eg[0] ==
0}, {\[Rho]ee, \[Rho]eg, \[Rho]ge, \[Rho]gg}, {t, 0, 12}];
Plot[Evaluate[{\[Rho]ee[t] /. X[5, 2, 6], \[Rho]ee[t] /.
X[2, 6, 17]}], {t, 0, 10}, PlotRange -> {0, 1}]