Skip to main content

Posts

plotting - Maclaurin series

Can someone tells me, why is that not working? I'm starting to learn Mathematica. I have to solved this eq and draw the graph. It is developing a series of Taylor in about x0 = 0. eq = 60 - 53 x - 13 x^2 + 5 x^3 + x^4 seq = Solve[eq == 0, x] p1 = D[eq, x] s1 = Solve[p1 == 0, x] f1 = seq + s1/1! x^1 z = Solve[f1, x] Plot[z, {x, 0, 2}] This is my equation to solve Answer eq = 60 - 53 x - 13 x^2 + 5 x^3 + x^4; The roots are seq = Solve[eq == 0, x] {{x -> -5}, {x -> -4}, {x -> 1}, {x -> 3}} The first order series expansion about zero is f1 = Series[eq, {x, 0, 1}] // Normal 60 - 53 x z = Solve[f1 == 0, x][[1]] {x -> 60/53} Plotting the polynomial and approximation and highlighting the roots Plot[{eq, f1}, {x, -5.5, 3.5}, Epilog -> {Red, AbsolutePointSize[5], Point[{x, eq} /. seq], Point[{x, f1} /. z]}, PlotRange -> {-50, 100}, PlotLegends -> "Expressions"] Looking closer at the region of interest Plot[{eq, f1}, {x, -0.5, 2}, Epilog -> {Re

graphics - Filling a rectangle with an image

Say I have some rectangles of different sizes drawn on the screen. How can I fill them all with the same image? Ideally, the image would be resized to completely fill each rectangle. I've been trying to do this by wrapping each rectangle in graphics and adding the image wrapped in an inset in the epilog for each one, something like this Example: Show[Graphics[{Rectangle[{0, 0}, {0.5, 0.3}]}, Epilog -> Inset[pic, {0, 0}, {0, 0}]], Graphics[{Rectangle[{0.5, 0.3}, {0.7, 0.4}]}, Epilog -> Inset[pic, {0, 0}, {0, 0}]]] but it's not giving me the result I want. Is there anything like Fill[Rectangle[],pic] that would fill the rectangle with the image pic? I would also be fine with using something other than rectangles. Maybe there's something like a frame I can work with instead and just draw the image into those? Answer Code: (*Sample data*) data = Table[ ColorData["Rainbow", t] /. RGBColor -> List, {t, 0, 1, 1/100}]; (*Visualize*) Graphics[{Textu

meta programming - Macro functions in GeneralUtilities?

Questions have been asked about the Macros package before (e.g., (83815) ), but I'm interested if anyone has some examples of using the macro related functions in GeneralUtilities: In[2397]:= Names["GeneralUtilities`*Macro*"] Out[2397]= {"DefineLiteralMacro", "DefineMacro", "MacroEvaluate", \ "MacroExpand", "MacroExpandList", "MacroPanic", "MacroRules", \ "UseMacros", "$MacroDebugMode"} I find their docstrings a bit cryptic: Answer The only official information about the Macro functions I am aware of can be found in a short segment in this presentation , which describes the "Macro System" as an essential part of the next compiler generation. However, the CreateMExpr shown there isn't included in Mathematica 10.4.1. (But maybe someone can find parts of it digging deeper into it?) Nevertheless, one can already apply the Macro System practically (while being aware tha

plotting - Legend in a Show

I am trying to put some legends on a graph but I am not able to do that. This is my code: k = 1; (* J/K *) T = 1; (* K *) nlm = NonlinearModelFit[data, V, {M, n}, λ]; nlm["BestFitParameters"] Show[ListPlot[data], Plot[Normal[nlm], {λ, 0, 8}, PlotStyle -> Red], Frame -> True, GridLines -> Automatic, GridLinesStyle -> Directive[Gray, Dashed], AspectRatio -> 1.6, ImageSize -> Large, FrameLabel -> {"λ", "σ [MPa]"}, LabelStyle -> {FontFamily -> "Helvetica", 14}] I tried to look on the internet already but I don´t understand how to do it. For example there is this discussion: Dynamic legends involving show In which it looks like the same problem of mine, but I am not able to fully understand it. I am really new with Mathematica , so please consider it. Thanks, Fab. EDIT: After discussing here, this is the new code which works for me. Of course as someone told me, it is not running this piece of code because you need

bugs - Why does x++ behave strangely under Defer?

Bug introduced in 10.4.1 or earlier and persisting through 11.1.0 or later Using x++ or x=x+1 inside Defer generates strange results. Evaluating the following commands one by one (in three different input cells), with Shift + Enter , one gets: x=1; Defer[x++]; x (* 15 but I have also seen 17! *) Even stranger, if one creates a notebook with only those same commands, restarts the kernel, and does Evaluation ► Evaluate Notebook then the command x returns 1 instead of 15 . Any explanations?

differential equations - Mathematica9: NDSolve slows down after repeated calls

Bug introduced in 9.0 and persisting through 11.0.1 I have noted that in Mathematica 9 my code, which involves a lot of calls to NDSolve , slows down considerably after some time. Apparently, the problem is NDSolve itself and it seems to be related to Mathematica 9, as the following example shows: RunNDSolve := Timing[Do[NDSolve[{f''[x] == -f[x], f[0] == 1, f'[0] == 0}, {f[x]}, {x, 0, 1}], {1000}]][[1]] RunNDSolve integrates a simple differential equation 1000 times using NDSolve and returns the time needed to carry out the integrations. In Mathematica 9, RunNDSolve takes longer and longer times as it is called again, as the following example shows: Table[{j, RunNDSolve}, {j, 50}] In Mathematica 9 (extract of the full result): j, RunNDSolve 1, 0.712 10, 2.649000 20, 4.869000 30, 7.326000 50, 13.372000 In Mathematica 8 (extract of the full result): j, RunNDSolve 1, 0.5880000000000001` 10, 0.593` 20, 0.5719999999999992` 30, 0.5670000000000002` 40, 0.57700000000000

functions - What best practices or performance considerations are there for choosing between Cases, Position, Pick and Select?

Cases , Select , Pick and Position each have different syntaxes and purposes, but there are times when you can express the same calculation equivalently using either of them. So with this input: test = RandomInteger[{-25, 25}, {20, 2}] {{-15, 13}, {-8, 16}, {-8, -19}, {7, 6}, {-21, 9}, {-3, -25}, {21, -18}, {4, 4}, {2, -2}, {-24, 8}, {-17, -8}, {4, -18}, {22, -24}, {-4, -3}, {21, 0}, {19, 18}, {-23, -8}, {23, -25}, {14, -2}, {-1, -13}} You can get the following equivalent results: Cases[test, {_, _?Positive}] {{-15, 13}, {-8, 16}, {7, 6}, {-21, 9}, {4, 4}, {-24, 8}, {19, 18}} Select[test, #[[2]] > 0 &] {{-15, 13}, {-8, 16}, {7, 6}, {-21, 9}, {4, 4}, {-24, 8}, {19, 18}} Pick[test, Sign[test[[All, 2]] ], 1] {{-15, 13}, {-8, 16}, {7, 6}, {-21, 9}, {4, 4}, {-24, 8}, {19, 18}} test[[Flatten@Position[test[[All, 2]], _?Positive] ]] {{-15, 13}, {-8, 16}, {7, 6}, {-21, 9}, {4, 4}, {-24, 8}, {19, 18}} Are there performance or other considerations that should guide which you shou