[WSG21] Daily Study Group: Wolfram Language Basics

Good morning/evening,

In exercise 3.7 if I use the following code (in version 12.0) I generate the error General::ivar: -12.5659 is not a valid variable:

Manipulate[
 Plot[{Sin[x], Normal[Series[Sin[x], {x, 0, n}]]}, {x, -4 \[Pi], 
   4 \[Pi]}, PlotRange -> {-10, 10}], {n, 1, 23, 2}]

In order to fix it I have to force the evaluation of the series expansions as:

Manipulate[
 Plot[{Sin[x], 
   Normal[Series[Sin[x], {x, 0, n}]] // Evaluate}, {x, -4 \[Pi], 
   4 \[Pi]}, PlotRange -> {-10, 10}], {n, 1, 23, 2}]

Does anyobody experience the same? Most importantly, does anybody know why the first code snippet is not a valid Wolfram statement?

Thanks

Plot has attribute HoldAll and tries to evaluate the function Normal[Series[…]] only after assigning specific numerical values to x. As you have shown in the resolution, in this case we need to force the symbolic evaluation of Series[Sin[x],{x,0,someInteger}] to obtain the power series expansion for Sin[x] before trying to substitute numeric values for x.
This is why the following is unable to plot:

Plot[Normal[Series[Sin[x], {x, 0, 10}]], {x, -2 Pi, 2 Pi}]

But using Evaluate resolves the issue:

Plot[Evaluate[Normal[Series[Sin[x], {x, 0, 10}]]], {x, -2 Pi, 2 Pi}]

Thanks Abrita for the explanation!

From time to time I consider equations like “f(x) = some right-hand side”. To investigate this kind of equations, I want to transform them by a rule like \xi = x/some factor, say \xi = x/a. (Typically, x and a have the same dimension and thus \xi is dimensionless.) Therefore, I want Mathematica to find and collect all quotients of x/a and replace them with \xi.

Replacing “(1 + x)^6 /. x -> 3 - a” is quite simple and standard. But the reverse way doesn’t seem to work, does it? Unfortunately I didn’t find any hints in the reference.

So, how do you deal with this symbolic issue?

Hi Larawag,

I’m not sure that I understand what you’re asking—if I do understand correctly, then that seems to work:

In[1]:= f[x_]:=2+Sin[x/a]
In[2]:= f[x]/.x/a->ξ
Out[2]:= 2+Sin[ξ]

Are you asking for something different? If so, could you provide an explicit sample input and the expected output?

Thanks!

Arben

Thank you very much for your quick reply, Arben!

You hit my point exactly, but “of course” I have more difficult examples in mind:

Example 1:

In[1]:= (-((4 s^2)/(-1 + s^2)^2) + (2 s)/(-1 + s^2))/(
  2 (1 + (4 s^2)/(-1 + s^2)^2)^(3/2)) + 
  Sin[(2 s)/(-1 + s^2)] /. (2 s)/(-1 + s^2) -> \[Xi]

Out[1]= (-((4 s^2)/(-1 + s^2)^2) + \[Xi])/(
 2 (1 + (4 s^2)/(-1 + s^2)^2)^(3/2)) + Sin[\[Xi]]

My expected output looks like this

(-\[Xi]^2 + \[Xi])/(2 (1 + \[Xi]^2)^(3/2)) + Sin[\[Xi]]

Example 2: In the example above I have already helped Mathematica, of course it would be nice if the task could also be solved when one is one step further away - i.e. if this equation had to be substituted:

-((2 s^2)/((-1 + s^2)^2 (1 + (4 s^2)/(-1 + s^2)^2)^(
   3/2))) + s/((-1 + s^2) (1 + (4 s^2)/(-1 + s^2)^2)^(3/2)) + 
 Sin[(2 s)/(-1 + s^2)] /. (2 s)/(-1 + s^2) -> \[Xi]

So you could summarize that I would like to have a) an automatism (or a workaround or a strategy) that collects the terms as desired (see Example 2) and b) also considers powers of the variable (see Example 1).

sol = NDSolve[{f’‘ + f’ + 8 f == 0, f[0] == 0, f’[0] == 1},
f, {x, 0, 10}] ;

Plot[Evaluate[f[x] /. sol], {x, 0, 10}, PlotRange -> All]

