Using Solve to find value of this function? - wolfram-mathematica

I'm looking to find the x value of function PotentialProfileCurve1 when it is equal to 1. What do I need to do to get a numerical solution out of applying solve to this function?
s2[x_] = NDSolve[{Vsemi'[x] == -((V3 - V1)/Abs[V3 - V1]) (p2*(
(nb*(Exp[-zn*Vsemi[x]*q2])) +
(pb*(Exp[-zp*Vsemi[x]*q2])) +
(-zNd*NdZnO*Vsemi[x]*q2) - (nb + pb)))^(1/2),
Vsemi[0] == V3 - V1}, Vsemi, {x, 0, 1000*10^-9},
MaxSteps -> 100000];
PotentialProfileCurve1[x_] = Evaluate[Vsemi[x] /. s2[x]];
Solve[PotentialProfileCurve1[x] == 1, x]
Instead of outputting a numerical value, i get this output:
{{x -> InverseFunction[ -6
InterpolatingFunction[{{0., 1. 10 }}, <>], 1, 1][1]}}
the function looks like this: enter image description here

Related

Simplifying a square root expression in Mathimatica

I'm trying to calculate the eigenvalues/vectors of a system with Mathematica. The 1D system is given:
In[1]:= t1 = {{0, -(lm + 2 mu)}, {-1/roh, 0}}
using the built-in function to calculate the eigenvalues we get :
In[2]:= t11 = Eigenvalues[t1]
Out[1]= {-(Sqrt[lm + 2 mu]/Sqrt[roh]), Sqrt[lm + 2 mu]/Sqrt[roh]}
I'm trying to simplify the eigenvalues expressions by assuming :
UP = Sqrt[(lm + 2 mu)/roh]
What I have tried is :
In[3]:= Simplify[t11, Assumptions -> {UP == Sqrt[(lm + 2 mu)/roh]}]
I'm expecting to get the following :
Out[2]= {-U, U}
But it's not working . Any Idea?

mathematica, picking our linear terms from an equation

so I'm trying to pick out the linear terms in an expression - for example if I say
eqn = dy/dt + y == y^2
, dy/dt+y is linear and y^2 is non linear.
I know in this case I can just use
eqn[[1]] to pull out the lhs and have that be my linear terms, but is there some way I can use a string pattern or something to get the linear parts of any entered equation?
This is some quick solution I came up with. You may need to change this for your purposes, but maybe it is a possibility. First the terms are converted into a string. This is then searched for "+" or "-" and separated. The individual terms are tested for linearity.
terms = y + y^2 - Sqrt[y];
string = ToString[terms, InputForm];
split = StringSplit[string, {"+", "-"}];
Do[
(*Take term i and convert the string to an expression*)
exp = ToExpression[split[[i]]];
Print[exp];
(*Test for Additivity: f(x+y)= f(x)+f(y)*)
pexp = exp /. {y -> y + c};(*f(x+y)*)
cexp = exp /. {y -> c};(*f(y)*)
test1 = pexp - (exp + cexp) // Simplify;(*f(x+y)-(f(x)+f(y))*)
(*Test for Homogeneity: f(a*x) = a*f(x)*)
aexp = exp /. {y -> a*y};(*f(a*x)*)
test2 = a*exp - aexp // Simplify;(*a*f(x)-f(a*x)*)
If[test1 == 0 && test2 == 0,
Print["linear"]]
, {i, 1, Length[split]}]

Uncertainty propagation formula in mathematica

