Skip to main content

Posts

Showing posts from November, 2015

fitting - List argument for NonlinearModelFit

If I have a function of list argument (presumably with large number of entries), how can I pass it to NonlinearModelFit ? I don't want to pass it by pattern checking ( f[x_?(VectorQ[#, Numeric!]&)] ) because apparently it turns off some intermediate operations and heavily slows down the fitting. Example: f[x_, a_] := Exp[-Sin[{x, x}.a]]; NonlinearModelFit[RandomReal[{0, 1}, {10, 3}], f[x, a], {{a, {1, 2}}}, x] Answer I've always done it like this, and I've done fits with a few hundred parameters so it isn't that much of a pain. The point is that you define the function f to take a vector input for x and a . Then you define a vector of variables, {x 1 , x 2 ,....} and that is the fourth argument to NonlinearModelFit . The second argument to NonlinearModelFit needs to be a scalar function, where all the matrix expansions have happened already. In the case here, f[variables, parameters] evaluates the matrix product before it does any fitting. parameters = Arra

mathematical optimization - Fitting one function to another

I'd like to see how close I can fit an exponential function to a cosine function in the domain of $\;-\frac{\pi}{2}$ to $\frac{\pi}{2}$ in the least squares sense. FindFit[Cos[x], {A*Exp[l*(Cos[x] - 1)], -π/2 <= x <= π/2}, {A, l}, x] FindFit::fitd: First argument Cos[x] in FindFit is not a list or a rectangular array. I could create a table for Cos[x] to pass as the first parameter but I feel like I'm approaching this wrong and should be able to give Mathematica my problem in symbolic form. Perhaps, I should try Minimize but I didn't get any results out of it. Minimize[{(Cos[x] - A*Exp[l*(Cos[x] - 1)])^2, -π/2 <= x <= π/2}, {A, l, x}] Minimize[{(-A E^(l (-1 + Cos[x])) + Cos[x])^2, -π/2 <= x <= π/2}, {A, l, x}] Answer You can do the integral symbolically, then minimize numerically: distance = Integrate[(A*Exp[l*(Cos[x] - 1)] - Cos[x])^2, {x, -Pi/2, Pi/2}, Assumptions -> (A | l) \[Element] Reals] {min, sol} = NMinimize[distance, {A, l}] Plot[{A*Ex

Abstract algebra: define constants in a finite field

