## Category Archives: Mathematica

## Let mathematica combine integral limits

You’re looking for TagSetDelayed I believe:

```
Unprotect@Integrate;
Integrate /:
Plus[Integrate[ft_, {t_, a_, b_}], Integrate[ft_, {t_, b_, c_}]] :=
Integrate[ft, {t, a, c}];
Protect@Integrate;
```

But be careful when you unprotect system functions…

*This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.*

## How to plot an implicit funtion

```
dist = MixtureDistribution[{6, 4}, {NormalDistribution[1, 0.3],
NormalDistribution[3, 0.3]}]
f[x_, y_] := y InverseSurvivalFunction[dist, x + y]
```

**Given function f[x,y], for any x, we can get the optimal value y*[x] to maximize f[x,y]. My question is how to plot y*[x] on support [0,1].**

To make you better understand the trend, here are some useful codes:

```
Manipulate[
Plot[y InverseSurvivalFunction[dist, x + y], {y, 0, 1},
ImageSize -> 400, PlotRange -> {{0, 1}, {-1, 2}}], {x, 0, 1}]
Plot3D[y InverseSurvivalFunction[dist, x + y], {y, 0, 1}, {x, 0, .5},
ImageSize -> 400, PlotRange -> {0, 1}]
```

*This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.*

## What kind of PDF files that MMA does not read (import)

I can confirm that neither Mathematica 10, nor Mathematica 9 will import this file. Your installation is not broken.

There is (very likely) nothing you can do to make Mathematica read this file. Your best best is transforming the PDF without changing it visually, then trying to import it again. There are many programs that can do this, including Adobe Acrobat, Ghostscript (`pdfopt`

?), OS X Preview, Multivalent, etc.

I tried File -> Export as PDF… from OS X Preview and the result can be read with Mathematica (though the colours are lost upon import).

*This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.*

## Poisson equation with a implicit nonlinearity

Consider the nonlinear Poisson equation for u=u(x,y) as follows:

u_xx+ u_yy = f(u) in {x,y | x^2+y^2<1}

x u_x+y u_y=0 on {x,y | x^2+y^2=1}

Here f(u) is a function which is implicit in u, and f(u) cannot have an explicit function. Moreover, by using NDSolve f(u) can be numerically plotted point by point.

Is it possible to use Mathematica to numerically solve this boundary value problem? Thank you very much!

## parsed matrix not deleting elements in adjusted matrix

I have a matrix that has been parsed to delete certain rows and columns, as well as delete certain elements on the diagonal on the matrix. The problem is that it does this in theory when i take the elements in terms of w[i,j]. However, when I put in an equation for w[i,j], it does not actually delete the elements. Here is the code in theory where it works M = 10; initselect = 5; secselect = 6; k[i_] := Sum[w[i, j], {j, 1, M}]; V[i_, j_] := Piecewise[{{w[i, i] – k[i], i == j}, {w[i, j], i > j}, {w[i, j], i < j}}] matrix = Table[V[i, j], {i, M}, {j, M}]; (*This generates the original matrix that is unparsed*) (*MatrixForm[matrix]*) stay = Join[Range[initselect], Range[secselect, M, 2]]; TrueMatrix = matrix[[stay, stay]]; (*This generates the matrix that has parsed the selected rows and \ columns*) MatrixForm[TrueMatrix]

(*delete=Complement[Range[M],Join[Range[5],Range[6,M,2]]];*) delete = Complement[Range[M], stay]; TrueMatrix /. w[_, Alternatives @@ delete] -> 0 // MatrixForm

It initially deletes the appropriate rows and columns to get the TrueMatrix, then the last line it also deletes the appropriate elements on the diagonal.

