Hot answers tagged

16

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 ...


11

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 ...


6

PlusMinus formats nicely, but it does not have a built-in meaning. You may work around that: h = 0.08; t = 0.13; Plot[ Evaluate[ Sqrt[(1/4) + (1/64 h^2) - x^2] - (1/8 h) + {-1, 1} ((3/8) t (1 - 2 x) Sqrt[1 - (2 x)^2]) ], {x, -.5, .5} ] If need be, you could also define your own meaning for PlusMinus: Clear[PlusMinus] PlusMinus[a__] := {-1, 1} (a)...


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 -> {"...


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 ...


4

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


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

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

After reading this and this answer, I noticed that the debugger is also useful for identifying the error source in this case. You just need to: Go to Evaluation and check on Debugger. Check on Break at Messages and click the Show Stack button (alternatively you can click the button after running the erroneous code) in the newly opened panel. Run the ...



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