Tag Info

New answers tagged

0

ArcSin[x] returns the angle a between -Pi/2 and Pi/2 radians such that Sin[a] = x . If we want values in another interval or in Degrees we need a conversion. f[x_] := Module[{answer}, If[x < 0, answer = {[Pi] - ArcSin[x], 2 [Pi] + ArcSin[x]} , answer = {ArcSin[x], [Pi] - ArcSin[x]} ]; Map[DMSString, answer/Degree] ] x = ...


0

You can solve for the reciprocal of res: capacitance =.; Solve[i[250*10^-6, res] == 10^-6 /. res -> 1/reciprocal, reciprocal, Reals] Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result. >> {{reciprocal -> ...


2

I really don't get it. But here's walk-around. It seems that within projected area is everything with longitude in interval: {t-180, t+180} and if you set t = -180 algorithm does not care that it is plotting {-360 , 0} while oryginal data has domain {-180, 180}. We have to take care wbout Mod ourselvs: pos = Cases[ CountryData["World", ...


2

Please tell me if this simplified function does what you want: f[x_, n_] := Round[x, 10^(1 - n + ⌊ Log10 @ Abs @ x ⌋)] ~SetPrecision~ n Test: Table[f[x*Pi, 4], {x, {1/100, 1/10, 1, 10, 100}}] % // FullForm {0.03142, 0.3142, 3.142, 31.42, 314.2} List[0.03142`4., 0.3142`4., 3.142`4., 31.42`4., 314.2`4.] Related Q&A's: Meaning of backtick in ...


2

Most users probably want to use SetPrecision, which preserves extra digits and automagically handles fractional digits of precision. However, in this case, we need to somehow override this behavior. I'll use a custom object, sigFigNumber. First I'll define how it's displayed. Format[sigFigNumber[s_, d_]] := N[s, d] So we can see that sigFigNumber has ...


3

The obvious way to modify your code is Clear[f, r] r[x_, n_] := If[x > 0, Print[n]; x*r[x - 1, n + 1], 1] f[k_Integer /; k > 1] := r[k, 1] For small values of x, this works fine. f[5] But it is very inefficient and also limited by $RecursionLimit. Block[{$RecursionLimit = 20}, f[24]] Both these issues can be addressed by using a less ...


4

Here is a function makeOperator that takes any polynomial together with a replacement rule that maps the desired variable onto the desired operator. It outputs the result as a new operator: Clear[makeOperator]; makeOperator[poly_, Rule[x_, op_]] /; PolynomialQ[poly, x] := Module[{f}, Function[#1, #2] & @@ {f, Expand[poly]} /. Power[x, n_: 1] ...


6

Define L = (1/2) (D[#, x] + D[#, y]) & We see that L works as desired. For instance: Simplify[Nest[L, f[x, y], 3]] (* (Derivative[0, 3][f][x, y] + 3*Derivative[1, 2][f][x, y] + 3*Derivative[2, 1][f][x, y] + Derivative[3, 0][f][x, y])/8 *) And the Sum can be constructed in a similar manner. For instance: Simplify[Sum[Nest[L, f[x, y], n], {n, ...


1

Internally LineScaledCoordinate use Position[d, _?(#1 >= t &)] to detect a current segment. Here d is an accumulated list of distances (relative to the total length of segments). However, algebraic expressions are not atomic: t = 0.5; d = {0, 1/(3 + Sqrt[2]), 3/(3 + Sqrt[2]), 1}; Position[d, _?(#1 >= t &)] Position[N@d, _?(#1 >= t ...


5

When you enter a number with a decimal point like: x = 8.168643234; you are telling Mathematica that this is a machine precision number. You can see this with: Precision[x] (* MachinePrecision *) This means that the internal representation of the number is not exactly what you entered, but instead is the nearest machine real. RealDigits shows you the ...


0

Try this instead: y[aVec_, bVec_] := bVec[[1]]*x^2 - aVec[[1]] - aVec[[2]] g[aVec_, bVec_] := x /. FindRoot[y[aVec, bVec] == 0, {x, 1}] g[{1, 2}, {1}] 1.73205 By using Set to define y instead of SetDelayed the function ignored the two parameters and used the already-defined aVec and bVec, which were non-numerical and led to the error. That is, your ...


0

You almost have it right! sn[n_, x_, y_] := Sum[x[[i]] (sn[n - 1, x[[i + 1 ;; Length[x]]], y]), {i, Length[x]}] - Sum[y[[i]] (sn[n - 1, {}, y[[i ;; Length[y]]]]), {i, Length[y]}]; sn[0, x_, y_] := Total[x] - Total[y] So for example: x = RandomReal[{-1, 1}, 10]; y = RandomReal[{-1, 1}, 10]; sn[4, x, y]


3

There is a nice function BenchmarkPlot The usage is something like this Needs["GeneralUtilities`"] BenchmarkPlot[{f1,f2}, # &, PowerRange[1, 1000], "IncludeFits" -> True] Typical output: There are already many examples on MMA.SE.


2

There is some built-in binary search code but not in the core language as far as I know. There is BinarySearch from the Combinatorica package, which is still the function I use most often despite the fact that that package is now deprecated and loading it causes shadowing of some Symbols. There is the undocumented GeometricFunctions`BinarySearch but this ...


4

One more subtle variation: Sum[Defer[# - #2] & @@ (1/(2 i + {-1, 1})), {i, 6}] (1/11 - 1/13) + (1/9 - 1/11) + (1/7 - 1/9) + (1/5 - 1/7) + (1/3 - 1/5) + (1 - 1/3)


5

Another way of doing it (something similar to Kuba's great answer) is: Sum[HoldForm[#1 - #2] &[1/(2 i - 1), 1/(2 i + 1)], {i, 1, 6}] May be also something different: Sum[(1/(2 i - 1) - 1/(2 i + 1) // Trace)[[-2]], {i, 1, 6}]


5

HoldForm[# - #2] & @@@ Table[{1/(2 i - 1), 1/(2 i + 1)}, {i, 1, 6}] // Total


0

Use m=0; While[m<=50,p=m; m=m+1 ] Reversing the order of the last 2 instructions fixes the problem.


0

I suspect you are seeking a For loop: For[m = 0, m <= 50, ++m, p = m] {m, p} {51, 50} However as george2079 already showed you could also use While: m = 0; While[(++m) <= 50, p = m] {m, p} {51, 50}


2

You may be thinking of another idiom. http://en.wikipedia.org/wiki/Do_while_loop#Equivalent_constructs m = 0; While[True, m = m + 1; If[Not[m <= 50], Break[]]; p = m]


0

You could try this: tf = TransferFunctionModel[{{1/s}}, s] ClearAttributes[Times, Protected]; Times[k_, TransferFunctionModel[m_, s_]] := TransferFunctionModel[k m, s] SetAttributes[Times, Protected]; But you would have to modify a lot of operations.


0

You Just use the tf, with no k multiplied in the call to RootLocusPlot. The k goes to the tf it self. Like this sys = TransferFunctionModel[k*(s^2 + 2 s + 4)/(s (s + 4)(s + 6)(s^2 + 1.4 s + 1)), s]; RootLocusPlot[sys, {k, 0, 100}, ImageSize -> 300, GridLines -> Automatic, GridLinesStyle -> Dashed, Frame -> True, AspectRatio ...


0

This is a straightforward application of Select as Fred Simons showed: list = {{1, 1}, {2, 2}, {9, 3}, {16, 4}, {24, 5}}; f[x_] := x^2 Select[list, #[[1]] == f[#[[2]]] &] {{1, 1}, {9, 3}, {16, 4}} Again with Cases: Cases[list, {a_, b_} /; a == f[b]] {{1, 1}, {9, 3}, {16, 4}}


1

Here is an alternative, using dispatch between multiple definitions: g[{},_] = g[_,{}] = "One of the lists is empty"; g[a_,b_] := { a~Complement~b, b~Complement~a }; If you like syntactic sugar, you could also write the second line as g[a_,b_] := Complement @@@ { {a,b}, {b,a} };


1

Try the following: g[w_, v_] := If[w == {} || v == {}, "D'oh!", {Complement[w, v], Complement[v, w]}] Some tests: g[{2, 3, 4}, {2, 5}] g[{2}, {}] producing {{3, 4}, {5}} "D'oh!"


3

The problem is caused by ambiguity in the control-inferencing logic used by Manipulate and Control in the absence of an explicit control type specification. A Manipulate value with a list of pairs is a valid specification for a Slider, SetterBar, PopupMenu or InputField. Manipulate arbitrarily chooses to use a slider. Mathematica uses various heuristics ...


2

I suspect that you can obtain what you need by following way: list = {{1, 2}, {3, 4}, {5, 6}}; Manipulate[list[[i]], {i, 1, Length@list, 1}] This code always gives you the element (sublist) of initial list.


4

IntegerQ is meant for programming and tests for a data type, not whether something is mathematically an integer. Element is meant to represent a mathematical concept. The two are not interchangeable. Functions ending in ...Q always return True or False. Since the data type of x is not Integer (in the programming sense---it's a symbol), IntegerQ[x] ...


2

In addition to @belisarius' solution, you might want to look at StringForm Clear[f] f[c_, d_, z_] = c + d z; (* either Set or SetDelayed works *) s = Solve[{f[c, d, 1] == 2, f[c, d, -1] == 1}, {c, d}]; StringForm["c = `1`, d = `2`", c, d] /. s[[1]]


4

You need to understand how to define a function and how replacement rules work. This is a nice start point. f[c_, d_, z_] := c + d z s = Solve[{f[c, d, 1] == 2, f[c, d, -1] == 1}, {c, d}] (* {{c -> 3/2, d -> 1/2}} *) Print["c=", c /. s[[1]], " d=", d /. s[[1]]] (* c=3/2 d=1/2 *)


5

FullForm is a formatting wrapper(1)(2)(3). It does not change the way that the expression it contains evaluates but rather the way it is displayed by the Front End. You can combine it with HoldForm, which is another formatting wrapper that specifically holds its argument, to show the unevaluated full form of an expression: Cases[{a^2, a^3}, a_^1] // ...


5

As already mentioned in the comment: FullForm is a function that evaluates in the standard way because it has no attribute saying otherwise. One consequence is that before FullForm acts on your input, it evaluates it, which leave only the result of cases. None of this helps you to get your answer anyway. One easy way is to use Unevaluated on arguments to ...


2

You can identify the problem by performing the steps in your function one at a time for theta = .1 and phi = 0.. {c1, c2, c3} = 1 - 3 Sin[0.1]^2 Cos[0 - 2 Pi (# - 1)/3]^2 & /@ {1, 2, 3} {* {0.9701, 0.992525, 0.992525} *} Phi = Sqrt[((c2 + c3)^2 - c1^2)/(4 c2 c3)] {* 0.87245 *} PhiPr = ((c3 - c2)/c1) Phi {* 0. *} Because 0 <= Phi^2 <= 1 is ...


1

There are several ways to display a number in a specific way. Some of them, with examples: EngineeringForm[10000.] = 10.x10^3 ScientificForm[10000.] = 1.x10^4 NumberForm[10000., {7, 1}]= 10000.0 And some special as AccountingForm that use accounting notation featuring e.g. a special notation of negative numbers. NumberForm seems most relevant for you, ...


8

If your operation can be converted to a canonical ranking rather than a pairwise comparison then you can use MaximalBy introduced in version 10. If not a good approach to a single pass through a list is Fold. Here is a function using that: foldMax[list_, p_] := Fold[If[p[##], ##] &, list] This proves to be faster in some cases than using Ordering ...


9

You are asking for a solution to the equation $10^r\equiv 1$, mod $n$, where in your particular case $n=37$. The multiplicative order is the smallest exponent $r$ such that $x^r\equiv 1$, mod $n$. The multiplicative order is given by the Mathematica command MultiplicativeOrder[x,n], and corresponds to the "Foo" you asked for in your comment to @mgamer. ...


1

Maybe these other approaches will be useful for you pointer to function problem : 1.expr[x_] := 2*func[x] + 5 You can now for example always use expr but switch func to your needs. For example : func = Sin ; expr[hello] func = #^2 &; {expr[hello], expr[5]} return 5 + 2 Sin[hello] {5 + 2 hello^2, 55} But if you really need to create ...


10

We can solve the equation after a fashion with some algebraic manipulation. There infinitely many complex solutions, but Mathematica can find the ones in a finite domain. Here is the left hand side of the OP's equation: lhs = (2/5)^(2/5) Sqrt[x] λ^(1/5) BesselI[-(2/5), 4/5 x^(5/4) Sqrt[λ]] C[1] Gamma[3/5] + ((-2)^(2/5) Sqrt[x] λ^(1/5) BesselI[2/5, ...


2

I seem to recall a similar question but I cannot find it now. Anyway a key detail is that the body of the function presumably should not be evaluated, meaning e.g. func = Print[#^2] & should still work. Here is one approach: expr = Hold[2*{"FUNCTION1"} + 5]; func = Print[#^2] &; newfunc = Function @@ expr /. {"FUNCTION1"} -> Hold @@ func // ...


8

This does not appear to be possible to solve in Mathematica, but here's a couple steps that might help. Simplify it: Simplify[(2/5)^(2/5) Sqrt[x] λ^(1/5) BesselI[-(2/5), 4/5 x^(5/4) Sqrt[λ]] C[1] Gamma[3/5] + ((-2)^(2/5) Sqrt[x] λ^(1/5) BesselI[2/5, 4/5 x^(5/4) Sqrt[λ]] C[2] Gamma[7/5])/5^(2/5)] which gives Sqrt[x] λ^( 1/5) (BesselI[-(2/5), ...


2

Simple brute force : r = 1; While[Not@IntegerQ[(10^r - 1)/37], r++] r returns 3.


3

We can reduce your problem to solving a linear equation modulo an integer. That way we can avoid functions like Minimize. If a primitive root exists for the modulus, we can take discrete logarithms of both sides and easily solve for r. This following code finds the smallest positive r such that (b^r - c)/mod ∈ Integers. ...


14

Let us try to produce the solution without applying brute force, similar to mgamer's answer (that did not actually use Mathematica). Reduce[Mod[10^r - 1, 37] == 0, r, Integers] (* -> C[1] \[Element] Integers && C[1] >= 0 && r == 3 C[1] *) We see that the value of r can in fact be any nonnegative multiple of 3. The result sought is ...


6

Instead of a brute-force approach on larger and larger powers of ten, this constructs the multiplier digit by digit by dividing from the bottom up. There is only one choice for each digit. This is much faster than the brute-force approach when the number of digits is large. nines[n_ /; n > 2 && OddQ[n] && ! Divisible[n, 5]] := ...


9

Find the first positive integer that satisfies the condition: NestWhile[# + 1 &, 1, ! Element[(10^# - 1)/37, Integers] &] 3 Or r = 0; NestWhile[Element[(10^(++r) - 1)/37, Integers] &, False, Not]; r 3 Or Block[{r = 1}, While[! Element[(10^r++ - 1)/37, Integers]]; r - 1] 3


3

Because the domain is described as Integers, and because negative integers cannot satisfy the condition, the first non negative integer that satisfy the condition is 0, as the following expression can prove if necessary. NestWhile[# + 1 &, 0, ! IntegerQ[(10^# - 1)/37] &] (* 0 *) If the domain is instead positive Integers, a sligtly modification is ...


3

From $(10^r-1)/37=n$ it follows $10^r=37n+1$ The first $n$ such that $37n+1$ is a power of 10 is 27, since $37*27+1=1000$. So $(10^3-1)/37=3$. So r=3


0

I can reproduce the crash on V10.0.1 Mac OSX 10.9.5. parabolloid = Function[{input1, input2}, x = input1^2; y = input2; x + y^2]; This crashes the kernel (uncomment or copy without the comment markers if you wish to run): (* RegionPlot[parabolloid[x, y] < 1, {x, 0, 2}, {y, 0, 2}] *) Note the function is not well-defined: parabolloid[x, y] ...


2

The answer is pretty simple and you were very close to seeing this yourself: Default[Times] (* Out[9]= 1 *) As you see, the DefaultValues for Times and Plus are already built in.


1

You could use the following functions (that D Lichtblau wrote I believe) headlist = {Or, And, Equal, Unequal, Less, LessEqual, Greater, GreaterEqual, Inequality}; getAllVariables[f_?NumericQ] := Sequence[] getAllVariables[{}] := Sequence[] getAllVariables[t_] /; MemberQ[headlist, t] := Sequence[] getAllVariables[ll_List] := ...



Top 50 recent answers are included