Message Boards Message Boards

[✓] Implement an external function to a discretionary notebook?

GROUPS:

Hey guys, I've got a function which computes the propagation of uncertainty for a given function. It is called "fehler" and an example would be:

fehler[Sin[2*a], a]

which gives me

2 Sqrt[\[CapitalDelta]a^2 Cos[2 a]^2]

So basically the syntax is fehler[function, variable1, variable2,...]. The whole file is attached to the post, the credit goes to some random guy from my university, unluckily I could not figure out who made this function.

Now I'd like to make this function usable inside every notebook, because right now I always use it externally which is not really nice. So for example if i have a random notebook it should work like this:

(*random notebook*)
fehler= (*this is the code i need*)
a = 1; b=2; const=9;
fehler[a*const+b*2, a, b]
(*the output should like this*) Sqrt[const^2 \[CapitalDelta]a^2 + 4 \[CapitalDelta]b^2]

So that in every notebook I use i can simply use the "fehler"-function to calculate to propagation of uncertainty without having to use it globally. I tried to make this possible for a long time, but the main problem is that the existing fehler-function requires to clear all Variables with ClearAll["Global`*"]; before executing fehler. It is important that the output can be further used as a function, and not is simply a (numerical) result.

Do you guys have an idea how I can solve this problem? It would help me (and other students) a lot, if we could the function inside our notebooks and not externally.

Thanks you and best regards, Tobias

POSTED BY: Tobias Mitterdorfer
Answer
24 days ago

Tobias,

you can do a

NotebookEvaluate["Fehlerrechnung-gauss.nb"] 

if you delete the ClearAll and I suggest removing all the other extraneous evaluatable cells. This is simplest.

The "better" way to do this is to make a package out of it --Delete the ClearAll at the beginning and create a package:

You can load the package with

Needs["yourpackageName`"] 

(note the prime at the end of the name)

Regards,

Neil

POSTED BY: Neil Singer
Answer
24 days ago

Tobias,

Also, you can simplify your function quite a bit and make it not use any global variables with:

fehler2[exp_, vars_List] := 
 Sqrt[Plus @@ 
   Map[(D[exp, #]*ToExpression["\[CapitalDelta]" <> ToString[#]])^2 &,
     vars]]

which is used like this

fehler2[a*const + b*2, {a, b}]

Note that the variables are given as a list and not inlined in the function. You can make them inlined as you originally had but I think that is less conforming to Mathematica standards for functions:

fehler2[exp_, vars__] := 
 Sqrt[Plus @@ 
   Map[(D[exp, #]*
        ToExpression["\[CapitalDelta]" <> ToString[#]])^2 &, {vars}]]

which is called your old way:

fehler2[a*const + b*2, a, b]

You also have a big mistake in the way you use the function -- you must have the a,b,etc NOT defined as numbers when you call the function (in any of the versions). My suggestion is to do the following:

error = fehler[a*const + b*2, a, b]
error /. {a -> 1, b -> 2, const -> 9}

The same applies for fehler2 in either format. This approach will not define the a,b,const so you can change things without clearing anything in the kernel. Hope this helps.

Regards,

Neil

POSTED BY: Neil Singer
Answer
24 days ago

Hey guys,

thanks a lot for your suggestions! I'll try it now and then tell you what I've achieved.

have a nice evening, Tobias

POSTED BY: Tobias Mitterdorfer
Answer
24 days ago

Group Abstract Group Abstract