Expressions like Plot[Evaluate[f[x] /. sol] are given in the Help-examples of NDSolve

In the solution for Exercise 5.8 Plotting a result from NDSolve the following expression is given

Plot[f[x] /. sol, {x, 0, 10}, PlotRange -> All]

Both expressions give the same diagram.

In which cases i have to use Plot[Evaluate[f[x] /. sol] and in which cases i have to use Plot[f[x] /. sol] ?

Hi Ulf,
A good rule of thumb is to use Evaluate when you want to ensure all symbolic processing of the expression has been completed before Plot (which has HoldAll attribute) tries to substitute numerical values for x in the function.

In this particular example, using Evaluate to first replace f with the InterpolatingFunction from the NDSolve solution is ideally the right way to proceed. However, if we substitute x with a numeric value and then replace f with another function, it still works fine (replacement of f is not affected by the numeric value of x).

The order in which evaluation proceeds is more important in the example quoted a few comments back in this thread:

Plot[Evaluate[Normal[Series[Sin[x], {x, 0, 10}]]], {x, -2 Pi, 2 Pi}]

Here you absolutely want to finish the symbolic evaluation of Normal[Series[Sin[x], {x, 0, 10}]]] before numeric values of x are passed to it. So you need to force the evaluation using Evaluate.

2 Likes

Hi larawag,

Ah, I see what you mean. That is indeed a difficult general problem to solve, and I’ve run into it myself in the past. After some searching, I’ve found something that seems to work… it still needs a bit of simplification (with Simplify or FullSimplify), but it ought to do what you want, and thee discussion surrounding it provides some insight as to why this is a nontrivial problem:

https://mathematica.stackexchange.com/questions/3822/can-i-simplify-an-expression-into-form-which-uses-my-own-definitions

Using:

FullSimplify[expr // TermErsetzung[2 s == \[Xi] (-1 + s^2), s], 
 Assumptions -> \[Xi] > 0]

I was able to get pretty close to your desired output (really, just requiring breaking up the sum. Hopefully this is helpful!

Arben

1 Like

Good day, everyone.

I was doing some exploring recently. And tried to use a web-camera image as a good source of data. I am using Mathematica 12.1 on my Raspberry Pi 4 with an USB webcam attached.

What I don’t understand is, when I try to grab an image, I get an error. I did the documentation digging and found out, that there were special variables $ImagingDevice and $ImagingDevices, that select and represent the devices, the image is grabbed from. I checked, what device was used, and without chaning anything the image grab started working as expected. I reproduced this behaviour several times and made a screenshot.

What was I doing wrong?
Thanks in advance,
F.

When will the Advanced Programming recording become available to rewatch?

Yesterday’s Study Group recording is now available: https://wolfr.am/TyQ6Qt0q

Reminding our Study Group participants that the New in Version 12.2 webinar starts in less than 1 hour, at noon CST. Today’s topics include the latest enhancements in visualization, the new area of bio-sequence computation, and new functionality for Wolfram Notebooks and working with code.

Sign up for the webinar here: https://wolfr.am/TyPQmtDD

Thank you Arben for bringing this link to my attention (and of course for the quick and on-target reply…).

1 Like

Hi Fedor, Unfortunately I do not have a better explanation other than a guess that the code behind $ImagingDevices (or FindDevices[] or other similar functions) triggered an initial handshake with the device or camera. Seems like initially DeviceOpen or DeviceRead was failing under the hood behind CurrentImage. Once the camera device was recognized, CurrentImage started working.

We do have a Raspberry Pi group on community: https://community.wolfram.com/content?curTag=raspberry%20pi
Perhaps you will find other folks there who may have seen and tried some troubleshooting for similar issues?

You’re welcome, Larawag—glad to be able to help!

Arben

1 Like

Thanks Arben, The german term Ersetzung attacted me as I read German. That’s a useful function. John.

1 Like

Executing the packed array timing example I didn’t get the order of magnitude faster execution. Attached file shows the timing I got. Anyone have an idea why the timing stayed the same? Thanks in advance.

05 Numerics PackArray Timing.nb (7.91 KB)

Exercise 7.7 states “…load your package to introduce the rules for the function fib…”

How exactly do i load the package fib as generated by exercise 7.7?

Needs["..."] or Get["..."] are not working properly.

Hi Arben, see code snippet attached. Question why I didn’t see an order of magnitude faster timing.
John

05 Numerics PackArray Timing.nb (7.91 KB)