How to evaluate a function on Imported data (.xlsx file)

I have imported an excel file, and I want to evaluate an expression on each point:

Data = Import["data.xlsx"] which gives output like this:


I want to evaluate following expression for each point:


where, a is some type constant. So far I am trying to use Evaluate function in mathemtica:

Evaluate[Rate, Data[[All, {1, 2}]]] Evaluate[Rate, Data[[All, {3, 4}]]] 

But couldn’t figure out how to obtained values for expression (Rate) for each value. Any help will be appreciated.

How to plot a crossection of a ParametricPlot3D?

I’m plotting a surface using ParametricPlot3D. Ideally I would like to plot an intersection of the surface with a plane just as seen in another question (Cross Sections or slices of 3d figures – Mathematica). However in my case the parameters u and v are not equivalent to the spatial coordinates x,y. This is why i tried RegionPlot in place of CountourPlot to plot all pairs (x,y) that intersect the surface for a given value of z. This is not working somehow and I’m lost trying to look for alternatives.

Surface[u_, v_] = {Cos[u], Sin[u] + Cos[v], Sin[v]};  Row[{Show[{ParametricPlot3D[      Surface[u, v], {u, 0, 2Pi}, {v, -Pi, Pi},       PlotStyle -> Opacity[ 0.2]],      Graphics3D[{Blue, Opacity[0.4],        InfinitePlane[{0, 0, 0}, {{1, 0, 0}, {0, 1, 0}}]}]},     ImageSize -> 300],    RegionPlot[    Exists[{u, v}, Surface[u, v] == {x, y, 0}], {x, -1, 1}, {y, -1, 1},     ImageSize -> 300]}] 

In reality my surface is a bit more complicated so I chose a simple one for posing this question. Also ideally I would like to be able to change around the normal vector of the plane later, checking out any angle of the crosssection I like, so please keep that in mind when thinking about the question. Thank you very much for any engagement in my problem!

Plot the solution from DSolve

I’m trying to solve a differential equation as in the following code:

