. 1816 === Subject: Re: Array > The command: {A[1], A[2], A[3]} = {1, 2, 3} assigns a value to each A-variable. However, the command: Array[A,3] = {1,2,3} generates an error message. What is the difference between the two lines? > Bruce > Set (=) works in a special way when you make assignments to lists, as stated in the Help: ?Set lhs = rhs evaluates rhs and assigns the result to be the value {l1, l2, ... } = {r1, r2, ... } evaluates the ri, and assigns the results to be the values of the corresponding li. This is why your first example works. However, since Set does not evaluate its first argument, it tries to assign a rule to the symbol Array, which it can't do since Array is Protected. Hence what you should use is: Evaluate[Array[A,3]]={1,2,3} which will work exactly as the first example. Andrzej Kozlowski === Subject: Books on learning mathematics with Mathematica I'm (amongst other things) an autodidact programmer. Quite often I'll run into computing problems that I realize are well understood, I'll problem. Sprinkled in the text there's infallibly some mathematical formulas. That's were my troubles begin ;) One part of the problem is simply not being familiar mathematical notation. Once I grasp what the formula express I usually have no problem understanding the concept. If the formula had been expressed in pseudo code, I'd have an easier time following along. I've recently found out about Mathematica and it seems like it could be a great learning tool for someone like me. If I understand things correctly, Mathematica will let me enter formulas in the syntax of a programming language and then render them in standard mathematical notation, right? Now, to optimize this self study program, all I need is a great text book that uses Mathematica to teach math. Preferably the book should start with recapitulating high school math and than move on to undergraduate levels. TIA alex -- Alex Polite === Subject: Re: how to identify plane and measuring planar area > I am taking linear algebra and I have questions about the following code. > Can someone run it and help me here? These are the two questions --> (1) > How do I measure the planar area of this flat thing? (2) What plane does > this ellipsoid plot out on? > > What would I do in any case like this in the future to determine the plane? > > In[566]:= > {xstretch,ystretch,zstretch} = {1.5,0.8,0}; > ranger=1.5; > {slow,shigh} = {0,Pi}; > {tlow,thigh} = {0,2 Pi}; > > hungellipsoidplot = > ParametricPlot3D[ xstretch Sin[s] Cos[t] perpframe[1] + > ystretch Sin[s] Sin[t] perpframe[2] + > zstretch Cos[s]perpframe[3], {s,slow,shigh},{t,tlow, > thigh}, > > PlotRange->{{-ranger,ranger},{-ranger,ranger},{-ranger,ranger}}, > Axes->True,AxesLabel->{x,y,z}, > Boxed->False,ViewPoint->CMView,DisplayFunction->Identity]; > > setup =Show[hungellipsoidplot,frameplot,Axes3D[ranger], > DisplayFunction->$DisplayFunction]; > > > > Show[setup, ViewPoint -> 12 perpframe[1]]; > Could you, please, post something that works -- your code does not work at all -- or at least post all the required definitions -- definitions of perpframe and CMView are missing -- and possibly something that is syntacticly correct -- say a list of three functions when a list of three functions is expected rather than the sum of three functions -- and also meaningful -- the Show of the Show command, even in disguised of a setup expression, is quite meaningless -- ? Here is the last of my attempts -- at least you get a plot -- before I gave up: Evaluate[Array[perpframe, 3]] = {1, 2, 3} CMView = {1.3, -2.4, 2}; { xstretch,ystretch,zstretch}= { 1.5,0.8,0}; ranger=1.5; { slow,shigh}= { 0,Pi}; { tlow,thigh}= { 0, 2 Pi};[IndentingNewLine] hungellipsoidplot= ParametricPlot3D[ { xstretch Sin[s] Cos[t] perpframe[1], ystretch Sin[s] Sin[t] perpframe[2], zstretch Cos[s] perpframe[3]}, { s,slow,shigh}, { t,tlow,thigh}, PlotRange-> { { -ranger,ranger}, { -ranger,ranger}, { -ranger,ranger}}, Axes->True, AxesLabel-> { x,y,z}, Boxed->False, ViewPoint->CMView]; HTH, Jean-Marc === Subject: Re: Array Set has attribute HoldFirst, use Evaluate Attributes[Set] {HoldFirst, Protected, SequenceHold} Clear[A]; Evaluate[Array[A,3]] = {1, 2, 3}; Bob Hanlon > The command: > > {A[1], A[2], A[3]} = {1, 2, 3} > > assigns a value to each A-variable. However, the command: > > Array[A,3] = {1,2,3} > > generates an error message. > > What is the difference between the two lines? > > > Bruce > === Subject: Re: Problem with using /. Hi Nag, you try to assign a value to Sum[...]; this becomes Set[Sum[...],0] -- > but Sum is a Protected function. Furthermore, Set is not Rule. I guess you wanted: M[j_, t_, k_] := FullSimplify[D[MGF[t, k], {t, j}] /. u -> 0, > Sum[Subscript[p, i], {i, 0, k}] == 1]; You can use Simplfy instead of FullSimplify too. Peter Hi Peter: The = was a typo. It should have been a -> With -> there is no error but the substitution doesn't happen. The substitution works when I use == as you suggested. Why does -> not work but == work? Nag === Subject: Variable-level outlines? (of notebooks, in the notebooks themselves) Two generic things I'd very much like to be able to do in working with a lengthy notebook, *right in the notebook itself*, are: 1) Open or close all the cells in a notebook ranked above or below a given style, with the styles ranked in the same hierarchy as the cmd-N ordering in the Format >>Style menu, using a quick keyboard command (no selection, and no mousing, needed). In other words, be able to view and navigate (and print) the notebook itself in a pseudo outline or Table of Contents form, with all cells from cmd-1 down to cmd-N open and all lower level cells closed. One can of course accomplish something like this using the AuthorTools >> MakeContents palette -- but only by creating a separate TOC file, which has to be examined and dealt with, and can't itself be used, for example, to copy some subsection in the notebook itself and paste it into a new location. The user interface I'd visualize might be that cmd - ' would not just toggle open and closed a selected group but rather open *all* the cells in the notebook below a certain hierarchical style level, increasing by one level per tap (and decreasing one level per tap for each cmd - option '). Maybe I can gin up a palette with a button to do this . . . or maybe someone else already has? 2) More generically, I'd like to know how to create a palette that would use NotebookFind[] to add or remove or change one or more specific options in all the cells in a given notebook that are of a specific style and contain a specific content. As one use for this, I could insert cells of a certain header level that say ***** Page Break ***** at selected points in a long notebook, leaving them open but otherwise inactive, just so I'd know they were there. Then a button click could (a) insert PageBreakBelow->True into each of these cells, and (b) close the cell, so it wouldn't clutter up the printout. Maybe I'll try my (limited!) skills at a palette or package for that also. === Subject: Re: Array > The command: > > {A[1], A[2], A[3]} = {1, 2, 3} > > assigns a value to each A-variable. However, the command: > > Array[A,3] = {1,2,3} > > generates an error message. > > What is the difference between the two lines? > > > Bruce > Hi Bruce, The function Set, or = sign, has the attribute HoldFirst [1], that is the function does not create the array first but try to assign the list to the symbol Array[A, 3] and Array is of course write protected since it is a built-in function. Use Evaluate to force the evaluation of the first argument [2]. In[1]:= Array[A, 3] = {1, 2, 3}; --> Set::write : Tag (Array) in (Array[A, 3]) is Protected. More... In[2]:= Attributes[Set] Out[2]= {HoldFirst, Protected, SequenceHold} In[3]:= Evaluate[Array[A, 3]] = {1, 2, 3}; In[4]:= A[1] Out[4]= 1 Jean-Marc [1] http://documents.wolfram.com/mathematica/functions/HoldFirst [2] http://documents.wolfram.com/mathematica/functions/Evaluate === Subject: Re: Array > The command: > > {A[1], A[2], A[3]} = {1, 2, 3} > > assigns a value to each A-variable. However, the command: > > Array[A,3] = {1,2,3} > > generates an error message. > > What is the difference between the two lines? > > > Bruce > Hi Bruce, in the first line you evaluated Array[A,3] manually _before_ setting the value(s). The second line fails, because Set has got the attribute HoldFirst and Array is protected. Evaluate[Array[A,3]]={1,2,3} works perfectly. Peter === Subject: Re: Array >The command: {A[1], A[2], A[3]} = {1, 2, 3} assigns a value to each A-variable. However, the command: Array[A,3] = {1,2,3} generates an error message. What is the difference between the two lines? >Bruce > Whsat you have done is create an integer indexed function not an array: A[n_Integer]=Table[n,{n,1,3}][[n]] The array would be a={1,2,3} with elements a[[1]]=1 etc. === Subject: Re: Array > The command: {A[1], A[2], A[3]} = {1, 2, 3} assigns a value to each A-variable. However, the command: Array[A,3] = {1,2,3} generates an error message. What is the difference between the two lines? Array[A,3]={1,2,3} attempts to assign {1,2,3} to Array[A,3] (since Set doesn't evaluate the left hand side) whose head is protected. There are so many differences between the first and apparent intended meaning (I assume you want a1 dimensional array whose elements are 1,2 and 3) I don't know where to begin. Ssezi === Subject: Re: Array > The command: > > {A[1], A[2], A[3]} = {1, 2, 3} > > assigns a value to each A-variable. However, the command: > > Array[A,3] = {1,2,3} > > generates an error message. > > What is the difference between the two lines? > > > Bruce > Array[A,3] is the list {A[1],A[2],A[3]} only after evaluation so In[2]:= Evaluate[Array[A, 3]] = {1, 2, 3} Out[2]= {1, 2, 3} does what you want: In[3]:= DownValues[A] Out[3]= {HoldPattern[A[1]] :> 1, HoldPattern[A[2]] :> 2, HoldPattern[A[3]] :> 3} Adriano Pascoletti === Subject: Re: Re: variable K? (Really strange behavior . . . ) Hi AES, > The problem arises if it's your practice (as it is mine) to start > your notebooks with Remove[Global`*] as the first Input cell, and > then define various functions and modules in the immediately > following cells, carefully avoiding assigning any numerical values to > any symbols or parameters in those functions and modules until after > all those definitions have all been completed. > > Many of the symbols or parameters in those functions or modules will, > of course be given numerical values further down in the notebook, > when you run specific cases, but starting the notebook with > Remove[Global`*] means (or *should* mean) that if you run the > notebook once, then re-run it from the top, the initial functions and > modules will not be contaminated by numerical values assigned in the > previous run. > > The problem is, K is apparently *not* cleared out by > Remove[Global`*], and no warning of this is given; and hence if > you've used K as a variable in these initial definitions, the value > of K from the previous run is hard-wired into the initial > definitions on the following run -- whether that's what you intended > or not. I think it should be obvious that Remove[Global`*] is only doing almost what you want, and only by chance. As the documentation will tell it removes all symbols in the context Global`. If you define (own)values for symbols in other contexts, may that be System` as in the case you mentioned or in any other context, e.g. mycontext`var = 5, these will not be removed, and there is no reason to believe they should. Nothing wrong with Mathematica, I think. The fact that variable names starting with uppercase letters can conflict with internal names is well known and documented. If for readability you want to use short uppercase letters for variable names, I would suggest to use the script-Letters. These you can input from a palette or with Esc-scA-Esc for a script capital A. I use this all the time and have never had problems with it. For cleanup of the kernel you should check the package CleanSlate`, because that AFAIK has been made to provide the functionality you are misusing Remove[Globals*] for. If CleanSlate` will not cleanup System`-Variables that have been changed, than you could ask the author of that package (Todd Gayley I think) to include such a functionality or provide it yourself. hth, albert === Subject: Re: Re: variable K? (Really strange behavior . . . ) 3) Acquire CleanSlate --- which I did once, a while back; > discovered it > didn't seem to be -- at that time anyway -- just a click the button > process, but seemed to have its own learning curve and > complexities; and > tossed it. Over and out on this topic . . . --AES > You acquired CleanSlate?? I hope cheaply, since CleanSlate has been included with Mathematica since version 2 (if not earlier). Look in in ExtraPackages/Utilities folder, where you should also find the Notation package. I hope you did not acquire that too? ;-) Andrzej Kozlowski === Subject: Re: variable K? (Really strange behavior . . . ) > I think it should be obvious that Remove[Global`*] is only doing almost > what you want, and only by chance. As the documentation will tell it > removes all symbols in the context Global`. If you define (own)values for > symbols in other contexts, may that be System` as in the case you > mentioned or in any other context, e.g. mycontext`var = 5, these will not > be removed, and there is no reason to believe they should. Nothing wrong > with Mathematica, I think. Not to argue or quarrel here, just to comment. I'd suggest that an ordinary user (whatever that may mean -- perhaps someone who was trained and works in another field of engineering, science, business, statistics, you name it, and just uses Mathematica as a tool) can do an immense amount of productive, useful, and successful work in Mathematica, including using many of Mathematica's most powerful numerical and graphics tools, without *ever* hearing about -- much less coming to understand -- the concept of context in the Mathematica sense. [And if Remove[**GLOBAL`***] doesn't at least imply that it's removing everything that's around to be removed, well . . . ] > The fact that variable names starting with uppercase letters can conflict > with internal names is well known and documented. Yes -- depending on how deeply someone digs into the documentation. After all, some people might want to just turn on Mathematica **and start *using* it to do useful work**. And, as I noted in another post, there seem to be at present seven cap letters that are currently used as symbols in the System context. Six of these are documented as such in the Help system, and produce an error flag if you try to assign a value to them. One of them -- K, of course -- does *neither* of those helpful things (and all of the other 19 cap letters seem to work just fine -- currently at least -- as user variables). > If for readability you > want to use short uppercase letters for variable names, I would suggest to > use the script-Letters. These you can input from a palette or with > Esc-scA-Esc for a script capital A. I use this all the time and have never > had problems with it. Useful idea -- will explore. > For cleanup of the kernel you should check the package CleanSlate`, because > that AFAIK has been made to provide the functionality you are misusing > Remove[Globals*] for. If CleanSlate` will not cleanup System`-Variables > that have been changed, than you could ask the author of that package (Todd > Gayley I think) to include such a functionality or provide it yourself. Always wondered why the core concept of CleanSlate wasn't built into Mathematica from the start. If I want the CleanSlate functionality, the choices seem to be: 1) Shut down and restart Mathematica. 2) Do a bunch of learning and testing to discover and understand all the Remove[] and Clear[] commands I need to give to accomplish the same thing (in essence, write my own CleanSlate). 3) Acquire CleanSlate --- which I did once, a while back; discovered it didn't seem to be -- at that time anyway -- just a click the button process, but seemed to have its own learning curve and complexities; and tossed it. Over and out on this topic . . . --AES === Subject: RE: Array Bruce, You need to Evaluate the left hand side, and generally also Clear A in advance if you ever reevaluate. Clear[A]; Evaluate[Array[A, 3]] = {1, 2, 3}; {A[1], A[2], A[3]} {1, 2, 3} David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ The command: {A[1], A[2], A[3]} = {1, 2, 3} assigns a value to each A-variable. However, the command: Array[A,3] = {1,2,3} generates an error message. What is the difference between the two lines? Bruce === Subject: RE: how to identify plane and measuring planar area Your code won't evaluate because we don't know what perpframe is. Is it some kind of police procedure? David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ I am taking linear algebra and I have questions about the following code. Can someone run it and help me here? These are the two questions --> (1) How do I measure the planar area of this flat thing? (2) What plane does this ellipsoid plot out on? What would I do in any case like this in the future to determine the plane? In[566]:= {xstretch,ystretch,zstretch} = {1.5,0.8,0}; ranger=1.5; {slow,shigh} = {0,Pi}; {tlow,thigh} = {0,2 Pi}; hungellipsoidplot = ParametricPlot3D[ xstretch Sin[s] Cos[t] perpframe[1] + ystretch Sin[s] Sin[t] perpframe[2] + zstretch Cos[s]perpframe[3], {s,slow,shigh},{t,tlow, thigh}, PlotRange->{{-ranger,ranger},{-ranger,ranger},{-ranger,ranger}}, Axes->True,AxesLabel->{x,y,z}, Boxed->False,ViewPoint->CMView,DisplayFunction->Identity]; setup =Show[hungellipsoidplot,frameplot,Axes3D[ranger], DisplayFunction->$DisplayFunction]; Show[setup, ViewPoint -> 12 perpframe[1]]; === Subject: Re: Dot Product in Cylindrical Coordinates David, Can one do such calculations (with tangent vectors and differential forms) in Mathematica yet? Igor I get 2 also, using an independent calculation using differential forms and > Hodge star. I think the Mathematica vector calculus package get confused between the > points and the tangent vectors. > === Subject: Re: Errors in Mathematica > NDSolve[{P'[x] == Q[x], Q'[x] == 10^(-10) , > P[10^(-6)]==0, Q[10^(-6)] == 0, > n[x] == Exp[-A[x]] , > A'[x] == -A[x]/236, > A[10^(-6)]== -.0001}, > {P,Q,A,n}, {x, 10^(-6),1000}, > MaxSteps rightarrow Infinity] > Plot[Evaluate[P[x]/. %], {x,10^(-6), 10}] > > Here 2 equations link the functions P and Q. > Two other equations link the functions n and A. > There is no coupling between the two sets, and yet: > > Note the denominator 236 that appears in the second set. > As this number is changed the solution for P[x], and in particular the > value P[6] changes. > Of course this should not happen. > There is a discontinuity between 236 and 237 and another between 274 > and 275. > After that, further increase has no effect on P[6]. > > The problem goes away if the small numbers are increased. > It also goes away if the exponential function is replaced by unity or > some elementary function. > > If the small numbers are made much smaller it causes Mathematica to > shut down. > > Can anyone tell me how to handle this? > Hi Chris, I guess the change from 236 to 237 changes the stepsize of the algorithm at some point. You solve for (approx.) the range 0..1000 but use the value at 6. Using a different StartingStepSize eliminates this problem: P6 gives P[6] as a function of the 'magic' denominator and the StartingStepSize: P6[denom_, sss_:Automatic] := P[6] /. First[NDSolve[{P'[x] == Q[x], Q'[x] == 10^(-10), P[10^(-6)] == 0, Q[10^(-6)] == 0, n[x] == Exp[-A[x]], A'[x] == -A[x]/denom, A[10^(-6)] == -10^(-4)}, {P, Q, A, n}, {x, 10^(-6), 1000}, MaxSteps -> Infinity, StartingStepSize -> sss]] P6[237] - P6[236] --> 3.896888607874218*^-11 this is the effect, you have observed. Let's try a smaller sss: P6[237, 10^(-6)] - P6[236, 10^(-6)] --> 0. To see how small the stepsize has to be: Plot[Log[10, $MachineEpsilon + Abs[P6[237, Re[10^(-x)]] - P6[236, Re[10^(-x)]]]], {x, 0, 1}, AxesOrigin -> {0, Log[10, $MachineEpsilon]}]; The plot shows that for sss ~10^0.7 (ca. 5) and smaller values the effect vanishes. HTH, Peter === Subject: Re: scaled complementary error function in Mathematica? > Hi: > > I was wondering if anyone had a package or notebook file that computes the > scaled complementary error function directly? Specifically, something that > > Tanim Islam what is wrong with Exp[x^2]*Erfc[x]? Peter === Subject: Re: using FindRoot to find multiple answers in a domain? > IntervalRoots package and RootSearch do not work for the kind of formulas I > have. Are you sure? > For example in the domain [0,Pi/2] we want to solve (I only used FindRoot > because this was the fastest): > > I put the package I test in on > http://home.wanadoo.nl/akomur/testRootSearch.nb so that you can see the > formulas I use. > > Copying them In here ruins the formulas. Copy as Plain text. Your first formula is nlv = Max[-81.24275115593154 Cot[beta] - 48.489352280270914, Min[-46.844746272761526 Cot[beta] - 71.4213555357176, -46.480384751324436 Cot[beta] - 71.66426321667566]] Note that PiecewiseExpand can be applied to such formulae. If you simplify the result Simplify[PiecewiseExpand[nlv]] you deduce that nlv is just -81.24275115593154 Cot[beta] - 48.489352280270914 if Cot[beta] <= 2/3 -46.844746272761526 Cot[beta] - 71.4213555357176 if Cot[beta] >= 2/3 You can check that these values are consistent when Cot[beta] == 2/3. Moreover, the catch-all condition -46.480384751324436 Cot[beta] - 71.66426321667566 is redundant for real beta since it only applies when Cot[beta] == 2/3. A brief examination of nuvb shows that it involves ratios of (powers of) expressions similar to nlv. I expect that simplification of these expressions prior to constructing nuvb would lead to a final form that is not that hard to handle. I expect that if you step back and tell us more about the original problem, then a more elegant approach would present itself. Paul _______________________________________________________________________ Paul Abbott Phone: 61 8 6488 2734 School of Physics, M013 Fax: +61 8 6488 1014 The University of Western Australia (CRICOS Provider No 00126G) AUSTRALIA http://physics.uwa.edu.au/~paul === Subject: Re: Co-Displaying Combinatorica and Graphics Objects Show@Block[{$DisplayFunction=Identity}, Plot[x,{x,0,1}]}]] > Re Mathematica 5.2.0.0 under WinXP. > Let G2 be any Graphics object, e.g., G2 = Graphics[...]. I want to display G1 and G2 side-by-side via Show@GraphicsArray@{G1, G2}, but can't figure it out: - G1 isn't a Graphics object (and so my command fails). - I can display G1 via G3 = ShowGraph@G1, but then Show@GraphicsArray@{G3, G2} fails (G3 is a symbol). - Hoping to piece together a Graphics object from FullGraphics@G1, I am stumped by the embedded Rule object. Any ideas on how to display these two objects in an array? > Bruce -- http://chris.chiasson.name/ === Subject: Co-Displaying Combinatorica and Graphics Objects Re Mathematica 5.2.0.0 under WinXP. Let G2 be any Graphics object, e.g., G2 = Graphics[...]. I want to display G1 and G2 side-by-side via Show@GraphicsArray@{G1, G2}, but can't figure it out: - G1 isn't a Graphics object (and so my command fails). - I can display G1 via G3 = ShowGraph@G1, but then Show@GraphicsArray@{G3, G2} fails (G3 is a symbol). - Hoping to piece together a Graphics object from FullGraphics@G1, I am stumped by the embedded Rule object. Any ideas on how to display these two objects in an array? Bruce === Subject: Re: comboboxes and paste to notebook The last part of number two sounds like OutputForm. OutputForm does not generate boxes, or even correct Mathematica syntax. It is good for typeset display on terminals. > Hi! I'm having two problems with GUIKit, probably because I don't know a thing > about Java, and if you know the solution to any of: 1 - I have 3 comboboxes, say A, B and C. Combobox A has a BindEvent > (action): each item selected changes the list of items of combobox B. Now, > combobox B (wich list of items depends on the item selected on A), also > changes the items on combobox C through a BindEvent (action), the same way A > does on B... This on the comboboxes, but the rest of the GUI doesn't work > (loading of packages). It works if I put the BindEvent of B on another > widget, like a button, so I would like to know if anyone knows a way to > avoid using external widgets and to have the lists of items adjust itselves > automatically. > 2 - Is possible to send to a notebook a Mathematica expression (with > sub/superscripts, fractions, squareroots, etc...) from a widget? So far, the > result I get is always a distorted form of the expression, with powers in > different lines, fractions represented by a line of ---, etc. > L.9ccia -- http://chris.chiasson.name/ === Subject: comboboxes and paste to notebook Hi! I'm having two problems with GUIKit, probably because I don't know a thing about Java, and if you know the solution to any of: 1 - I have 3 comboboxes, say A, B and C. Combobox A has a BindEvent (action): each item selected changes the list of items of combobox B. Now, combobox B (wich list of items depends on the item selected on A), also changes the items on combobox C through a BindEvent (action), the same way A does on B... This on the comboboxes, but the rest of the GUI doesn't work (loading of packages). It works if I put the BindEvent of B on another widget, like a button, so I would like to know if anyone knows a way to avoid using external widgets and to have the lists of items adjust itselves automatically. 2 - Is possible to send to a notebook a Mathematica expression (with sub/superscripts, fractions, squareroots, etc...) from a widget? So far, the result I get is always a distorted form of the expression, with powers in different lines, fractions represented by a line of ---, etc. L.9ccia === Subject: Re: Unexpected Invalid comparison error when plotting function defined with a Condition pattern one of yours below. > Andrew, I do not know precisely why you obtain those error messages. It seems to be > a combination of selecting values in Plot, the use of Abs and the form of > the definition of g. But I think there are ways around it. The following give error messages. g[x_ /; 0 < x] = x; > Plot[Abs[g[10^y]], {y, 1, 2}]; Clear[g] > g[x_ ] = If[x > 0, x, 0]; > Plot[Abs[g[10^y]], {y, 1, 2}]; Clear[g] > g[x_] := Piecewise[{{0, x <= 0}, {x, x > 0} }] > Plot[Abs[g[10^y]], {y, 1, 2}]; The following do not give error messages. Clear[g] > g[x_ /; 0 < x] = x; > Plot[Sqrt[g[10^y]^2], {y, 1, 2}]; Clear[g] > g[x_ /; 0 < x] = x; > Plot[Abs[g[10^y]] // ComplexExpand // Evaluate, {y, 1, 2}]; Clear[g] > g[x_ /; 0 < x] = x; > data = Table[{y, Abs[g[10^y]]}, {y, 1, 2, 0.025}]; > ListPlot[data, PlotJoined -> True]; Clear[g] > g[x_?Positive] = x; > Plot[Abs[g[10^y]], {y, 1, 2}]; Clear[g] > g[x_] := Piecewise[{{0, x <= 0}, {x, x > 0} }] > Plot[Abs[g[10^y]] // ComplexExpand // Evaluate, {y, 1, 2}]; So try using ComplexExpand on your more complicated expression. David Park > djmp@earthlink.net > http://home.earthlink.net/~djmp/ Please consider the following simple function, defined for positive x > using a Condition pattern: g[x_ /; 0 < x] = x; When I try to plot this function in the following way: Plot[Abs[g[10^y]], {y, 1, 2}]; I unexpectedly receive some Less::nord : Invalid comparison with 10. + > 0. I attempted errors. Can anyone explain why this is, and how I > should best prevent it? Relevant note: > Both of the following different plots of g succeed with no errors: > Plot[Abs[g[y]], {y, 1, 2}]; > Plot[g[10^y], {y, 1, 2}]; Irrelevant note: > In my actual application, the function g is more complicated and is > complex-valued (hence my use of Abs); but the error is reproducable > with the very simple real-valued function g as defined above. > > Andrew === Subject: Re: Unexpected Invalid comparison error when plotting function defined with a Condition pattern Hi Jens, Does Mathematica give a small complex part for 10^y for any y in the range {1,2}? I can't find any y for which it does. Of course there are various ways I can modify my expression until it works; my question is really about understanding why Mathematica's Plot function has this behaviour, so that I can predict it and design for it in the future. Andrew just read the error message -- 10^y gaves a small > imaginary part and g[x_?NumericQ /; 0 < Chop[x]] := x will fix the problem. Jens Andrew Moylan | > | Please consider the following simple function, > defined for positive x > | using a Condition pattern: > | > | g[x_ /; 0 < x] = x; > | > | When I try to plot this function in the > following way: > | > | Plot[Abs[g[10^y]], {y, 1, 2}]; > | > | I unexpectedly receive some Less::nord : > Invalid comparison with 10. + > | 0. I attempted errors. Can anyone explain why > this is, and how I > | should best prevent it? > | > | Relevant note: > | Both of the following different plots of g > succeed with no errors: > | Plot[Abs[g[y]], {y, 1, 2}]; > | Plot[g[10^y], {y, 1, 2}]; > | > | Irrelevant note: > | In my actual application, the function g is more > complicated and is > | complex-valued (hence my use of Abs); but the > error is reproducable > | with the very simple real-valued function g as > defined above. > | > me on this. > | > | > | Andrew > | === Subject: Re: Trigonometric simplification noticing the factor of 2). The reason I need to get 2 + 4*Cos(a)^3 is to match a published solution in a homework solutions manual. > > A while back Maxim gave a nice method to convert Cos[n a] into Cos[a]^_.: > > d = 2 + 3*Cos[a] + Cos[3*a] > > First, replace a with ArcCos[x] and use TrigExpand: > > In[5]:= TrigExpand[d /. a -> ArcCos[x]] > > Out[5]= 4 x^3 + 2 > > Then, replace x with Cos[a]. All together we have: > > In[6]:= TrigExpand[d /. a -> ArcCos[x]] /. x -> Cos[a] > > Out[6]= 4 (Cos[a])^3 + 2 Personally, I prefer to use Cos[n a] == ChebyshevT[n, Cos[a]]. So 2 + 3 Cos[a] + Cos[3 a] /. Cos[n_ a] :> ChebyshevT[n, Cos[a]] yields 2 + 4 Cos[a]^3 Paul _______________________________________________________________________ Paul Abbott Phone: 61 8 6488 2734 School of Physics, M013 Fax: +61 8 6488 1014 The University of Western Australia (CRICOS Provider No 00126G) AUSTRALIA http://physics.uwa.edu.au/~paul === Subject: Re: Why doesn't Mathematica solve this simple differential equation? > Here is the system I'm trying to solve. It's an electrical circuit > consisting of a capacitor C1 (with initial voltage 4.0 volts), a > resistor R1, and a diode in series. There is an NDSolve example involving a diode in the built-in documentation. Evaluate FrontEndExecute[FrontEnd`HelpBrowserLookup[ DemosLink, Chaotic Circuit]] and click on the Demos link. > Approach 1: > > eqns11 = {Q1'[t] == -Iloop[t], Q1[t] == C1*Vc[t], Vr[t] == > R1*Is*Exp[Vd[t]/0.026], Vc[t] == Vr[t] + Vd[t], Vc[0] == 4.0} > > eqns12 = eqns11 /. {C1 -> 1.0*10^-6, R1 -> 16, Is -> 10^-13} > > eqns12soln = NDSolve[eqns12, Q1, {t, 0, 1}] > > Approach 2: > > eqns21 = {Vc'[t] == -Id[t]/C1, Vc[t] == 0.026*Log[Id[t]/Is] + R1*Id[t], > Vc[0] == 4.0} > > eqns22 = eqns11 /. {C1 -> 1.0*10^-6, R1 -> 16, Is -> 10^-13} You mean eqns22 = eqns21 /. {C1 -> 1.0*10^-6, R1 -> 16, Is -> 10^-13} ? Even so, your equations are inconsistent. > eqns22soln = NDSolve[eqns22, Vc, {t, 0, 1}] > > Both approaches fail with Mathematica complaining that NDSolve::ndode: > Input is not an ordinary differential equation. Which is not suprising. Paul _______________________________________________________________________ Paul Abbott Phone: 61 8 6488 2734 School of Physics, M013 Fax: +61 8 6488 1014 The University of Western Australia (CRICOS Provider No 00126G) AUSTRALIA http://physics.uwa.edu.au/~paul === Subject: Re: Why doesn't Mathematica solve this simple differential equation? > Here is the system I'm trying to solve. It's an electrical circuit > consisting of a capacitor C1 (with initial voltage 4.0 volts), a > resistor R1, and a diode in series. > > > Approach 1: > > eqns11 = {Q1'[t] == -Iloop[t], Q1[t] == C1*Vc[t], Vr[t] == > R1*Is*Exp[Vd[t]/0.026], Vc[t] == Vr[t] + Vd[t], Vc[0] == 4.0} > > eqns12 = eqns11 /. {C1 -> 1.0*10^-6, R1 -> 16, Is -> 10^-13} > > eqns12soln = NDSolve[eqns12, Q1, {t, 0, 1}] > You want to solve for Q1. Unless there is an dependency in a previous definition of Iloop between Q1 and the voltages, the relevant equations remaining are: Q1'[t] == -Iloop[t] and Q1[t] == C1*Vc[t] with unknown(?) Iloop. Obviously this can not be solved. > > Approach 2: > > eqns21 = {Vc'[t] == -Id[t]/C1, Vc[t] == 0.026*Log[Id[t]/Is] + R1*Id[t], > Vc[0] == 4.0} > > eqns22 = eqns11 /. {C1 -> 1.0*10^-6, R1 -> 16, Is -> 10^-13} > > eqns22soln = NDSolve[eqns22, Vc, {t, 0, 1}] The same happens here more functions (all undefined?) than equations. > > > Both approaches fail with Mathematica complaining that NDSolve::ndode: > Input is not an ordinary differential equation. > > Another, simpler, problem (same circuit but without the R1) solves > happily, so long as I eliminate all intermediate variables manually. > > eqns1 = {Vd'[t] == -Is*Exp[Vd[t]/0.026]/C, Vd[0] == 4.0} > > eqns2 = eqns1 /. {C -> 1.0*10^-6, Is -> 10^-13} > > eqns2soln = NDSolve[eqns2, Vd, {t, 0, 1}] > One function, one equation - Mathematica is happy > > Any ideas? > > Joe Gwinn > consider this example, where elimination is trivial: In[4]:= DSolve[{f'[x] == g[x] - f[x], g[x] == Sin[x]}, f[x], x] DSolve::deqx: Supplied equations are not differential equations of the given functions. Out[4]= DSolve[{f'[x] == g[x] - f[x], g[x] == Sin[x]}, f[x], x] and In[5]:= DSolve[ Eliminate[{f'[x] == g[x] - f[x], g[x] == Sin[x]}, g[x]], f, x] solves the deq. HTH Peter === Subject: Re: Why doesn't Mathematica solve this simple differential equation? > Here is the system I'm trying to solve. It's an electrical circuit > consisting of a capacitor C1 (with initial voltage 4.0 volts), a > resistor R1, and a diode in series. > > > Approach 1: > > eqns11 = {Q1'[t] == -Iloop[t], Q1[t] == C1*Vc[t], Vr[t] == > R1*Is*Exp[Vd[t]/0.026], Vc[t] == Vr[t] + Vd[t], Vc[0] == 4.0} > > eqns12 = eqns11 /. {C1 -> 1.0*10^-6, R1 -> 16, Is -> 10^-13} > > eqns12soln = NDSolve[eqns12, Q1, {t, 0, 1}] Let's try this one: eqns11= { Q1'[t]== - Iloop[t], Q1[t]== C1* Vc[t], Vr[t]== R1*Is* Exp[ Vd[t]/0.026], Vc[t]== Vr[t]+ Vd[t], Vc[0]==4.0} --> {Derivative[1][Q1][t] == -Iloop[t], Q1[t] == C1*Vc[t], Vr[t] == E^(38.46153846153846*Vd[t])*Is*R1, Vc[t] == Vd[t] + Vr[t], Vc[0] == 4.} eqns12= eqns11/. { C1-> 1.0* 10^ -6, R1->16, Is-> 10^ -13} --> {Derivative[1][Q1][t] == -Iloop[t], Q1[t] == 1.*^-6*Vc[t], Vr[t] == E^(38.46153846153846*Vd[t])/625000000000, Vc[t] == Vd[t] + Vr[t], Vc[0] == 4.} If I follow you correctly, you want to solve for the function Q1[t], function that you have already defined as 1.*^-6*Vc[t], that is Q1[t] depends on the function Vc[t], which is itself defined as the sum of functions that depends on another function Vd[t] that is defined nowhere. Moreover, the derivative of Q1[t] is also specified as being equal to another undefined function Iloop[t]... You wonder then why NDSolve complains about the input not being an ODE? eqns12soln= NDSolve[ eqns12,Q1, { t,0,1}] --> NDSolve::ndode : Input is not an ordinary differential equation. More... Jean-Marc === Subject: Re: generalized foldlist problem - part 2 right - that's what I wanted to get. (Of course, I didn't take me a lot of time to get it from Jens or others answers) In the code below you will see what I want to do next with our matrix - it is explained below. So lets take list1=Table[Random[],{10}]; list2=Table[Random[Integer,{1,5}],{10}] lets use Anrzeje's solution ma=GFMatrix[list1,list2] and now my code that manipulates ma in the way explained below (it is not allowed that Plus@@ma[[i]] is >1 for every i. ) The code is the following moje[T_] := Module[{i, matrix, dl, ile, ile1, ile22, ile33}, dl[i_] := First[Flatten[Position[Plus @@ Take[matrix[i], i + 2], _?(# > 1 &)]] /. {} -> {0}]; matrixNew[i_] := ReplacePart[matrix[i], matrix[ i][[i + 2]] /. {0 -> list1[[i + 2]]}, i + 2]; ile2[i_] := If[dl[i] > 0, Last[Flatten[Position[Plus @@ Take[ matrixNew[i], i + 2], _?(# > 1 &)]] /. {} -> {0}], 0]; ile33[i_] := ile2[i] - dl[i] + If[dl[i] > 0, 1, 0]; matrix[0_] := ma; matrix[i_] := matrix[i] = Flatten[Join[{Map[PadRight[#, Dimensions[Map[PadRight[#, Length[#] + ile33[i - 1], 0, ile33[i - 1]] &, Drop[matrix[i - 1], i]]][[2]]] &, Take[ matrix[i - 1], i]], Map[PadRight[#, Length[#] + ile33[i - 1], 0, ile33[i - 1]] &, Drop[matrix[i - 1], i]]}], 1]; matrix[T]] and solution moje[Length[list1] - 1] This algorithm is very brutal and very time consuming. Program repeats calculations etc. Do you have any ideas how to improve it or rebuild it entirely? p.s. I made many tests using my code and it is correct, i.e it generates solutions that i expect. p.s2. The algorithm tries to simulate agents (say computing jobs) that arrive to a service (say computing element). Power of a service is <= one. Power of agents <=1 - so sometimes more than one agent can occupy a service , but their sum must by <= 1. Agents come in to a service following the rule FCFS (first come first serve) - it means tha tall agents must patiently waiting in the queue before execution. Have a beautiful week, Arek Andrzej Kozlowski napisal(a): > problem, that sounds > >>> I have two list > list1=={a,b,c,d,e} > list2=={3,2,5,1,6} > and I want to apply a modified version of FoldList to list1 in the > following way: list2 indicates that element a appears only 3 times > (if > space enough) beginning from the beginning of the list , element b > appears 2 times, c - 5 times , etc. > So the output should be > GeneralizedFoldList[list1,list2]=={a,a+b,a+b+c,c+d,c+e} > Now, I still need your help. What I want to do now, and what I have problem with, is to add a constraint to the algorithm, i.e every element in my GeneralizedFoldList must be less than one. The following example says what to do if it is not. > > Lets take two lists > > list1=={0.9,0.8,0.7} list2=={3,3,3} > > All your algorithms use PadRight (you pad 0's). So the following matrix is built > > {{0.9, 0.9, 0.9, 0, 0}, {0, 0.8, 0.8, 0.8, 0}, {0, 0, 0.7, 0.7, 0.7}} > > and we add elements along colums and obtain {0.9, 1.7, 2.4, 1.5, 0.7} > > The first element is less than 1 so it's ok. The second is > 1 what I need to avoid. I want to avoid it by shifting the nonzero elements of the second and third row of above matrix of two positions: {0,0,0,0.8,0.8,0.8,0}, {0,0,0,0,0.7,0.7,0.7}. > > I go on with suming along columns and discover that everything is fine until I have to add 0.8 and 0.7 what is >1. So I repeat the procedure by shfting hte third row of the number of position that is needed to ensure that my sum is <1. > > Finally I obtain {{0.9, 0.9, 0.9, 0, 0, 0, 0, 0, 0},{0,0,0,0.8,0.8,0.8,0,0,0},{0,0,0,0,0,0,0.7,0.7,0.7}} and Plus@@% is what I desire. > > Arek > > Here is a (not very pretty) code that should do what you want > starting with a matrix of the above kind: >> GF[P_]:==Module[{M==P},Do[M == NestWhile[ > Join[(PadRight[#1, Length[#1] + 1] & ) /@ > Take[#1, s], (PadLeft[#1, Length[#1] + 1] & ) /@ > Take[#1, s - Dimensions[#1][[1]]]] & , M, > !And @@ Thread[Total[#1[[All,Range[Length[ > Flatten[Split[#1[[s]]] /. {l___, > {(0)..}} :> {l}]]]]]] < 1] & ], > {s, Dimensions[M][[1]]}]; M] >> Let's verify that it works. First, starting with your original case: >> In[8]:== > M=={{0.9,0.9,0.9,0,0},{0,0.8,0.8,0.8,0},{0,0,0.7,0.7,0.7}}; >> In[9]:== > GF[M]//InputForm > Out[9]//InputForm== > {{0.9, 0.9, 0.9, 0, 0, 0, 0, 0, 0}, > {0, 0, 0, 0.8, 0.8, 0.8, 0, 0, 0}, > {0, 0, 0, 0, 0, 0, 0.7, 0.7, 0.7}} > Now let's change M somewhat: > In[10]:== > M=={{0.9,0.9,0.1,0,0},{0,0.1,0.8,0.3,0},{0,0,0.7,0.9,0.7}}; >> In[11]:== > GF[M]//InputForm > Out[11]//InputForm== > {{0.9, 0.9, 0.1, 0, 0, 0, 0, 0}, {0, 0, 0.1, 0.8, 0.3, 0, > 0, 0}, {0, 0, 0, 0, 0, 0.7, 0.9, 0.7}} > So you only need to combine this with your favourite code from > part 1. > (PS. In answering part 1 I obviously misunderstood your question, but > I think that was mainly because I do not think your function is in > any way a generalisation of Fold, or even of Fold > [Plus,element,list]. As far as I can see it does not really perform > any kind of folding. > Andrzej Kozlowski > In fact I still do not fully understand your original question and, in particular, if the example you originally posted (and which, if any, of the solutions various people provided) were correct. Just in case, here is a solution to your original question, which I think is different from any of the ones I have seen: > > GFMatrix[list1_, list2_] :== Normal[SparseArray[Flatten[Table[If[j =B2 i + list2 [[i]] - 1, Rule[{i, j}, list1[[i]]], Rule[{i, j}, 0]], {i, Length[list2]}, {j, i, Length[list1] + i - 1}], 1]]] > > GFMatrix builds the matrix, which, I think, you need in the second part of the question. The code makes no use of PadLeft or PadRight. However, note that in your original example, the output is not the same as you gave as the correct one, although the output below seems to me to fit better your verbal description of what you wanted done: > > list1=={a,b,c,d,e}; list2=={3,2,5,1,6}; > > GFMatrix[list1,list2] > > {{a, a, a, 0, 0, 0, 0, 0, 0}, {0, b, b, 0, 0, 0, 0, 0, 0}, {0, 0, c, c, c, c, c, 0, 0}, {0, 0, 0, d, 0, 0, 0, 0, 0}, {0, 0, 0, 0, e, e, e, e, e}} > > Total[%] > > {a,a+b,a+b+c,c+d,c+e,c+e,c+e,e,e} > > This is longer than the output you gave as the right solution, which was only a part of the above, namely {a,a+b,a+b+c,c+d,c+e}. > > Andrzej Kozlowski > > Oops, the code I copied and pasted in did not come out the way it > should have done. It shoudlhave been: GFMatrix[list1_, list2_] := Normal[SparseArray[Flatten[Table[If[j <= > i + list2[[i]] - 1, {i, j} -> list1[[i]], {i, j} -> 0], {i, Length > [list2]}, {j, i, Length[list1] + i - 1}], 1]]] > > Andrzej Kozlowski === Subject: Re: General--How to use CoefficientList command to get a matrice from a polynome? if you want an answer, you must formulate your question a bit more clear. Daniel > I'm trying to do the following: > First of all, I have this polynome Psid[[n]]= (-1)^n Psip[[n]] + ksi Sum[M[[n',n]] . Psip[[n']], {n',0,CN}] + 0(ksiåÓ). I need to get the matrix M. > > I put: > > CN=3; > Psip={1,0,0}; > Psid={1.0000000000000000000 - 0.9443595442280599 ksi, 0.6356361712260359 ksi, 0.06956731675750103 ksi}; > M = CoefficientList[Psid, ksi]. > > But it doesn't resolve me the problem :-( > > What to do please to find the M matrix. > > Any suggestion Please. > Lian. > > > > > > > > Link to the forum page for this post: > http://www.mathematica-users.org/webMathematica/wiki/wiki.jsp?pageName=Specia l:Forum_ViewTopic&pid=13214#p13214 > >