Cannot assign raw object 2

My objective is to build a function using modules that runs the bisection method. I have a working bisection method but when I try to implement it as a module I keep getting this error and I can’t figure out why.

F[z_]:=3 + z + z^2 * Cos[z]   bisectionMethod[f_, a_, b_, n_] := Module[   {},   For[i = 0, i < n, i++,    Print[StringForm["Loop #: ``", i + 1]];    c = (a + b)/2;    y = f[c];    If[y < 0, a = c];    If[y > 0, b = c];    Print[StringForm["a = ``", N[a]]];    Print[StringForm["b  = ``", N[b]]];    c = (a + b)/2;    Print[StringForm["c = ``", N[c]]];    Print[N[f[c]]];    ]      ] bisectionMethod[F, -4 , 2 , 10]  

After being run

  Loop #: 1  Set::setraw: Cannot assign to raw object 2.  a = -4.  b  = 2.`  c = -1.  2.5403  Loop #: 2  Set::setraw: Cannot assign to raw object 2.  a = -4.  b  = 2.`  c = -1.  2.5403 

The issue appears to be specifically variable b but I have no idea how b is any different from the others and how to tackle this.

I did a little bit of experimentation and it seems like the line If[y > 0, b = c]; is problematic

Replacing $x,y$ in $f(x,y,z)$ with values from a list

I’m sure there is a simple solution to this using map or apply, but its not occurring to me.

Suppose I have a function $ f(x,y,z)= x+y+z)$

And I want to evaluate $ f(x,y,1)$ for $ \{x,y\} ∈ \{\{1,2\},\{3,4\}\}$

What is the best way to do this?


If I use f[x_,y_,z_]:= x+y+z; f[#1,#2,1]&/@ {{1,2},{3,4}}

this will give me things like {{2+#2,3+#2}}.

On the other hand, Apply works on a single element f[#1,#2,1]& @@ {1,2} but not on a list such as {{1,2},{3,4}}. On the list it gives me {f[1,3,1],f[2,4,1]

I am not sure how to go from the case of a single pair to a list of pairs

Animate ellipse drawing

$ (x/3)^2+(y/5)^2=1$ , $ x=3\sin[t]$ , $ y=5 \cos[t]$

I want to draw a phase trajectory with time-lapse using manipulate or animate.

Manipulate[ParametricPlot[(x/3)^2 + (p/5)^2 == 1, {x, -3, 3}, {p, -5, 5}], {t, 0, 10}] I tried this code. It is not working. What is the proper version?

Optimizing a slow For loop

I know that this is a "beginner question", and, in fact, I am. I want to improve my code, as it takes really too much time to run. I have already read some other discussions, like here, but I am struggling in translating the simplifications with Table or Do into my example.

The For cycle I want to improve is the following:

zh = 0.4; list = {}; eqdiff = phi''[x] + 2*phi'[x]/x + (2*(zg + zh*Exp[-zh*x/dh])/x + 1)*phi[x] == 0; For[i = 0, i < 10000, i++,      With[{zg = -0.6, dh = i*10^-2},           nsol = Block[{eps = $  MachineEpsilon},           NDSolve[{eqdiff, phi[eps] == 1, phi'[eps] == -(zg + zh)}, phi, {x, eps, 20000},                   WorkingPrecision->MachinePrecision, AccuracyGoal->15, PrecisionGoal->8, MaxSteps->Infinity]]];       AppendTo[list, 1/Evaluate[(15000*phi[15000])^2 + ((15000-Pi/2)*phi[15000-Pi/2])^2 /. nsol[[1]]]];] 

Clearly, this code, written in this way, is highly inefficient. Also, I need to do more of these, with different values for zg inside With, and make some plots out of the lists.

Anyone that can help me with this noob question? Thanks a lot!

How do i use Streamplot to plot a non homogenous differential equation

I have the following equations:

x’ = x-y y’ = x+y-2xy

I used the following code to do the streamplot: I apologize for only having an image, as I am using Mathematica through my school servers I cannot copy and paste the data.

enter image description here

But I do not get any results, so then I linearize the equations and plot them separately, but I cannot figure out how to combine the plots to show in one plot.:

enter image description here

Is there any way to use Streamplot with nonhomogenous DE’s or a better way to combine the equations?

What is the syntax for using Reap and Sow and WhenEvent with derivative = 0 with interpolating function?

I have a system of three ODEs. This system oscillates. I need to determine the frequency of each of the three solutions.

My file. You can download and modify this.

I’m new to Mathematica and I know this question has been asked multiple times, but I can’t get it working.

{xSol, ySol, zSol} = NDSolveValue[{ode1, ode2, ode3, ic2}, {x, y, z}, {t, 0, 200}] 

Plotting xSol, ySol and zSol (three interpolating functions) gives:


Can anyone show me how to determine the frequency of this thing using Reap and Sow or FFT. I’ve been instructed already how to do this using EcoEvo package, but I’m also wondering how to do it without such a package.

Both the Reap/Sow solution and the FFT come pretty close to what I have and need. But I fail in modifying the mentioned solutions so that it works with my code.

How do I individually determine the frequency of xSol, ySol and zSol?

My implementation of WhenEvent

For example, when I try to use WhenEvent, I used it like this:

{xSol, ySol, zSol} = NDSolveValue[{ode1, ode2, ode3, ic2, WhenEvent[x'[t] == 0, Sow[t]]}, {x, y, z}, {t, 0, 200}] 

This gives me an error (which I presume to be about the WhenEvent) and my regular solutions. Because the solutions are correct, I think that the problem here is in the WhenEvent.


Error: Event location failed to converge to the requested accuracy or precision within 100 iterations between t = … and t = ….

All suggestions and solutions are highly appreciated! Many thanks😊

Numerical contour integral

I am trying to compute the double integral for fixed $ m,z>0$ :

   Integrate[(Gamma[y/2] Sqrt[Gamma[3 - y]/Gamma[y]])/   Gamma[(3 - y)/2] z^(3 - y)    (Exp[-m x] - 1) x^(y - 3)/x, {x, 0, \[Infinity]},{y,    3/2 - I \[Infinity], 3/2 + I \[Infinity]}] 

The integral over $ x$ can be done analytically, and the result depends on the product $ mz$ , so there is effectively just one parameter. The $ x$ integral needs to be split into two regions I suspect, and in one region the contour of the $ y$ integral would need to be deformed so that it remains convergent. Since the $ y$ integral involves complicated branch cuts, I wanted to be able to do it numerically for a range of $ mz$ , to get a least a few digits of precision. I am having difficulty getting stable results numerically tho.