FullSimplify[DSolve[x'[t] == a + b E^(g t) + (c + d E^(-g t)) x[t], x[t], t]] 

which generates

enter image description here

Now, I would like to plot it with specific parameter values assigned, for example: a = 1; b = 2; c = 3; d = 4; g = 0.1; A = 1 where I replaced the integration constant c_1 with A.

Here is my code for plotting x[t]:

a = 1; b = 2; c = 3; d = 4; g = 0.1; A = 1 x[t_] := E^(-((d E^(-g t))/g) + c t) (A + Integrate[E^((d E^(-g K[1]))/g - c K[1]) (a + b E^(g K[1])), {K[1], 1, t}]) Plot[x[t], {t, 1, 10}] 

It runs forever. To check whether Mathematica is doing calculations, I tried


and it yielded


which is nice. But when I tried


I got

enter image description here

It seems Mathematica cannot compute the integral unless the integration region is $ \int_1^1$ . Is this because the integrand is too complicated? Is there any way to let Mathematica compute it? Thanks!

How to solve the function and check the concavity of the function with respect to x?

enter image description here

As shown in the above formula.

Given x, we are able to solve nR and thereby the pi.
We check that nR increase in x using x ranging from [0:1:50] by plotting. But how to check the concavity of pi with respect to x to show the existence and uniqueness of x* Finally, check how the optimal x* changes with alpha.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear;

v0 = 0;  v = 10;  m = 4;  f = 1.5;  pT = 8;  beta = 20;  n = 100;  gamma = 10;  w0 = 10;  alpha=0.2; c0=1; c1=0.3; alpha=0.2; x_gd = [0:1:10000];  for nk = 1:length(x_gd)  x = x_gd(nk);   funCustomer = @(y) y - n.*exp (v-m*f+beta.*gamma.*x./(m.*y)-w0)./(exp(v0)+...           exp(v-m*f+beta.*gamma.*x./(m.*y)-w0)+exp(v-pT));   nR(nk) = fzero(@(y) funCustomer(y), [-1000, 1000]);  nS(nk) = n.*exp (v-pT)./(exp(v0)+exp(v-m.*f+beta.*gamma.*x./(m.*nR(nk))-w0)+exp(v-pT));  pi_x(nk)=(1-0.2)*f*m*nS(nk)-c1*x-c0; end  figure(1)  plot(x_gd,nR) xlabel('x');  ylabel('nR');   figure(2)  plot(x_gd,nS) xlabel('x');  ylabel('nS');   figure(3)  plot(x_gd,pi_x) xlabel('x');  ylabel('pi_x'); 

Derivative of Sort

I want to calculate gradient of Sort[{x1,x2,x3,x4}] but it seems like I’m getting a wrong answer. Specifically Plot[Sort[{2, 1, x, 10}][[4]], {x, -20, 20}] gives me

enter image description here

while D[Sort[{2,1,x,10}],x] returns {0,0,0,1}, which is not true.

Is there a way to fix it?

Should Reduce give all cases when $\sqrt{x y} = \sqrt x \sqrt y$?

My understanding is that Reduce gives all conditions (using or) where the input is true.

Now, $ \sqrt{xy} = \sqrt x \sqrt y $ , where $ x,y$ are real, under the following three conditions/cases

$ $ \begin{align*} x\geq 0,y\geq0\ x\geq0,y\leq0\ x\leq0,y\geq 0 \ \end{align*} $ $

but not when $ x<0,y<0$

This is verified by doing

ClearAll[x,y] Assuming[Element[{x,y},Reals]&&x>= 0&&y<= 0,Simplify[ Sqrt[x*y] - Sqrt[x]*Sqrt[y]]] Assuming[Element[{x,y},Reals]&&x<= 0&&y>= 0,Simplify[ Sqrt[x*y] - Sqrt[x]*Sqrt[y]]] Assuming[Element[{x,y},Reals]&&x<= 0&&y>= 0,Simplify[ Sqrt[x*y] - Sqrt[x]*Sqrt[y]]] Assuming[Element[{x,y},Reals]&&x<= 0&&y<=  0,Simplify[ Sqrt[x*y] - Sqrt[x]*Sqrt[y]]] 

Mathematica graphics

Then why does

 Reduce[ Sqrt[x*y] - Sqrt[x]*Sqrt[y]==0,{x,y},Reals] 

Give only one of the 3 cases above?

Mathematica graphics

Is my understanding of Reduce wrong or should Reduce have given the other two cases?

V 12 on windows.

Datasets and Time values: Filtering and Plotting

After struggling for a few weeks, I have accumulated a few examples of challenging date-related operatations with datasets. Some I have partial solutions; others I cannot yet do. The resources on Datetime objects in datasets is thin on here (both are relatively new to Mathematica, so no surprise), so maybe this will fill in that gap.

For all of my data, we have events that occur at a datetime. We want to understand how the progress on given days compare; for example, when did task A occur on April 4, 2017 vs October 12, 2018? What is the distribution for Task A occurring throughout a day over 100 days?

  1. How can I plot a DateHistogram with a bin of, say, 20 minutes? I can do it by hour: DateHistogram[dataset, "Hour", DateReduction->"Day"]

  2. How can I utilize the DateHistogram plot style using the operator form? A relatively new functionality allows the syntax dataset[plotstyle,"Key"] (see this nice StackExchange thread). For example, I can implement:

    dataset[Groupby[Key["State"]] /* (PieChart[#, ChartLabels->Keys[#]]&), Length]

    I want to use this syntax with DateHistogram; I can implement the most basic version:


    I want to add the DateReduction option, along the lines of:


    Unfortunately, the above example doesn’t work and I can’t find more documentation.

  3. How can I select objects in a certain time window? I figured out a method, but perhaps there is a more elegant solution. In the below example, I can plot the events that occur before 2016:

    eventsBefore2016 = dataset[Select[#Sent<DateObject[{2016,1,1}]&,"Sent"]; DateHistogram[eventsBefore2016,"Hour",DateReduction->"Day"]

    Can I make these two lines into one line of code, for example? Could I do it using the operate form (question 2)?

  4. How can I adapt a DistributionChart for a dataset of Datetime values? I don’t example starter code for this one.

(Apologies for the generic code examples below; I can’t share the actual data and my workplace blocks the tutorial datasets such as titanic. If this is a real sticking issue I can adapt this question with a tutorial dataset later at home.)

Optimization of Do loop with multiple variables

I am pretty new to Mathematica and I would like to optimize a Do loop with multiple variables that I have.

I have three lists of three elements each, with the condition that the first element must be bigger than the second and so on. From those, I construct matrices and make operations on them; since I want to scan on all the possible matrices, I have this big Do loop:

l1={a,b,c};l2={d,e,f};l3={g,h,i}; Do[ m1=Table[(l1[[i]]+l2[[j]]),{i,3},{j,3}]; (*just as an example, not the real operations*) m2=Table[(l1[[i]]+l3[[j]]),{i,3},{j,3}]; {vl,m1d,vr}=SingularValueDecomposition[m1]; {ul,m2d,ur}=SingularValueDecomposition[m2]; ... ,{a,2,5},{b,a-1},{c,0,b-1},{d,2,5},{e,d-1},{f,0,e-1},{g,2,5},{h,g-1},{i,0,h-1} ] 

The operations inside the loop are much more difficult, and this loop would take way too much time to complete. How can I improve my situation? I’ve read something about the use of Map but I didn’t quite understand how it should apply to my case.

How to reduce the time of calculation using ParalleDo instead of Do Loop?

I am trying to use ParalleDo function through ParametricNDSolveValue and NIntegrate.

The question is pretty similar how to apply paralleDo to my code

First I intend to use Do loop and its works fine but It cost a longer time, so to reduce the computational time I want to use ParalleDo, but I get an error message and I don’t understand where it comes from.

Moreover, I intend to write the output data in data file formate by using

  file = OpenWrite[        "file1.dat",         FormatType -> TableForm]; 

But I got the following error OutputStream[file1.dat ,3] is not open.

Below is my minimal working code and some comments:

 l1 = 0.81;         Z = 1500;         x0 = 10;         v0 = 0.02;         \[Epsilon] = $  MachineEpsilon;          l0 = 0.0714`20.;          ps = ParametricNDSolveValue[{y''[r] +                2 y'[r]/r == -4 \[Pi] l k Exp[-y[r]], y[\[Epsilon]] == y0,              y'[\[Epsilon]] == 0, WhenEvent[r == 1, y'[r] -> y'[r] + Z l]}, {y,              y'}, {r, \[Epsilon], R}, {k, l},             Method -> {"StiffnessSwitching"}, AccuracyGoal -> 5,             PrecisionGoal -> 4, WorkingPrecision -> 15];        file = OpenWrite[            "file1.dat",             FormatType -> TableForm];         ParallelDo[x = i x0;           v = i^3 v0;           R = Rationalize[v^(-1/3), 0];           l = Rationalize[l1/(i x0), 0];           nn = FindRoot[Last[ps[y0, l]][R], {y0, -1}, Evaluated -> False][[1,              2]];           Tot = 4 \[Pi] nn NIntegrate[              r^2 Exp[-First[ps[nn, l]][r]], {r, \[Epsilon], R},               PrecisionGoal -> 4];           Print[NumberForm[i*1., 5], "  ", NumberForm[Tot, 5]];, {i, 292/100,             31/10, 1/100}] // Quiet // AbsoluteTiming Close[file];