Basically I am searching for a way to add the letter u
to an existing list of variables {a,b,c,d}
to obtain the variable list {ua,ub,uc,ud}
.
I am trying to make an function for an automatic calculation of the propagation of uncertainty.
U[term_Expression, varlist_Expression] := Sqrt[
Sum[i,{i,
Function[var,
Power[D[term, var], 2]*u] /@ varlist
}]]
The Input U[a*b, {a, b}]
gives me Sqrt[a^2*u + b^2*u]
but I want it to be Sqrt[a^2*ua + b^2*ub]
.
Probably this is a very easy task but I am struggling for hours with this small problem. Maybe you know a solution? Thanks!
UPDATE
Made it with
U[term_, varlist_] := Sqrt[Sum[i, {i,
Power[
Function[var,
D[term, var]
] /@ varlist*
ToExpression[StringInsert[#, "u", 1] & /@ ToString /@ varlist]
, 2]
}]]
Thank you!
sym
, useToString[sym]
; to join two strings together useStringJoin[str1,str2]
; to make a symbol out of the joined string useToExpression[str]
. Something likeToExpression[StringJoin[ToString[u],ToString[a]]]
should create the symbol you want. Now you are left with a pattern matching problem. How much should this task be automated? $\endgroup$StringInsert[#, "u", 1] & /@ ToString /@ {a, b, c, d}
produces{"ua", "ub", "uc", "ud"}
. $\endgroup$U[a*b, {a, b}]
beSqrt[b^2*ua + a^2*ub]
-- i.e.,a
andb
reversed becauseD[a*b, a] = b
? $\endgroup$U[term_Expression, varlist_Expression]
? That argument pattern won't accept any normal arguments -- in particular it won't acceptU[a*b, {a, b}]
. $\endgroup$