Hot answers tagged

15

Description In software engineering, it is a good practice to comment your code. I would advise you to utilise comments to partition your code in a following way. Additionally, if you have ever worked with any other programming languages, you could employ indentation. Alternatively, you could modularize your application as to develop it in smaller, more ...


12

Personally, I use lots of newlines and let the Front End indent. There doesn't seem to be anything special in your code, other than a lot of nesting that is in fact necessary in this case. You were using Grid incorrectly. Grid[a,b] is wrong. Grid[{{a,b}}] or Grid[{{a},{b}}] are correct. I guess you wanted Column[{a,b}], so I changed that. Using Text ...


9

I appear to be in the minority but I never write big blocks of Mathematica code, it's just too difficult to read. The way I look at it you have to consider how a reader will understand your app. So I make the main block very small, like this: Manipulate[ Row[{ vectorPlotAndTrajectory[y0, b], Show[fy, position[y0, b]] }] , {b, -7.99, 8} , {{y0, 2}...


6

Or you can use Map A = {{a, b, c, d, e}, {f, g, h, j}}; A /. (# -> 0 & /@ {a, b, c}) Update: or to have fun. Fold[#1 /. #2 -> 0 &, A, {a, b, c}]


6

In terms of generating the replacement rule efficiently, you can do the following: A = {{a, b, c, d, e}, {f, g, h, j}}; A /. Thread[{a, b, c} -> 0] (*{{0, 0, 0, d, e}, {f, g, h, j}}*)


6

Here is the way it would look if it were my code.My formatting style is much the same as Szabolcs' (including replacing your grid with a column), but I don't like his dangling ], and you won't see those. Also, I always start control specifications on a new line, and I mostly start option specifications on a new line, too. Note: I have redefined ϕ to ...


5

The result returned by WeatherData[] is a TimeSeries[] object. ListPlot[] can deal with it directly, but LinearModelFit[] needs some assistance to handle it, since it cannot directly deal with either TimeSeries[] or Quantity[] objects. Thus: trendLine = LinearModelFit[theTemps["Path"] // QuantityMagnitude, x, x] Show[ListPlot[theTemps, AxesLabel -> {"...


5

Albert Retey has demonstrated in a similar situation that you can use "EventLocator" to detect an event in NDSolve. For example: eqn = {\!\( \*SubscriptBox[\(∂\), \(t\)]\(u[t, x]\)\) == 1/100 \!\( \*SubscriptBox[\(∂\), \(x, x\)]\(u[t, x]\)\) - u[t, x] \!\( \*SubscriptBox[\(∂\), \(x\)]\(u[t, x]\)\), u[0, x] == Sin[2 π x], u[t, 0] == u[t, 1]}; NDSolve[...


4

You may use HoldForm. Query["a", HoldForm[#"b"] &]@assoc (* HoldForm[Range[0,10]] *) Hope this helps.


4

As it has been pointed out in the comments made to your question, using reserved symbols in identifiers is not allowed. What you can and might want to do is bind expressions to values. The arguments of the expression can serve as indices or tags to produce the kind of differentiation you want. Here are two examples of what I am alluding to. a["00", 1] = 3; ...


4

Here is one possibility: MakeBoxes[l[a_, b_], form_] := RowBox[{"l", "[", ToBoxes[PaddedForm[0, NumberPadding -> {"0", "0"}]], ",", ToBoxes[b], "]"}] l[00,1] continues to display as l[00,1]. Here is another possibility: MakeBoxes[l[a_String, b_], form_] := With[{string = "\"L" <> a <> "," <> ToString[b] <> "\""}, ...


4

The confusion simply arises from mistaking a typesetting information for \[CenterDot] with the meaning of a symbol. Replacing · with . will solve the issue: ClearAll["Global`*"]; r = (6.3674447) (10^6); θm = (90 - 21.43) Degree; ϕm = 39.82 Degree; θe = (90 - 56.85) Degree; ϕe = 60.6 Degree; RM = {r Sin[θm] Cos[ϕm] , r Sin[θm] Sin[ϕm], r Cos[θm]}; RE = {r ...


3

How about adding some assumptions (I think the following is reasonable): res = Integrate[(x1 + x2 - 1)*(Boole[ x1 + x2 >= s && x1 >= t1 && x2 >= t2]), {x1, 0, 1}, {x2, 0, 1}]; FullSimplify[res, 0 < s < 1 && 0 < t1 <= t2 < 1] $$\begin{cases} \frac{1}{2} (\text{t1}-1) (\text{t2}-1) (\text{t1}+\text{...


3

I would argue that the most idiomatic solution is A /. a | b | c -> 0


3

I can't see your problem. PlotRange in Version 10.4.1 does work fine for BoxWhiskerChart (as it should): d1 = RandomVariate[NormalDistribution[10, 3], 100]; d2 = RandomVariate[NormalDistribution[5, 1], 100]; d3 = RandomVariate[NormalDistribution[-5, 3], 100]; d4 = RandomVariate[NormalDistribution[-10, 1], 100]; P1 = BoxWhiskerChart[ {d1, d2}, ...


2

{DataA, DataB, DataC, DataD} = 10 + RandomReal[NormalDistribution[], {4, 100}]; P1 = BoxWhiskerChart[{DataA, DataB}, ChartLabels -> {Placed[{"A", "B"}, Above]}]; P2 = BoxWhiskerChart[{DataC, DataD}, ChartLabels -> {Placed[{"C", "D"}, Above]}]; GraphicsGrid[{Show[#, PlotRange -> {0, 20}] & /@ {P1, P2}}] Note: Re PlotRange doesn't work, it ...


2

A method is to write a message handler, like in this answer. The handler is passed an argument of the form Hold[Message[...], boolean] where the boolean tells the handler if the message is to be displayed, or not. Since you are looking to capture the info passed to NDSolve::ndsz, I would write the handler like Clear[messageHandler, vals]; vals = {}; ...


2

Your 2nd attempt is closer to something that's useful. Let's rewrite it with valid Mathematica syntax. lissajous[{a_, b_, c_, d_}] := ParametricPlot[{Re[E^(I a t) E^(I b t)], Im[E^(I c t) E^(I d t)]}, {t, 0, 2 Pi}, Axes -> False] Then DynamicModule[{u = RandomInteger[10, 4]}, Column[ {Dynamic@lissajous[u], Button["Run", u = ...


1

Is this a "standard" Manipulate which will not grow larger? Ok, use one of already posted answers. Here is an alternative in case where Manipulate is supposed to be generated from a package function or is a part of a bigger code. ClearAll["Test`*"]; BeginPackage["Test`"] myManipulate::usage = "myManipulate[] generates a demo Manipulate"; Begin["`...


1

To help you get started: func[a_, b_, ϕ_] := Exp[(I a ϕ) + (I ϕ])^b] ParametricPlot[{Re[func[1, 1, ϕ]]], Im[func[1, 1, ϕ]]]}, {ϕ, 0, 2 Pi}] a = 1; b = 1; c = 1; d = 1; ParametricPlot[{Re[Exp[(I phi)^a + (I phi)^b]], Im[Exp[((I phi)^c + (I phi)^d)]]}, {phi, 0, 2 Pi}]


1

Activate@Replace[Inactive[f][3],3->1, Infinity] 1 or Replace[Hold@f[3], 3 -> 1, Infinity] // ReleaseHold 1



Only top voted, non community-wiki answers of a minimum length are eligible