Now here is the code with an equation for w[i,j] M = 10; initselect = 5; secselect = 6; k[i_] := Sum[w[i, j], {j, 1, M}]; w[i_, j_] := i*j^2 V[i_, j_] := Piecewise[{{w[i, i] – k[i], i == j}, {w[i, j], i > j}, {w[i, j], i < j}}] matrix = Table[V[i, j], {i, M}, {j, M}]; (*This generates the original matrix that is unparsed*) (*MatrixForm[matrix]*) stay = Join[Range[initselect], Range[secselect, M, 2]]; TrueMatrix = matrix[[stay, stay]]; MatrixForm[TrueMatrix] (*This generates the matrix that has parsed the selected rows and \ columns*) (*MatrixForm[TrueMatrix];*)

(*delete=Complement[Range[M],Join[Range[5],Range[6,M,2]]];*) delete = Complement[Range[M], stay]; TrueMatrix /. w[_, Alternatives @@ delete] -> 0 // MatrixForm

Here when I print the TrueMatrix and when it computes the last line to get the final matrix, they are identical which shouldn’t be the case and isn’t the case when I just use w[i,j] without and expression.

Thank you for your help

*This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers. Want something else to read? How about ‘Grievous Censorship’ By The Guardian: Israel, Gaza And The Termination Of Nafeez Ahmed’s Blog*

## What is teh difference between Array and Table?

I cannot understand the difference between `Array`

command and `Table`

. I can reproduce the example in `Array`

help in MMA with Table:

```
Array[1 + #^2 &, 10]
Table[1 + j^2, {j, 1, 10}]
Array[f, {2, 3}, {0, 4}] // MatrixForm
Table[f[i, j], {i, 0, 1}, {j, 4, 6}] // MatrixForm
Array[f, 10, {0, 1}]
Table[f[i/9], {i, 0, 9}]
```

Are they the same exactly with the exception that `Table`

Is better?! At least it can be parallelized. What is the advantage of `Array`

over `Table`

? If they are the same I cannot get it why there is more than one way to do the same thing in MMA.

*This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers. Want something else to read? How about ‘Grievous Censorship’ By The Guardian: Israel, Gaza And The Termination Of Nafeez Ahmed’s Blog*

## Interpolation fails for elongated data

Consider a regular but unstructured array of points on a rectangular domain of arbitrary aspect ratio. In this example, the domain can be scaled along the x and y axes by the input arguments:

```
dataZ = RandomReal[{1, 5}, {10, 10}];
scaleTheDomainBy10To[bx_, by_] := Module[{},
data = Flatten[Table[{x*10^(bx - 1), y*10^(by - 1), dataZ[[x, y]]}
, {x, 1, 10}, {y, 1, 10}], 1];
Show[
ListContourPlot[data, ImageSize -> 250, PlotLabel -> {bx, by}],
ListPlot@Map[#[[{1, 2}]] &, data]
]
];
```

In v10 on Win7, When the aspect ratio of the domain approaches ~10^4, the interpolation methods inside ListContourPlot start to break down:

```
Grid[{{scaleTheDomainBy10To[3, 0], scaleTheDomainBy10To[3.5, 0], scaleTheDomainBy10To[4, 0]},
{scaleTheDomainBy10To[0, -3], scaleTheDomainBy10To[0, -3.5], scaleTheDomainBy10To[0, -4]}}]
```

I tried scaling the domain and then applying DataRange inside ListContourPlot, but apparently ListContourPlot scales the data before doing its thing, which seems rather counterproductive.

My current workaround is to scale the domain, construct an Interpolation object of order 1, then call ContourPlot on that object using explicitly scaled input arguments.

However, I am not happy with that solution because the domain of my actual data is slightly ragged, which means to prevent Mathematica from showing the regions where the interpolation object is garbage, I have to set explicit boundaries for the plot region, either using

```
ContourPlot[..., {x,1.1,9.9},{y,1100,9900}]
```

or using RegionFunction, which is slow and requires a hefty amount of manual twiddling to get the region right. One nice feature of ListContourPlot is that it usually seems to truncate the plot region to the Delaunay “hull” of the input data, which is a feature I’d like to keep using. (Anyone know if this is indeed what it is doing?)