I'm trying to write a short piece of code that will perform propagation of errors. So far, I can get Mathematica to generate the formula for the error delta_f in a function f(x1,x2,...,xi,...,xn) with errors dx1,dx2,...,dxi,...dxn:
fError[f_, xi__, dxi__] :=
Sum[(D[f[xi], xi[[i]]]*dxi[[i]])^2, {i, 1, Length[xi]}]^(1/2)
where fError requires that the input function f has all of its variables surrounded by {...}. For example,
d[{mv_, Mv_, Av_}] := 10^(1/5 (mv - Mv + 5 - Av))
FullSimplify[fError[d, {mv, Mv, Av}, {dmv, dMv, dAv}]]
returns
2 Sqrt[10^(-(2/5) (Av - mv + Mv)) (dAv^2 + dmv^2 + dMv^2)] Log[10]
My question is, how can I evaluate this? Ideally I would like to modify fError to something like:
fError[f_, xi__, nxi__, dxi__]
where nxi is the list of actual values of xi (separated since setting the xi's to their numerical values will destroy the differentiation step above.) This function should find the general formula for the error delta_f and then evaluate it numerically, if possible. I think the solution should be as simple as a Hold[] or With[] or something like that, but I can't seem to get it.
I'm not following everything that you've done, and since this was posted two years ago it's likely you aren't working on it anymore anyways. I'll give you my solution for error propagation in hopes that it will somehow help you or others.
I tried to include the best documentation that I could in the video and files linked below. If you open the .cdf file and weed through it you should be able to see my code...
Files:
https://drive.google.com/file/d/0BzKVw6gFYxk_YUk4a25ZRFpKaU0/view?pli=1
Video Tutorial:
https://www.youtube.com/watch?v=q1aM_oSIN7w
-Brian
Edit:
I posted the links because I couldn't attach files and didn't want to post code with no documentation for people new to mathematica. Here's the code directly. I would encourage anyone finding this solution helpful to take a quick look at the documentation because it demonstrates some tricks to improve productivity.
Manipulate[
varlist = ToExpression[variables];
funct = ToExpression[function];
errorFunction[variables, function]
, {variables, "{M,m}"}, {function, "g*(M-m)/(M+m)"},
DisplayAllSteps -> True, LabelStyle -> {FontSize -> 17},
AutoAction -> False,
Initialization :> (
errorFunction[v_, f_] := (
varlist = ToExpression[v];
funct = ToExpression[f];
varlength = Length[Variables[varlist]];
theoretical =
Sqrt[(Total[
Table[(D[funct, Part[varlist, n]]*
Subscript[U, Part[varlist, n]])^2, {n, 1,
varlength}]])];
Part[theoretical, 1];
varlist;
uncert = Table[Subscript[U, Part[varlist, n]], {n, 1, varlength}];
uncert = DeleteCases[uncert, Alternatives ## {0}];
theoretical = Simplify[theoretical];
Column[{Row[{Grid[{
{"Variables", varlist},
{"Uncertainties", uncert},
{"Function", function},
{"Uncertainty Function", theoretical}}, Alignment -> Left,
Spacings -> {2, 1}, Frame -> All,
ItemStyle -> {"Text", FontSize -> 20},
Background -> {{LightGray, None}}]}],
Row[{
Grid[{{"Brian Gennow March/24/2015"}}, Alignment -> Left,
Spacings -> {2, 1}, ItemStyle -> "Text",
Background -> {{None}}]
}]}]))]
This question was posted over 5 years ago, but I ran into the same issue recently and thought I'd share my solution (for uncorrelated errors).
I define a function errorProp that takes two arguments, func and vars. The first argument of errorProp, func, is the symbolic form of the expression for which you wish to calculate the error of its value due to the errors of its arguments. The second argument for errorProp should be a list of the form
{{x1,x1 value, dx1, dx1 value},{x2,x2 value, dx2, dx2 value}, ... ,
{xn,xn value, dxn, dxn value}}
Where the xi's and dxi's are the symbolic representations of the variables and their errors, while the xi value and dxi value are the numerical values of the variable and its uncertainty (see below for an example).
The function errorProp returns the symbolic form of the error, the value of the input function func, and the value of the error of func calculated from the inputs in vars. Here is the code:
ClearAll[errorProp];
errorProp[func_, vars_] := Module[{derivs=Table[0,{Length[vars]}],
funcErrorForm,funcEval,funcErrorEval,rplcVals,rplcErrors},
For[ii = 1, ii <= Length[vars], ii++,
derivs[[ii]] = D[func, vars[[ii, 1]]];
];
funcErrorForm = Sqrt[Sum[(derivs[[ii]]*vars[[ii, 3]])^2,{ii,Length[vars]}]];
SetAttributes[rplcVals, Listable];
rplcVals = Table[Evaluate[vars[[ii, 1]]] :> Evaluate[vars[[ii, 2]]], {ii,
Length[vars]}];
SetAttributes[rplcErrors, Listable];
rplcErrors = Table[Evaluate[vars[[ii, 3]]] :> Evaluate[vars[[ii, 4]]], {ii,
Length[vars]}];
funcEval = func /. rplcVals;
funcErrorEval = funcErrorForm /. rplcVals /. rplcErrors;
Return[{funcErrorForm, funcEval, funcErrorEval}];
];
Here I show an example of errorProp in action with a reasonably complicated function of two variables:
ClearAll[test];
test = Exp[Sqrt[1/y] - x/y];
errorProp[test, {{x, 0.3, dx, 0.005}, {y, 0.9, dy, 0.1}}]
returns
{Sqrt[dy^2 E^(2 Sqrt[1/y] - (2 x)/y) (-(1/2) (1/y)^(3/2) + x/y^2)^2 + (
dx^2 E^(2 Sqrt[1/y] - (2 x)/y))/y^2], 2.05599, 0.0457029}
Calculating using the error propagation formula returns the same result:
{Sqrt[(D[test, x]*dx)^2 + (D[test, y]*dy)^2],
test /. {x :> 0.3, dx :> 0.005, y :> 0.9, dy :> 0.1},
Sqrt[(D[test, x]*dx)^2 + (D[test, y]*dy)^2] /. {x :> 0.3,
dx :> 0.005, y :> 0.9, dy :> 0.1}}
returns
{Sqrt[dy^2 E^(
2 Sqrt[1/y] - (2 x)/y) (-(1/2) (1/y)^(3/2) + x/y^2)^2 + (
dx^2 E^(2 Sqrt[1/y] - (2 x)/y))/y^2], 2.05599, 0.0457029}
Mathematica 12 introduced the Around function that handles error propagation using the differential method.
So although not quite in the format required in the question, but something like this is possible:
expression = a^2*b;
expression /. {a -> Around[aval, da], b -> Around[bval, db]}
output:
aval^2 bval ± Sqrt[aval^4 db^2+4 bval^2 Abs[aval da]^2]
Instead of aval, bval, da, db you can use numerical values as well.

integrating over the solutions of an implicit equation

I have an implicit equation in Mathematica, which I solve by using NSolve. Now, I need to weigh the various solutions according to a Gaussian, but I can't quite make it work. Here is my suggestion so far:
a = (4.2*10^(-5));
b = 4067;
c = 112;
sol[d_] := Select[NSolve[s == (1 + a^2*(2*Pi*1000*d)^2)/((1 + c/(1 + (s*b)/(1 + a^2*(2*Pi*1000*d)^2)))^2 + a^2*(2*Pi*1000*d)^2), {s}], Chop[(Im[s] /. #)] == 0 &][[1]][[1]][[2]];
NIntegrate[Exp[-v^2]*sol[v], {v, -2, 2}]
However, this does not work. Does anyone know what I am doing wrong? What I want is pretty straightforward, but I've had some problems implementing it.
Best,
Niles.
Try this; the main points are to use the third argument to NSolve to specify the domain and to make sure the function sol2 is called only on a numerical argument.
sol2[d_?NumericQ] := NSolve[s == (1 +
a^2*(2*Pi*1000*d)^2)/((1 +
c/(1 + (s*b)/(1 + a^2*(2*Pi*1000*d)^2)))^2 +
a^2*(2*Pi*1000*d)^2), {s}, Reals][[1]][[1]][[2]]
NIntegrate[Exp[-v^2]*sol2[v], {v, -2, 2}]
(* 1.66556 *)
Plot[Exp[-v^2]*sol2[v], {v, -2, 2}]

How to substitute an aggregate expression

For example, I have symbollically
1/n*Sum[ee[k] + 1, {k, j, n}]^2
And I want to substitute Sum[ee[k], {k, j+1, n}] to be x. How can I do this? May thanks for your help!
You may use the recurrence relation for the sum. For example:
f[j] := f[j + 1] + (ee[j] + 1);
1/N f[j]^2 /. f[j + 1] -> x
Out
(1 + x + ee[j])^2/N
Edit
Based on several questions you posted, I think you are somehow misinterpreting what the Replace[] command does. It is not "algebraic" based, but "pattern" based. It doesn't understand nor use more algebraic transformations than those already defined (by you or by Mma itself).
For example:
x/. (x-1)->y
will not match anything. But
(x-1) /. x->y-1
Will give you (y-2) because the pattern x is matched.
Moreover:
x = 3;
(x - 1) /. x -> y - 1
will give you 2 because x is evaluated before the possible match, and the x in the pattern is also evaluated (just paste, execute and look at the symbol color).
1/N*Sum[ee[k] + 1, {k, j, N}]^2 /. Sum[ee[k] + 1, {k, j, N}] -> x
Doesn't that work, or do I misunderstand? By the way, you shouldn't use N as a variable. It's a Mathematica function.

Resources