When I try to calculate the following integral in Mathematica 8, I get this strange result:
In[1]:= Integrate[y/((1 + x^2 + y^2)^(3/2)), {y, 0, 1}]
Maple 14 can solve this one easily:
Why is Mathematica giving me a different result?
Try this
r = Integrate[y/((1 + x^2 + y^2)^(3/2)), {y, 0, 1}]
r = Assuming[Element[x, Reals], Simplify[r]];
Together[r]
which gives
(-Sqrt[1+x^2]+Sqrt[2+x^2])/(Sqrt[1+x^2] Sqrt[2+x^2])
Which is the same as Maple's :
Related
Using ParametricPlot I can plot a lemniscate expressed in parametric coordinates:
ParametricPlot[1/(1 + Sin[t]^2) {Cos[t], Cos[t] Sin[t]}, {t, 0, 2 [Pi]}]
I want to find using Mathematica the equivalent cartesian expression and plot it using ContourPlot that I know to be:
ContourPlot[(x^2 + y^2)^2 == (x^2 \[Minus] y^2), {x, -1, 1}, {y,-1,1}]
Looking up among the MMA functions I wondered if CoordinateTransformData or TransformedField could help me but none of them has the appropriate coordinate transformation :"Parametric" -> "Cartesian" which had me baffled.
How can this be done ?
It depends how much automatic solution you want.
eq = Thread[{x, y} == 1/(1 + Sin[t]^2) {Cos[t], Cos[t] Sin[t]}];
cont = Eliminate[eq, t] // Simplify
y != 0 && x^4 + y^2 + 2 x^2 y^2 + y^4 == x^2
ContourPlot[Evaluate#Last#cont, {x, -1, 1}, {y, -1, 1}]
I have a task to solve equation system with FindRoot:
a*x+b*y^2-c*x^2=a-b, a*x^2+b*y^2+b*x-cy=b+c , where a = 10, b = 10, c = 6;
I'm very(!) new to Mathematica and have one day to get to know it.
Any comments on how to solve that equation will be much appreciated!
Thanks!
This starts searching for root at x = 0 and y = 0
eq = {a*x + b*y^2 - c*x^2 == a - b, a*x^2 + b*y^2 + b*x - c*y == b + c}
param = {a -> 10, b -> 10, c -> 6}
result = FindRoot[eq /. param, {x, 0}, {y, 0}]
This only gives you 1 of the two Real solutions. Solve will give you both (and even some solutions with Complex numbers). To test it:
eq /. param /. result
This returns (True, True) so you know you've found the correct root.
To find the solution graphically, use ContourPlot:
ContourPlot[Evaluate[eq /. param], {x, -5, 5}, {y, -5, 5}]
Here is the version with Solve/NSolve
NSolve[{a*x + b*y^2 - c*x^2 == a - b,
a*x^2 + b*y^2 + b*x - c*y == b + c} /. {a -> 10, b -> 10, c -> 6},
{x, y}]
It will give the 4 solutions. If you use Solve instead of NSolve you will have a (large) closed form of each component of each solution.
If you need more digits for the solution, add at the end of the NSolve command the option WorkingPrecision->30 (or any other number of digits. These are quadratics, they can computed to any precision necessary).
I'm writing a simple Mathematica implementation of the black-scholes model and using Plot3D to plot the pricing surface. However, when I run this code, no plot is produced. My call and put functions to produce correct values when run separately, but no plot is produced. Code:
Clear[d1, d2, call, put, stockPrice, strikePrice, riskFreeRate, timeToExp, volatility]
d1[stockPrice_, strikePrice_, riskFreeRate_, timeToExp_, volatility_] := (Log[stockPrice / strikePrice] + (riskFreeRate + 0.5*volatility^2)*timeToExp) / (volatility * Sqrt[timeToExp])
d2[stockPrice_, strikePrice_, riskFreeRate_, timeToExp_, volatility_] := d1[stockPrice, strikePrice, riskFreeRate, timeToExp, volatility] - volatility*Sqrt[timeToExp]
call[stockPrice_, strikePrice_, riskFreeRate_, timeToExp_,
volatility_] := stockPrice * CDF[NormalDistribution[0, 1], d1[stockPrice, strikePrice, riskFreeRate, timeToExp, volatility]] - strikePrice * Exp[-riskFreeRate*timeToExp] *CDF[NormalDistribution[0, 1], d2[stockPrice, strikePrice, riskFreeRate, timeToExp, volatility]]
Plot3D[call[stockPrice, 500, 0.0030, timeToExp, 0.39], {stockPrice,
10, 1000}, {timeToExp, 0.0833333, 5}]
Other plots, like this sample from the reference, do work.
Plot3D[{x^2 + y^2, -x^2 - y^2}, {x, -2, 2}, {y, -2, 2},
RegionFunction -> Function[{x, y, z}, x^2 + y^2 <= 4],
BoxRatios -> Automatic]
You just have timeToExp call[] and timetoExp (lowercase t) in the specification of the plot range.
Following code returns different values for NExpectation and Expectation.
If I try the same for NormalDistribution[] I get convergence erors for NExpectation (but the final result is still 0 for all of them).
What is causing the problem?
U[x_] := If[x >= 0, Sqrt[x], -Sqrt[-x]]
N[Expectation[U[x], x \[Distributed] NormalDistribution[1, 1]]]
NExpectation[U[x], x \[Distributed] NormalDistribution[1, 1]]
Output:
-0.104154
0.796449
I think it might actually be an Integrate bug.
Let's define your
U[x_] := If[x >= 0, Sqrt[x], -Sqrt[-x]]
and the equivalent
V[x_] := Piecewise[{{Sqrt[x], x >= 0}, {-Sqrt[-x], x < 0}}]
which are equivalent over the reals
FullSimplify[U[x] - V[x], x \[Element] Reals] (* Returns 0 *)
For both U and V, the analytic Expectation command uses the Method option "Integrate" this can be seen by running
Table[Expectation[U[x], x \[Distributed] NormalDistribution[1, 1],
Method -> m], {m, {"Integrate", "Moment", "Sum", "Quantile"}}]
Thus, what it's really doing is the integral
Integrate[U[x] PDF[NormalDistribution[1, 1], x], {x, -Infinity, Infinity}]
which returns
(Sqrt[Pi] (BesselI[-(1/4), 1/4] - 3 BesselI[1/4, 1/4] +
BesselI[3/4, 1/4] - BesselI[5/4, 1/4]))/(4 Sqrt[2] E^(1/4))
The integral for V
Integrate[V[x] PDF[NormalDistribution[1, 1], x], {x, -Infinity, Infinity}]
gives the same answer but multiplied by a factor of 1 + I. This is clearly a bug.
The numerical integral using U or V returns the expected value of 0.796449:
NIntegrate[U[x] PDF[NormalDistribution[1, 1], x], {x, -Infinity, Infinity}]
This is presumably the correct solution.
Edit: The reason that kguler's answer returns the same value for all versions is because the u[x_?NumericQ] definition prevents the analytic integrals from being performed so Expectation is unevaluated and reverts to using NExpectation when asked for its numerical value..
Edit 2:
Breaking down the problem a little bit more, you find
In[1]:= N#Integrate[E^(-(1/2) (-1 + x)^2) Sqrt[x] , {x, 0, Infinity}]
NIntegrate[E^(-(1/2) (-1 + x)^2) Sqrt[x] , {x, 0, Infinity}]
Out[1]= 0. - 0.261075 I
Out[2]= 2.25748
In[3]:= N#Integrate[Sqrt[-x] E^(-(1/2) (-1 + x)^2) , {x, -Infinity, 0}]
NIntegrate[Sqrt[-x] E^(-(1/2) (-1 + x)^2) , {x, -Infinity, 0}]
Out[3]= 0.261075
Out[4]= 0.261075
Over both the ranges, the integrand is real, non-oscillatory with an exponential decay. There should not be any need for imaginary/complex results.
Finally note that the above results hold for Mathematica version 8.0.3.
In version 7, the integrals return 1F1 hypergeometric functions and the analytic result matches the numeric result. So this bug (which is also currently present in Wolfram|Alpha) is a regression.
If you change the argument of your function u to avoid evaluation for non-numeric values all three methods gives the same result:
u[x_?NumericQ] := If[x >= 0, Sqrt[x], -Sqrt[-x]] ;
Expectation[u[x], x \[Distributed] NormalDistribution[1, 1]] // N;
N[Expectation[u[x], x \[Distributed] NormalDistribution[1, 1]]] ;
NExpectation[u[x], x \[Distributed] NormalDistribution[1, 1]];
{% === %% === %%%, %}
with the result
{True, 0.796449}
I am attempting to solve the linear biharmonic equation in mathematica using DSolve. I think this issue is not just limited to the biharmonic equation but MATHEMATICA just spits out the equation when I attempt to solve it.
I've tried solving other partial differential equations and there was no trouble.
The biharmonic equation is just:
Laplacian^2[f]=0
Here is my equation:
DSolve[
D[f[x, y], {x, 4}] + 2 D[D[f[x, y], {x, 2}, {y, 2}]] +
D[f[x, y], {y, 4}] == 0,
f,
{x, y}]
The solution is spit out as
DSolve[(f^(0,4))[x,y]+2 (f^(2,2))[x,y]+(f^(4,0))[x,y]==0,f,{x,y}]
That is obviously not the solution. What gives? What am I missing? I've solved other PDEs without boundary conditions.
How about try it in polar coordinates? If f(r, \[Theta]) is symmetric with respect to azimuth \[Theta], the biharmonic equation reduces to something Mathematca can solve symbolically (c.f. http://mathworld.wolfram.com/BiharmonicEquation.html):
In[22]:= eq = D[r D[D[r D[f[r],r],r]/r,r],r]/r;
eq//FullSimplify//TraditionalForm
Out[23]//TraditionalForm= f^(4)(r) + (2 r^2 f^(3)(r) - r f''(r)
+ f'(r))/r^3
In[24]:= DSolve[eq==0,f,r]
Out[24]= {{f -> Function[{r},
1/2 r^2 C[2] - 1/4 r^2 C[3] + C[4] + C[1] Log[r]
+ 1/2 r^2 C[3] Log[r]
]}}
In[25]:= ReplaceAll[
1/2 r^2 C[2]-1/4 r^2 C[3]+C[4]+C[1] Log[r]+1/2 r^2 C[3] Log[r],
r->Sqrt[x^2+y^2]
]
Out[25]= 1/2 (x^2+y^2) C[2]-1/4 (x^2+y^2) C[3]+C[4]+C[1] Log[Sqrt[x^2+y^2]]+
1/2 (x^2+y^2) C[3] Log[Sqrt[x^2+y^2]]
DSolve[D[f[x, y], {x, 4}] + 2 D[f[x, y], {x, 2}, {y, 2}] +
D[f[x, y], {y, 4}] == 0, f, {x, y}]
This ought to be the actual syntax