Is there a way I can tell ListContourPlot what numerical precision to use when constructing the interpolation? I would have expected the algorithm to fail for aspect ratios approaching machine precision, not 10^4. If this can be done, all the other problems seem be handled elegantly inside ListContourPlot.

I am not really interested in trying to regularize or smooth my data, as some of the raggedness is crucial for interpretation.

*This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers. Want something else to read? How about ‘Grievous Censorship’ By The Guardian: Israel, Gaza And The Termination Of Nafeez Ahmed’s Blog*

## Constrained Nonlinear Optimization with expectations functions in the constraints

I am modeling an optimal compensation contract with a CRRA utility function

sol= Minimize[{a + s*100 + o*25, Expectation[(1/(1-c))*((((w + a) Exp[r*T]) + (s Exp[d*T] +o*l)*p Exp[(r-d-(1/2)*q^2)T +(x*Sqrt[T]*q)])^(1 – c)), x [Distributed] NormalDistribution[0, 1]]=-0.1349, Expectation[((((a + w)* Exp[r* T]) + (p *(l*o + s*Exp[d*T])* Exp[(-d-q^2*(1/2)+ r)*T + q*Sqrt[T]*x]))^(-c))*(l*o+ s*Exp[d*T])*Exp[(-d-q^2*(1/2)+r)*T+q*Sqrt[T]*x], x [Distributed] NormalDistribution[0, 1]]>=0.00075, a >= 0, 0 <= s <= 1, 0 <= o <= 1}, {a,s,o}]

I need to find the results of expectation in the form of function(a,s,o) to be used in the constraints. This program have to be solved for multiples values of the parameters; in one case we have {{d -> 0.0269, q -> 0.3315, r -> 0.0435, T -> 1, w -> 1.1465, p -> 100, l -> 1, c -> 3}} and the initial values of a, s and o are: {{a -> 0.0941, s -> 0.0029, o -> 0.0089}} It is used to calculate the value of the constraint. I need to find the results of expectation in the form of function(a,s,o) to be used in the constraints. thank you very much in advance. Sawsen

## View inside the room

I used Cuboid to build 4 walls and make them composed like a room, the codes and result as followings.

My question is that is it possible to move the viewpoint to inside the room? The thing I want to do is making users feel themselves are inside the room, and they can rotate the screen to see what the room look like from the inside. Is it possible?

```
wallChosenColor = LightGray;
WallThick = 10;
roomLength = 10;
roomWidth = 5;
roomHeight = 2.5;
floorChosenColor = White;
Room3D = {
EdgeForm[],
Specularity[White, 50],
(*Walls*)
FaceForm[wallChosenColor],
Cuboid[{{-WallThick/100, -WallThick/100,
0}, {roomLength + WallThick/100, 0, roomHeight}}],
Cuboid[{{roomLength, -WallThick/100,
0}, {roomLength + WallThick/100, roomWidth + WallThick/100,
roomHeight}}],
Cuboid[{{-WallThick/100, roomWidth,
0}, {roomLength + WallThick/100, roomWidth + WallThick/100,
roomHeight}}],
Cuboid[{{-WallThick/100, -WallThick/100, 0}, {0,
roomWidth + WallThick/100, roomHeight}}],
(*Floor*)
Opacity[1],
FaceForm[floorChosenColor],
Cuboid[{0, 0, 0}, {roomLength, roomWidth, 0}]
};
Graphics3D[{Room3D}
, ViewPoint -> {-3, -2, 1}
, ImageSize -> {400, 400}, Lighting -> "Neutral"]
```

## How to make a function for (1/2) harmonic series

*[unable to retrieve full-text content]*

How to make a function for 1/2 Hn (Harmonic series) and plot it for values between 1 and 100?

Could you help me with this? Thanks.