Skip to main content

Posts

homework - Least Square Fitting

stuck on a mathematica assignment and could really use some help. A) Fit a function with the shape log(Y) = a0 + a1log(U) to the datapoints (log(Ui), log(Yi) with an least square fitting. Show this in a plot ... B) Use your answer in A) to find a function Y = kU^n that fit to the datapoints (Ui, Yi) The U datapoints is: (600000, 200000, 60000, 10000, 2500) The Y datapoints is: (250, 60, 25, 12, 5) I've done this: data2 = {{Log[2500], Log[5]}, {Log[10000], Log[12]}, {Log[60000], Log[25]}, {Log[200000], Log[60]}, {Log[600000], Log[250]}} fitExample = Fit[data2, {1, x}, x] Am I right to be using the Fit function this way? How can I use this answer in the B question?

syntax - How to get 'exact' expression for result

I have a result from Mathematica for which want to have the exact expression, i.e. all of the arguments of type # replaced. It is given by: { a > 0, Inequality[ 0, Less, r, LessEqual, Root[a^3 #1^2 + a^2 #1^4 - 22 a #1^6 + #1^8 & , 5]]} where a and r are the only two variables. What exactly does the #1 mean here? To which variables does it refer? How do I make Mathematica replace all these place holders with its corresponding variable? Answer Root objects are just exact expressions, if possible one can express them in terms of radicals (e.g. with ToRadicals ). To deal with inequalities the best approach might be Reduce : ToRadicals @ Reduce[{ a > 0, Inequality[0, Less, r, LessEqual, Root[a^3 #1^2 + a^2 #1^4 - 22 a #1^6 + #1^8 &, 5]]}, a] // ComplexExpand r > 0 && a >= -(r^2/3) + 2/3 Sqrt[67] r^2 Sin[1/3 ArcTan[227/(3 Sqrt[127947])]] % // TraditionalForm and we can get an arbitraril...

list manipulation - Find all disjoint subsets of equal length

Suppose that $A$ is a list with $n$ elements where $n$ is even. I want to write a function that returns all pairs $(A_1,A_2)$ where the sets $A_1$ and $A_2$ each have length $\frac{n}{2}$, $A_1 \cap A_2=\emptyset$, and $A_1,A_2 \subset A$. Answer I'm assuming in A_1 A_2 pair, order matters: set = Range[6]; Transpose[{#, Reverse@#}] & @ Subsets[#, {Length[#]/2}] & @ set {{{1, 2, 3}, {4, 5, 6}}, {{1, 2, 4}, {3, 5, 6}}, {{1, 2, 5}, {3, 4, 6}}, {{1, 2, 6}, {3, 4, 5}}, {{1, 3, 4}, {2, 5, 6}}, {{1, 3, 5}, {2, 4, 6}}, {{1, 3, 6}, {2, 4, 5}}, {{1, 4, 5}, {2, 3, 6}}, {{1, 4, 6}, {2, 3, 5}}, {{1, 5, 6}, {2, 3, 4}}, {{2, 3, 4}, {1, 5, 6}}, {{2, 3, 5}, {1, 4, 6}}, {{2, 3, 6}, {1, 4, 5}}, {{2, 4, 5}, {1, 3, 6}}, {{2, 4, 6}, {1, 3, 5}}, {{2, 5, 6}, {1, 3, 4}}, {{3, 4, 5}, {1, 2, 6}}, {{3, 4, 6}, {1, 2, 5}}, {{3, 5, 6}, {1, 2, 4}}, {{4, 5, 6}, {1, 2, 3}}} If pair isn't ordered you can take halof of above or spare some memory doing: With[{l = Length[#]}, Transpose[{#, Reverse@#2}...

complex - Differentiation of an unknown function

I have to take the partial differentiation of an unknown function. For example, take the unknown function to be $g(x)$. Then it's derivative w.r.t $x$ is $g'(x)$. By default, Mathematica differentiates the function. I want to keep the result of differentiation as $d(g(x))$ and not $g'(x)$. Is there any way to achieve this? More precisely, I am using Conjugate[g[x]] as the unknown function and I want the output should be displayed only as d[Conjugate[g[x]] and not as Conjugate'[x]g'[x] . Also, can I handle the conjugate more efficiently than just carrying it all along in the code? Answer Edited because the goal was changed in the comment: This can be done by directly defining the outcome of Derivative when applied to g in the two combinations that you seem to be interested in: Derivative[1][g][x_] := d[g[x]] Derivative[1][Conjugate][g[x_]] := Conjugate[d[g[x]]]/d[g[x]]; Derivative[1][Conjugate][d[x_]] := Conjugate[d[d[x]]]/d[d[x]] Derivative[1][d][x_] := d[d[x...

graphics - Speeding up this fractal-generating code

I used the code below (which is a sample from this gist containing more similar code ) in my answer to my own question about Mandelbrot-like sets for functions other than the simple quadratic on Math.SE to generate this image: cosineEscapeTime = Compile[{{c, _Complex}}, Block[{z = c, n = 2, escapeRadius = 10 \[Pi], maxIterations = 100}, While[And[Abs[z] z = Cos[z] + c; n++]; n]] Block[{center = {0.5527, 0.9435}, radius = 0.1}, DensityPlot[ cosineEscapeTime[x + I y], {x, center[[1]] - radius, center[[1]] + radius}, {y, center[[2]] - radius, center[[2]] + radius}, PlotPoints -> 250, AspectRatio -> 1, ColorFunction -> "TemperatureMap"]] What could I do to improve the speed/time-efficiency of this code? Is there any reasonable way to parallelize it? (I'm running Mathematica 8 on an 8-core machine.) edit Thanks all for the help so far. I wanted to post an update with what I'm seeing based on the answers so far and see if I get any further re...

front end - How to Detect Special Key Presses in a Notebook?

We can use: SetOptions[EvaluationNotebook[], NotebookEventActions -> {{"KeyDown","s"} :> Print["triggered"]}] to make the current notebook respond to s button press, also we can do the same to Modifier Keys like Ctrl , Shift , Alt or so. Can we do the same with Backspace , Delete or F1 ~ F12 ? Additionally, can we detect the mouse-wheel's scrolling state? Answer Following detects backspace on Mac: SetOptions[EvaluationNotebook[],NotebookEventActions-> {{"KeyDown","\.08"}:>Print["triggered"]}] This code can be helpful for finding out different codes for non-standard keys. SetOptions[EvaluationNotebook[],NotebookEventActions-> {"KeyDown":>Print[FullForm@CurrentValue["EventKey"]]}]

Bug in Mathematica 12 neural networks?

I recently downloaded Mathematica 12, and I'm experiencing a curious failure in neural networks that had earlier worked. This failure I will demonstrate with the NN in "Classifying Data with Neural Networks—Wolfram Language Documentation" https://reference.wolfram.com/language/tutorial/NeuralNetworksClassification.html#280210622 Everything in that page works until this line: results = NetTrain[net, trainingData, All] It gives $Failed with this message: NetTrain::encgenfail2: Could not encode one or more inputs for port "Output": supplied data was a length-64 vector of real numbers, but expected a class. The invalid inputs had indices {504,841,210,1645,1285,165,956,691,940,613,1426,297,1560,754,650,1725,1662,44,831,1160,1262,1289,1060,886,1147,1578,1342,424,1176,749,1664,1030,211,1011,121,475,237,1120,79,1399,287,1791,293,214,271,222,761,1511,960,1154, >}. What is going wrong? A very similar NN worked in version 11. ETA: detailed version info: Mma 12.0.0.0 for...