I tried to calculate this:
limit(x^5−6*x^4+14*x^3−20*x^2+24*x−16, x, 1.999993580023622);
But I got 0.
I think it happens because a loss of significance. What can I do to get more precise result?
(1) use horner to rearrange the expression so that it can be evaluated more accurately.
(%i1) display2d : false;
(%o1) false
(%i2) horner (x^5-6*x^4+14*x^3-20*x^2+24*x-16, x);
(%o2) x*(x*(x*((x-6)*x+14)-20)+24)-16
(%i3) subst (x=1.999993580023622, %);
(%o3) -1.77635683940025E-15
(2) use bigfloat (variable precision) arithmetic.
(%i4) subst (x=1.999993580023622b0, x^5-6*x^4+14*x^3-20*x^2+24*x-16);
(%o4) -1.332267629550188b-15
(%i5) fpprec : 50 $
(%i6) fpprintprec : 8 $
(%i7) subst (x=1.999993580023622b0, x^5-6*x^4+14*x^3-20*x^2+24*x-16);
(%o7) -1.5876314b-15
(3) use rational arithmetic, which is exact.
(%i2) rat (1.999993580023622);
rat: replaced 1.999993580023622 by 38317775/19158949 = 1.999993580023622
(%o2) 38317775/19158949
(%i3) subst (x=38317775/19158949, x^5-6*x^4+14*x^3-20*x^2+24*x-16);
(%o3) -4098340979864306910009/2581418432543842245350194942774769749
(%i4) float (%);
(%o4) -1.587631407677531E-15
Related
I have a problem calculating with significant figures in Wolfram Mathematica.
Let me explain better.
I have
f[a_, b_] = a b Sin[25]
and
f[92.0 , 9.81] =381.421
However, I would first like to approximate the result of the product between a and b to three significant digits and then multiply it by Sin [25]. In short, I would like a function like this
f1[a_, b_] = NumberForm[a b, {3, 0}] Sin[25]
But if I evaluate
f1[92,0 , 9.81]
I get
f1[92,0 , 9.81]= 903.Sin[25]
instead of 381.62.
How should I modify f1[a_, b_] to get f1[92,0 , 9.81]=381.62 ?
You can use Round to round to 3 significant digits in your specific case. Then the result is an integer, so Sin[25] does not convert to a real number (a floating point number). However this can be forced with N.
Also Sin assumes radian input unless the input is specified as degree.
Note use of SetDelayed (:=) for the function definition.
f[a_, b_] := N[Round[a b] Sin[25 Degree]]
f[92.0, 9.81]
381.624
For 3 significant digits on a b in general you can use
f[a_, b_] := N[Round[a b, 10^(-3 + Floor[Log10[Abs[a b]]] + 1)] Sin[25 Degree]]
E.g. rounding a b
a = 1.2345;
b = 5.4321;
N[Round[a b, 10^(-3 + Floor[Log10[Abs[a b]]] + 1)]]
6.71
I would like Mathematica to evaluate square root of a squared variable. Instead it is just returning the squared variable under square root. I wrote a simple code as an example:
x = y^2
z = FullSimplify[Sqrt[x]]
But it is returning y^2 under a square root sign!
This behavior is documented on the Sqrt reference page:
Sqrt[z^2] is not automatically converted to z.
[…]
These conversions can be done using PowerExpand, but will typically be correct only for positive real arguments.
Thus:
In[1]:= x = y^2
Out[1]= y^2
In[15]:= PowerExpand[Sqrt[x]]
Out[15]= y
You can also get simplifications by supplying various assumptions:
In[10]:= Simplify[Sqrt[x], Assumptions -> Element[y, Reals]]
Out[10]= Abs[y]
In[13]:= Simplify[Sqrt[x], Assumptions -> y > 0]
Out[13]= y
In[14]:= Simplify[Sqrt[x], Assumptions -> y < 0]
Out[14]= -y
If you want more help, I suggest asking on the Mathematica Stack Exchange.
I would like to solve the following equation:
DSolve[u''[x]+k^2 u[x], u[x],x]
if k^2<0 the solution is
u[x]-> C[1] e^(kx) + C[2] e^(-kx)
if k^2>0 the solution is
u[x] -> C[1] Sin [kx] + C[2] Cos[kx]
in my equation
k^2=(a-b)/(c-d)
when b >a and c >d, meaning k^2<0
when I plug the equation into Mathematica, it reverses the sign and given me the exponents solution and not the cosine one.
does anyone have an idea how to plug the Assumptions or Conditions into the equation? Or patch between the two so I'll get the true solution?
Cheers
introduce a constant k2n which is the negative of your assumed negative k^2:
First#DSolve[{u''[x] - k2n u[x] == 0 }, u[x], x]
E^(Sqrt[k2n] x) C[1] + E^(-Sqrt[k2n] x) C[2]
now we know k2n>0 so back substitute
% /. Sqrt[k2n] -> k
E^(k x) C[1] + E^(-k x) C[2]
As a general answer I don't think there is a way to tell DSolve to make assumptions about parameters.
I'm trying to obtain the real part of the result of an operation which involves an undefined variable (let's say x).
How can I have Mathematica return x when I execute Re[x] if I know that x will never be a complex number? I think this involves telling Mathematica that x is a real, but I don't know how.
In my case the expression for which I want the real part is more complicated than a simple variable, but the concept will remain the same.
Some examples:
INPUT OUTPUT DESIRED RESULT
----- ------ --------------
Re[x] Re[x] x
Re[1] 1 1
Re[Sin[x]] Re[Sin[x]] Sin[x]
Re[1+x+I] 1 + Re[x] 1+x
Re[1 + x*I] 1-Im[x] 1
You can use for example the input Simplify[Re[x], x \[Element] Reals] which will give x as output.
Use ComplexExpand. It assumes that the variables are real unless you indicate otherwise. For example:
In[76]:= ComplexExpand[Re[x]]
Out[76]= x
In[77]:= ComplexExpand[Re[Sin[x]]]
Out[77]= Sin[x]
In[78]:= ComplexExpand[Re[1+x+I]]
Out[78]= 1+x
Two more possibilities:
Assuming[x \[Element] Reals, Refine[Re[x]]]
Refine[Re[x], x \[Element] Reals]
Both return x.
It can at times be useful to define UpValues for a symbol. This is far from robust, but it nevertheless can handle a number of cases.
Re[x] ^= x;
Im[x] ^= 0;
Re[x]
Re[1]
Re[1 + x + I]
Re[1 + x*I]
x
1
1 + x
1
Re[Sin[x]] does not evaluate as you desire, but one of the transformations used by FullSimplify does place it in a form that triggers Re[x]:
Re[Sin[x]] // FullSimplify
Sin[x]
if say i have a function given :
singlepattern = Cosh[theta] + Cosh[3theta]
How do i get a rational expression in terms of x of the function if i want to substitute Cosh[theta] by
"Cosh[theta] = ( x )/ 2 "
expression?
I retagged the question as a homework. You should look into ChebyshevT polynomials. It has the property that ChebyshevT[3, Cos[th] ]==Cos[3*th]. So for your problem the answer is
In[236]:= x/2 + ChebyshevT[3, x/2]
Out[236]= -x + x^3/2
Alternatively, you could use TrigExpand:
In[237]:= Cos[th] + Cos[3*th] // TrigExpand
Out[237]= Cos[th] + Cos[th]^3 - 3 Cos[th] Sin[th]^2
In[238]:= % /. Sin[th]^2 -> 1 - Cos[th]^2 // Expand
Out[238]= -2 Cos[th] + 4 Cos[th]^3
In[239]:= % /. Cos[th] -> x/2
Out[239]= -x + x^3/2
EDIT The reason the above has to do with the explicit question, is that Cosh[theta] == Cos[I*u] for some u. And since u or theta are formal, results will hold true.
Use Solve to solve for theta, then substitute, Expand, and Simplify:
In[16]:= TrigExpand[Cosh[3 theta] + Cosh[theta]] /.
Solve[Cosh[theta] == (x)/2, theta] // FullSimplify
During evaluation of In[16]:= Solve::ifun: Inverse functions are being used by Solve,
so some solutions may not be found; use Reduce for complete solution information. >>
Out[16]= {1/2 x (-2 + x^2), 1/2 x (-2 + x^2)}
This might interest you:
http://www.wolframalpha.com/input/?i=cosh%28x%29+%2B+cosh%283*x%29