How can I to define a constant in $Z_{2}$? For example, I want to create a constant b that inherits the properties of an element from $Z_{2}$. For example b + b = 0 b^n = b Answer Your question focuses on the wrong aspect of finite fields. It's not the numbers 0 and 1 that change because you are working with $Z_2$, it's the arithmetic operators. You could define your own operators plusZ2 and TimesZ2 . An alternative is to load the finite fields package with Needs[FiniteFields`] , which overloads the relevant arithmetic operators for you.

options - Solving/Reducing equations in $mathbb{Z}/pmathbb{Z}$

I was trying to find all the numbers $n$ for which $2^n=n\mod 10^k$ using Mathematica . My first try: Reduce[2^n == n, n, Modulus -> 100] However, I receive the following error: Reduce::nsmet: This system cannot be solved with the methods available to Reduce. >> using $n^2$ instead of $2^n$ works just fine, where is the problem with $2^n\;$? On top of that, how do I keep the modulus $10^k$ variable and therefore, receive a solution dependent on $k\;$? Answer The problem we encounter here is an instance of rather unexpected limitations of equation solving functionality (i.e. Modulus option in Reduce ), e.g. this question : Strange behaviour of Reduce for Mod[x,1] provides another example which has been fixed in the newest version ( 9.0 ) of Mathematica . Since Modulus unexpectedly doesn't work here we can take advantage of the Mod function. The main issue here is that there are infinitely many solutions and even though one might classify them easily, the task of findi

graphics - how to find circles that intersect a square

I use the following (based on ideas and functions I got as ansers in [1] , [2] by, repsectively, @ybeltukov and @J.M.) in order to find random circles with centers inside a unit square that intersect this square. findPoints = Compile[{{n, _Integer}, {low, _Real}, {high, _Real}, {minD, _Real}}, Block[{data = RandomReal[{low, high}, {1, 2}], k = 1, rv, temp}, While[k < n, rv = RandomReal[{low, high}, 2]; temp = Transpose[Transpose[data] - rv]; If[Min[Sqrt[(#.#)] & /@ temp] > minD, data = Join[data, {rv}]; k++;];]; data]]; npts = 150; r = 0.03; minD = 2.2 r; low = 0; high = 1; SeedRandom[159]; Timing[pts = Select[findPoints[npts, low, high, minD], Area[ImplicitRegion[((x - #[[1]])^2 + (y - #[[2]])^2 <= r^2) && ! (0 < x < 1 && 0 < y < 1), {x, y}]] != 0 &];] (* {8.68383, Null}*) pts // Length (*33*) g2d = Graphics[{FaceForm@Lighter[Blue, 0.8], EdgeForm@Directive[Thickness[0.004], Black], Disk[#, r] &

bugs - RegionIntersection fails for some values with polygons

Bug introduced in 10.1 and fixed in 10.3.0 I want to find the intersection (region) of 2 polygons. I use RegionInstersection but it only works for some sizes, for example, when c=.5 it gives the correct result, but with c=.4 it only finds a line as intersection. Area[r3[.5]] works fine... Area[r3[.4]] does not work... What is wrong? b = 15.0; d = 4.0; lbp = 40.0; d1E = 2.; l1E = 7.0; d1D = d1E; l1D = 5.0; r1 = Polygon[{{0, 0}, {lbp/2 - l1D, 0}, {lbp/2, d1D}, {lbp/2, d}, {-(lbp/2), d}, {-(lbp/2), d1E}, {-(lbp/2) + l1E, 0}}]; r2[c_] := Rectangle[{-1.1 lbp/2, -1.5}, {1.1 lbp/2, c}]; r3[c_] := RegionIntersection[r1, r2[c]]; Answer This bug has been fixed as of Mathematica 10.3. b = 15.0; d = 4.0; lbp = 40.0; d1E = 2.; l1E = 7.0; d1D = d1E; l1D = 5.0; r1 = Polygon[{{0, 0}, {lbp/2 - l1D, 0}, {lbp/2, d1D}, {lbp/2, d}, {-(lbp/2), d}, {-(lbp/2), d1E}, {-(lbp/2) + l1E, 0}}]; r2[c_] := Rectangle[{-1.1 lbp/2, -1.5}, {1.1 lbp/2, c}]; r3[c_] := RegionIntersection[r1, r2[c]

Solve doesn't give an answer with system of equations

I have to solve the following system of equations: Solve[{k*Sech[d]*Cos[b]/Sqrt[A^2 + B^2] == 1, k*Sech[d]*Sin[b]/Sqrt[A^2 + B^2] == 0, A*k*Tanh[d]/(A^2 + B^2)+c1 == 1, B*k*Tanh[d]/(A^2 + B^2)+c2 == 0, k*Sech[k + d]*Cos[a + b]/Sqrt[A^2 + B^2] == 1, k*Sech[k + d]*Sin[a + b]/Sqrt[A^2 + B^2] == 0.1, A*k*Tanh[k + d]/(A^2 + B^2)+c1 == 1.1, B*k*Tanh[k + d]/(A^2 + B^2)+c2 == 0}, {k, d, a, b, A, B, c1, c2}] but Solve will just compute forever and give no result. I've tried also with Reduce and NSolve, but with no luck. I can use approximated answers, so I've tried to use the Taylor series of the second order of the functions Sech Tanh Cos and Sin, but still Solve couldn't give me an answer. I there something else I could try? Thank you. Answer Notice that most of your transcendal functions are neatly separated into pairs of equations. This will get you most of the way to your solution sys={k*Sech[d]*Cos[b]/Sqrt[A^2+B^2]==1, k*Sech[d]*Sin[b]/Sqrt[A^2+B^2]==0, A*k*Tanh[d]/(A^2+B^

Is there a simple way to get the Conjugate of a complex term, provided all information is available?

I've found a few threads addressing this, but they all seem to have pretty clunky answers, and it's hard to believe there's not a more elegant solution in Mathematica: Remove annoying Conjugate a problem about simplifing conjugate The first thread is very clunky and I'm having trouble implementing it in a standalone function, and the 2nd solution isn't working for me, as far as I can tell. Here's the simplest I can reduce my problem to. Let's say I have a term: bop = Sqrt[x-2] And I want to find the complex conjugate (CC) of it, and let's assume x is Real. I think MMa runs into problems because, depending on the value of x, the form of the CC will be pretty different. For example, if x>2, then bop is real, and the CC is just bop. On the other hand, if x<2, then the value in the Sqrt is negative, you'd have to pull an imaginary i out of the Sqrt, and flip its sign. Alright, so now I'm giving it all the information it should need to give me t

numerical integration - Strategies to solve an oscillatory integrand only known numerically

I have an integrand that looks like this: the details of computation are complicated but I only know the integrand numerically (I use NDSolve to solve second order ODE). The integrand is not simply the solution of my ODE either; calling the two solutions of my ODE osc1[s], osc2[s] then schematically the integrand I have looks something like exp(-is)[g(s)osc1[s]osc2*[C-s]+f(s)osc2[s]osc2*[C-s]]. The exp bit is only very slowly oscillating over my integration range, it is really osc1,osc2 that give wild oscillation, as a certain parameter they depend on gets larger. More explicitely rstar[r_] := r + 2 M Log[r/(2 M) - 1]; M=1; rinf=10000; rH = 200001/100000; r0 = 10; wp=40; ac=wp-8; \[Lambda][l_] = l (l + 1); eq[\[Omega]_,l_] := \[CapitalPhi]''[r] + (2 (r - M))/( r (r - 2 M)) \[CapitalPhi]'[ r] + ((\[Omega]^2 r^2)/(r - 2 M)^2 - \[Lambda][l]/( r (r - 2 M))) \[CapitalPhi][r] == 0; init=-0.0000894423075560122420468703835499 + 0.0000447222944185058822813688948339 I; dinit=-4.4641

How to convert expression to String, but keep it looking similar to input form?

I have lots of differential equations, that I save to file (along with output and other things), as "strings", to process later in Latex and make a document of them. I save each input differential equation, by first converting it from expression to String, then write it to the file (using WriteString command). The problem is that when converting say eq = y'[x] == 1/y[x] to String, using ToString it becomes Which ofcource does not work, when saved to file, since it messes up the lines in the text file. So I use InputForm like this ToString[eq,InputForm] and now it works, the string is flat and on one line: The above is a string, and I can use that with no problem. What I like however is to have the string look like the original expression, since it is easier to read (these will later show as verbatim in Latex), i.e. I need to convert expression to y'[x] == 1/y[x] to same as above, but as string "y'[x] == 1/y[x]" I do not use 2D math at all in m

plotting - Manipulating the axis on a ListLinePlot

I am trying to create a plot using ListLinePlot in which the x-axis is represented in a logarithmic scale, and is reversed (so larger values are on the right side). I have found that ScalingFunctions -> {"Reverse"} will reverse the axis and ScalingFunctions -> {"Log"} will scale the axis to a log scale, but the two commands will not work together. Does anyone have any ideas?

plotting - Hatching pattern in ColorRules for MatrixPlot

Using ColorRules with MatrixPlot , how does one get a hatching pattern in one of the cells? For example, consider the data , data = { {1, 2, 1}, {2, 2, 3} } And now I consider the MatrixPlot , MatrixPlot[data, ColorRules -> { 1 -> Blue, 2 -> Red, 3 -> Purple } ] Question: For the cell with the value 3 and currently with the color Purple , how can I get it to have a Purple color with a hatched pattern? Answer As belisarius said, there is no current support for hatching but you could use a graphics overlay as a workaround. First create the desired pattern and texture a Polygon with it: t = Table[{0, n}, {n, -1, 1, 0.1}]; g[c_] := Graphics[{AbsoluteThickness[8], Line /@ Transpose[{t, t + 1}]}, PlotRange -> {{0, 1}, {0, 1}}, Background -> c]; pattern2[p_, c_] := Graphics[{Texture[g[c]], Polygon[{ {p[[1]] - 1, p[[2]] - 1}, {p[[1]], p[[2]] - 1}, {p[[1]], p[[2]]}, {p[[1]] - 1, p[[2]]}}, VertexTextureCoordinates -> {{0, 0},

How can I remove matching sublists from my list?

I want to use DeleteCases (or any appropriate function) to remove elements of a list, which in turn are lists of fixed size. The rule I wish to apply is that any element of the list which already appear elsewhere in the list, up to any number of -1 is to be removed. But I don't know how to do this kind of tricky pattern matching. For example, I have the following list: myData = {{h -> 255.155, c -> 0, s -> -10000.}, {h -> -255.155, c -> 0, s -> 10000.}, {h -> 0, c -> 0, s -> 10000.}, {h -> 0, c -> 0, s -> -10000.}, {h -> 255.155, c -> 0, s -> 10000.}, {h -> -255.155, c -> 0, s -> -10000.}, {h -> -255.155, c -> 1870.83, s -> 3535.53}, {h -> 255.155, c -> -1870.83, s -> -3535.53}, {h -> 0, c -> 1870.83, s -> 3535.53}, {h -> 0, c -> -1870.83, s -> -3535.53}, {h -> 255.155, c -> 1870.83, s -> 3535.53}, {h -> -255.155, c -> -1870.83, s -> -3535.53}, {h -> 255.155,

debugging - Automatically add debug contents to a program

Sometimes I'll use Print[1] or Print[n] to debug in a long loop, but it's really a boring job to add these sentence by sentence and removing them sentence by sentence. (It's already boring to read this first three lines sentence by sentence so you can imagine how boring it is to add these stuff sentence by sentence. :P) So my question is how to throw this job to a program? Let's take this small (and stupid) program as an example: Do[j=i^2;Thread[{{1},{2,3}}],{i,3}] I would like to add two Print in this program to determin where the problem occurs. Do[Print@1;j=i^2;Print@2;Thread[{{1},{2,3}}],{i,3}] In this way I can know that it's the second step that goes wrong. Any idea how to do this automatically? Answer Yesterday I checked my account's question list again and find this question lying inside. After a year wrestling mostly with Hold stuffs, I think I finally found a, well, not that elegant solution. Any suggestions or new answers are surely welcomed!!!

differential equations - Use NDSolve to solve a PDE

I have the following PDE (a master equation, and $P$ is probability density, $0\le x\le1$ and $0\le y\le1$): $$ \partial_t P(x,y,t)=x\partial_xP(x,y,t)+(1-y)\partial_yP(x,y,t)+2P(x,y,t) $$ The initial conditions are $P(x,y,t=0)=\delta(x-0.05,y-0.85)$. For the boundary conditions, if the probability density is going to flow through the boundary, I won't do anything to stop it. ( How to express the BC in Mathematica? ) I used the following command: NDSolve[{D[P[x, y, t], t] == x D[P[x, y, t], x] + (1 - y) D[P[x, y, t], y] + 2 P[x, y, t], P[x, y, 0] == DiracDelta[x - 0.05, y - 0.85]}, P, {t, 0, 2}, {x, 0, 1}, {y, 0, 1}] First, Mathematica gave the following warning: Warning: an insufficient number of boundary conditions have been specified for the direction of independent variable y. Artificial boundary effects may be present in the solution. >> Moreover, the result it gave obviously is wrong: Plot3D[Evaluate[(P[x, y, t] /. %) /. {t -> 0}], {x, 0, 1}, {y, 0, 1}] My question i

query - Unix-like reference to parent and root Association?

Is there a syntax or function to reference parent and root when querying nested Associations similar to Unix' ".." and "/", eg "cd ../../mydir". ? Here's a typical application of the root construct. Given a nested dataset: ds = <| "study" -> <| "all" -> <|"n" -> 223.`, "Median" -> 18.5` |>|>, "bySite" -> <| "VASD" -> <|"n" -> 107.`, "Median" -> 20.40`|>, "UCSD" -> <|"n" -> 116.`, "Median" -> 16.5` |>|>, "bySpeciality" -> <|"Primary" -> <|"n" -> 127.`, "Median" -> 17.8` |>, "Specialty" -> <|"n" -> 96.` , "Median" -> 19.5` |>|>|> // Dataset (Dataset is only used here for formatting convenience) using ds[&quo

calculus and analysis - Limit of Nested Radical $sqrt{1+sqrt[2!]{2^2+sqrt[3!]{3^3+...}}}$

I want to find limit of infinite nested radical $\quad \quad \sqrt{1+\sqrt[2!]{2^2+\sqrt[3!]{3^3+...}}}$ but I don't know how to define this expression in Mathematica . How can I define it and find the limit: $\quad \quad \lim_{n \to\infty} \sqrt{1+\sqrt[2!]{2^2+\sqrt[3!]{3^3+...+\sqrt[n!]{n^n}}}}$ Answer Here is an approach using FixedPoint , where I keep the output in exact form to see how many terms are needed to satisfy a given tolerance: Clear[x]; step[{n_, f_}] := {n + 1, f /. x -> (n^n + x)^(1/n!)}; tolerance = $MachineEpsilon; sum = Last@FixedPoint[step, {2, Sqrt[1 + x]}, SameTest -> (tolerance > Abs[Last[#1] - Last[#2]] /. x -> 0 &)] /. x -> 0 $$\sqrt{1+\sqrt{4+\sqrt[6]{27+\sqrt[24]{256+\sqrt[120]{31 25+\sqrt[720]{46656+\sqrt[720]{7}}}}}}}$$ N[sum, 16] $1.843075984668544$ tolerance = 10^-40; sum = Last@FixedPoint[step, {2, Sqrt[1 + x]}, SameTest -> (tolerance > Abs[Last[#1] - Last[#2]] /. x -> 0 &)] /. x -> 0 I

evaluation - What happens when you divide by ##?

I've been playing around with sequences a bit. In particular with using ## with unary and binary operators. Let's start simple, the following all make some kind of sense: + ## & [a,b] (* a + b *) x + ## & [a,b] (* x + a + b *) x * ## & [a,b] (* x * a * b *) x ^ ## & [a,b] (* x ^ a ^ b *) Now here is a slightly weird case: - ## & [a,b] (* -a*b *) x - ## & [a,b] (* x - a*b *) I guess, this sort of makes sense if - is actually interpreted as something like +(-1)* . But it also means that +##-## is generally non-zero. But now here's the real puzzle: x / ## & [a,b] (* x a^(1/b) *) x / ## & [a,b,c] (* x a^b^(1/c) *) Wh... what? Can anyone explain what's happening here or at least give some justification like the one for subtraction? Answers which correct my explanation for subtraction are also welcome! (No, I would never use this stuff in production code. But knowing what exactly is going on under the hood could come in handy some time

interpolation - Interpolating 2D data with missing values

I have a list (21 x 21) containing values. I want to eliminate slots containing zeros by interpolating the nearest values and overwriting the zeros. How do I use the Mathematica 's ListInterpolation function to do that? Answer You have to use Interpolation because there it is possible to give values which do not lie on a structured grid. Let's create some test data which has zeroes in it data = Table[ Sin[x] + Cos[y], {x, 0, Pi/2, Pi/2/29.}, {y, 0, Pi/2, Pi/2/29.}]* RandomInteger[{0, 1}, {30, 30}]; ListPlot3D[data] Now you attach the position of every value to it so you get tuples of the form {{x,y},value} and Select only those, where value is not zero. This can then be interpolated data2 = Select[Flatten[MapIndexed[{#2, #1} &, data, {2}], 1], Last[#] != 0 &]; ip = Interpolation[data2]; Plot3D[ip[y, x], {x, 1, 30}, {y, 1, 30}] Note, that with such data you always have an InterpolationOrder of 1. Update regarding your comment Yes, this is indeed a problem a

list manipulation - How to use Map inside MapThread?

Given this input lst1 = {{a, b, c}, {d, e, f}}; lst2 = {1, 2}; and the goal is to generate this output { {{1, a}, {1, b}, {1, c}}, {{2, d}, {2, e}, {2, f}} } Perfect candidate for MapThread So I made this diagram first to figure what the function I want to map should be So the function to use inside MapThread , needs to also use Map itself (in order to map each item into the other list). So I came up with this: lst1 = {{a, b, c}, {d, e, f}}; lst2 = {1, 2}; foo[i_, lst_List] := List[i, #] & /@ lst MapThread[foo[#1, #2] &, {Range[Length@lst2], lst1}] (* { {{1,a}, {1,b}, {1,c}}, {{2,d}, {2,e}, {2,f}} } *) Now here is the question : Is there a way to do the above without having to define an explicit function but using pure function inside MapThread ? I was getting conflict with # mapping. This is sort of the thing I was trying to do, but can't get the syntax right (*invalid, for illustration only *) MapThread[ Function[{idx, lst},List[idx, #] & /@ lst] &a

kernel - Aborting evaluation when the memory exceeds a certain limit

Sometimes when generating a lot of data the memory usage shoots beyond its physical realm and my laptop freezes. There is no way to stop Mathematica when this happens. Task manager (or its equivalent in other OSes) won't even start. I was wondering if it's possible to set a threshold somewhere in Mathematica to stop it automatically after a certain amount of memory. Answer Since one may not always accurately predict when MemoryContrained is needed, I recommend setting up a watch-dog task. Belisarius described how to do this here in answer to my question . I will reproduce it below as answers that are merely links are discouraged. In Mathematica 8 you could start a memory watchdog, something along the lines of: maxMemAllowed = 1.3 1024^3; (*1.3 GB*); intervalBetweenTests = 1; (*seconds*) iAmAliveSignal = 0; Dynamic[iAmAliveSignal] RunScheduledTask[ If[MemoryInUse[] > maxMemAllowed , Quit[], iAmAliveSignal++], intervalBetweenTests]; Remembe

plotting - How to graph a series of coupled oscillators and watch the wave move along them

Here are the differential equations that set's up the 11 coupled oscillators. new = Join[ Table[x[i]''[t] == - x[i][t] + 0.1*(x[i + 1][t] - 2*x[i][t] + x[i - 1][t]), {i, 1, 9}], {x[0]''[t] == -x[0][t], x[10]''[t] == x[9][t], x[0][0] == 1, x[0]'[0] == 1, x[1]'[0] == 0, x[1][0] == 0}, Table[x[i][0] == 0, {i, 2, 10}], Table[x[i]'[0] == 0, {i, 2, 10}]] Here are the solutions. Solt = NDSolve[new, Table[x[i], {i, 0, 10}], {t, 25}] Here are the individual plots. Table[Plot[Evaluate[x[i][t] /. Solt], {t, 0, 25}, PlotRange -> All], {i, 0, 10}] I am trying to figure out how to make a graph so along the x-axis are my i's from 0 to 10, and I can watch the wave move along each oscillator as time moves on. I keep getting errors in which it floods my notebook and doesn't stop unless I close the kernel. This is what I have so far, and I'm not sure how to incorporate time into this. Plot[Evaluate[x[i][t] /. Solt], {i, 0, 10}] EDIT Coupled in a cir

programming - Modifying a List in a function in place

An example will be most specific: func[list_, column_] := list[[All, column]] = Map[#*2 &, list[[All, column]]]; This throws errors. I want to avoid doing something like this: func2[list_] := Map[#*2 &; list]; list[[All, 2]] = func2[list[[All,2]]] because nesting a couple of functions raises complexity unnecessarily, the output would have to be reassigned every time. Thanks in advance. As a followup, using HoldFirst works fine, but using the so defined function in a Map gives again errors. The setup is as follows: create a nested list testList = Table[Table[{x y, x y 2}, {x, 1,3}], {y,1,3}] define afunc with HoldFirst Attribute afunc = Function[{list, col}, list[[All, col]] = Map[# * 2 &, list[[All, col]]], HoldFirst] and another function using the first bfunc[nestedList_, col_] := Map[afunc[#, col] &, nestedList] now, a call to bfunc[testList, 2] should alter the 2'nd columns of the nested lists I'd expect, but it instead throws errors i've tried to set Att

plotting - ListContourPlot interpolation fails if x and y axes have different scales

To summarize what is below: ListContourPlot doesn't work when the domain has different x and y scales (fails when x/y~10^4, which seems surprisingly small). Is it possible to call ListContourPlot on a pre-defined Interpolation[] object, while also restricting the domain of the interpolated solution to the domain of the data (the way ListContourPlot usually does automatically)? Is it possible to set the numerical precision of the interpolation algorithm inside ListContourPlot? and/or (Thanks to @SimonWoods) Is there an alternative to the undocumented ListContourPlot option Method -> {"DelaunayDomainScaling" -> True} that is implemented in v10? Related: @SimonWoods's solution for v7+ although note that my kernel is not crashing. -- Consider a regular but unstructured array of points on a rectangular domain of arbitrary aspect ratio. In this example, the domain can be scaled along the x and y axes by the input arguments: dataZ = RandomReal[{1, 10}, {10, 10}]; scal

plotting - Longer ticks without specifying the ticks itself

I know similar questions have been asked here before, but i can't figure out this specific problem. I have a lot of plots that i'd like to bring to a publishable form, but in my current customization the ticks are barley visible, therefore i'd like to make them a little bit longer, without having to specify the tick positions itself. An example would a be a plot like this ListPlot[Abs@Sin[N[#]] & /@ Range[350], Frame -> True, Joined -> True, PlotRange -> {{35, 300}, {0, 1.05}}, FrameLabel -> Evaluate[Style[#, 30] & /@ {"T [MeV]", "\[Sigma]/\!\(\*SubscriptBox[\(\[Sigma]\), \(0\)]\)"}], FrameStyle -> Thick, LabelStyle -> Directive[25, Thick], ImageSize -> {720*GoldenRatio, 720} ] I know i can customize the ticks by adding the following option FrameTicks -> { { {#, If[Chop[Mod[#, 0.25]] == 0, Chop@#, ""], {If[Chop[Mod[#, 0.25]] == 0, 0.015, 0.008], 0}}&/@Range[0, 1.05, 0.05] , {#, "", {If[Chop[Mod[#, 0

output formatting - Can I print to a different notebook? (within the context of the same kernel)

Please imagine a simple loop: For[i=1,i<=100,i++, Print[i]; ]; We can ask Print[] to output $(1,2,3,4,...)$ to a different notebook (in the context of the same kernel)? Answer As Yves already mentioned, you can easily create and edit notebooks through Mathematica commands. A start would be this tutorial , which you can find in the Documentation Center under tutorial/ManipulatingNotebooksFromTheKernel Here is a short example printing the i values into a new notebook: nb = CreateDocument[]; For[i = 1, i <= 10, i++, SelectionMove[nb, Next, Cell]; NotebookWrite[nb, Cell[BoxData@RowBox[{"i is now ", ToString[i]}], "Output"]]; ] If you want to know how to construct cell expressions, you could just go over any cell in a notebook and hit Ctrl + Shift + E to see the underlying structure.

fitting - Fourier Transform to help guess with NonLinearModelFit

I imported some data in Mathematica which I cleaned a bit. This process went well. I also managed to plot it, and tried to take the Fourier transform of the data and plot it again. I'm not sure if I did this correctly (see code). Now I would like to fit the data with help of NonlinearModelFit and use the initial guesses of the Fourier transform. Can someone help me with this? Sorry for the long data... This is the data after I imported and cleaned it: raw = {{-3.84945, 0.41909}, {-3.43009, -0.0701917}, {-3.73715, -0.0764585}, {-4.35638, 0.302424}, {-3.67285, -0.147868}, {-3.39557, -0.256599}, \ {-3.78187, -0.369644}, {-3.47578, -0.251411}, {-3.5905, -0.361445}, \ {-3.32659, 0.140039}, {-3.33448, -0.0781868}, {-3.2252, 0.180387}, {-3.42825, 0.0858649}, {-2.87733, 0.120913}, {-2.97688, -0.15721}, {-3.40708, 0.788854}, {-3.06072, 0.939109}, {-2.60687, 0.734961}, {-2.30167, 0.66035}, {-2.70302, 0.926581}, {-2.6086, 0.938341}, {-3.19459, 1.08926}, {-2.56533, 1.34221}, {-3

streams - LibraryLink: What can we do with MInputStream and MOutputStream?

Version 9 introduced the WolframStreamsLibrary.h header for LibraryLink. It contains MInputStream and MOutputStream . What can I do with this header? How can I use these functions? Are the documented anywhere? Possibly related: Update 2018 March: Partial spelunking results, someone else may want to pick up the trail. Currently, my best guess is that this is for extending Mathematica with new streams. I believe it to be the analogue of DefineInputStreamMethod and DefineOutputStreamMethod . The struct MInputStream contains most of the functions that would be passed to DefineInputStreamMethod . The actual analogue is registerInputStreamMethod in WolframLibray.h , which I believe sets the equivalents of the ConstructorFunction and NameTestFunction . My (unverified!) guesses for the parameters of registerInputStreamMethod are: const char *name , same as name in DefineInputStreamMethod void (*ctor)(MInputStream, const char* msgHead, void* optionsIn) , same as "ConstructorFunct

algorithm - Efficiently determining the largest minimum cost to travel from a fixed source to an arbitrary sink in a weighted graph

Let $v_i$ be a vertex in a graph $G$ with vertices $(v_1,...,v_N) \in V$ and edges $(e_1,...,e_M) \in E$ with associated weights $(w_1,...,w_M)$. Let $v_s \in V$ be specified vertex in the graph. In Mathematica 9, beyond repeatedly applying Dijkstra's algorithm by way of FindShortestPath[] and then summing the weights along the edges of each path, is there an efficient way to return the set of minimum costs to travel from $v_s$ to all other vertices $v_i \neq v_s$? Also, other than finding the maximum value of this matrix, is there an efficient way to detect if one path exists where the minimum cost exceeds some threshold $T$? Answer You could use GraphDistance function to get distances from the given vertex to all others: g = PolyhedronData["Dodecahedron", "SkeletonGraph"]; w = RandomInteger[{0, 10}, EdgeCount[g]]; wg = SetProperty[g, EdgeWeight -> w]; In[10]:= GraphDistance[wg, 1] Out[10]= {0, 24., 14., 11., 21., 15., 10., 10., 7., 4., 14., 14.,

output formatting - How to build a function object like the built-ins such as Interpolation?

There are many built-in functions that return a function object , such as Interpolation[] , BSplineFunction[] , LinearSolveFunction[] and so on. Now given that I want to build a function called CAGDBSplineFunction[] like the built-in BSplineFunction[] with the help of Cox-DeBoor algorithm. First trial , please see here To achieve the dynamic effect and check the validity of the option like the built-in BSplineFunction I refactored it as below: CAGDBSplineFunction::invknots = "Value of option SplineKnots \[Rule] `1` should be a non-decreasing \ real sequence of length `2`, or a symbol Automatic."; Options[CAGDBSplineFunction] = {SplineDegree -> Automatic, SplineKnots -> Automatic}; CAGDBSplineFunction /: MakeBoxes[CAGDBSplineFunction[pts_, opts : OptionsPattern[]], _] := Module[{n, sk, sd, range}, n = Length@pts - 1; sk = OptionValue[SplineKnots]; sd = OptionValue[SplineDegree]; (*check the validity of the option SplineKnots*) If[sk =!= Autom